Skip to content

Commit 0076502

Browse files
committed
fix: restore event_queue property and 3.9‑compatible typing in ToolContext
1 parent 594efb0 commit 0076502

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/agents/agent.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
if TYPE_CHECKING:
2828
from .lifecycle import AgentHooks
2929
from .mcp import MCPServer
30-
from .result import RunResult
30+
from .result import RunResult, RunResultStreaming
3131

3232

3333
@dataclass
@@ -356,9 +356,11 @@ def as_tool(
356356
self,
357357
tool_name: str | None,
358358
tool_description: str | None,
359+
*,
359360
custom_output_extractor: Callable[[RunResult], Awaitable[str]] | None = None,
360361
is_enabled: bool
361362
| Callable[[RunContextWrapper[Any], AgentBase[Any]], MaybeAwaitable[bool]] = True,
363+
stream_inner_events: bool = False,
362364
) -> Tool:
363365
"""Transform this agent into a tool, callable by other agents.
364366
@@ -387,17 +389,36 @@ def as_tool(
387389
async def run_agent(context: RunContextWrapper, input: str) -> str:
388390
from .run import Runner
389391

390-
output = await Runner.run(
391-
starting_agent=self,
392-
input=input,
393-
context=context.context,
394-
)
392+
output_run: RunResult | RunResultStreaming
393+
if stream_inner_events:
394+
from .stream_events import RunItemStreamEvent
395+
396+
sub_run = Runner.run_streamed(
397+
self,
398+
input=input,
399+
context=context.context,
400+
)
401+
parent_queue = getattr(context, "_event_queue", None)
402+
async for ev in sub_run.stream_events():
403+
if parent_queue is not None and isinstance(ev, RunItemStreamEvent):
404+
if ev.name in ("tool_called", "tool_output"):
405+
parent_queue.put_nowait(ev)
406+
output_run = sub_run
407+
else:
408+
output_run = await Runner.run(
409+
starting_agent=self,
410+
input=input,
411+
context=context.context,
412+
)
413+
395414
if custom_output_extractor:
396-
return await custom_output_extractor(output)
415+
return await custom_output_extractor(cast(Any, output_run))
397416

398-
return ItemHelpers.text_message_outputs(output.new_items)
417+
return ItemHelpers.text_message_outputs(output_run.new_items)
399418

400-
return run_agent
419+
tool = run_agent
420+
tool.stream_inner_events = stream_inner_events
421+
return tool
401422

402423
async def get_system_prompt(self, run_context: RunContextWrapper[TContext]) -> str | None:
403424
if isinstance(self.instructions, str):

src/agents/tool_context.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass, field, fields
2-
from typing import Any, Optional
2+
from typing import Any, Optional, Union
3+
import asyncio
34

45
from openai.types.responses import ResponseFunctionToolCall
56

@@ -21,14 +22,24 @@ class ToolContext(RunContextWrapper[TContext]):
2122
tool_name: str = field(default_factory=_assert_must_pass_tool_name)
2223
"""The name of the tool being invoked."""
2324

24-
tool_call_id: str = field(default_factory=_assert_must_pass_tool_call_id)
25+
tool_call_id: Union[str, int] = field(default_factory=_assert_must_pass_tool_call_id)
2526
"""The ID of the tool call."""
2627

28+
_event_queue: Optional[asyncio.Queue[Any]] = field(default=None, init=False, repr=False)
29+
30+
@property
31+
def event_queue(self) -> Optional[asyncio.Queue[Any]]:
32+
return self._event_queue
33+
34+
@event_queue.setter
35+
def event_queue(self, queue: Optional[asyncio.Queue[Any]]) -> None:
36+
self._event_queue = queue
37+
2738
@classmethod
2839
def from_agent_context(
2940
cls,
3041
context: RunContextWrapper[TContext],
31-
tool_call_id: str,
42+
tool_call_id: Union[str, int],
3243
tool_call: Optional[ResponseFunctionToolCall] = None,
3344
) -> "ToolContext":
3445
"""

0 commit comments

Comments
 (0)