Skip to content

Commit 9b453e1

Browse files
Return latest current-run fallback output
Co-authored-by: Codex <noreply@openai.com>
1 parent fe9f06e commit 9b453e1

2 files changed

Lines changed: 86 additions & 7 deletions

File tree

src/agents/agent.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -857,23 +857,18 @@ async def dispatch_stream_events() -> None:
857857

858858
from .items import ItemHelpers, MessageOutputItem, ToolCallOutputItem
859859

860-
latest_tool_output: str | None = None
861860
for item in reversed(run_result.new_items):
862861
if isinstance(item, MessageOutputItem):
863862
text_output = ItemHelpers.text_message_output(item)
864863
if text_output:
865864
return text_output
866865

867866
if (
868-
latest_tool_output is None
869-
and isinstance(item, ToolCallOutputItem)
867+
isinstance(item, ToolCallOutputItem)
870868
and isinstance(item.output, str)
871869
and item.output
872870
):
873-
latest_tool_output = item.output
874-
875-
if latest_tool_output is not None:
876-
return latest_tool_output
871+
return item.output
877872

878873
return run_result.final_output
879874

tests/test_agent_as_tool.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,90 @@ async def fake_run(
501501
assert output == "Current run summary"
502502

503503

504+
@pytest.mark.asyncio
505+
async def test_agent_as_tool_fallback_returns_most_recent_current_run_output(
506+
monkeypatch: pytest.MonkeyPatch,
507+
) -> None:
508+
agent = Agent(name="summarizer")
509+
510+
older_message = ResponseOutputMessage(
511+
id="msg_older",
512+
role="assistant",
513+
status="completed",
514+
type="message",
515+
content=[
516+
ResponseOutputText(
517+
annotations=[],
518+
text="Older message output",
519+
type="output_text",
520+
logprobs=[],
521+
)
522+
],
523+
)
524+
525+
class DummyResult:
526+
def __init__(self) -> None:
527+
self.final_output = ""
528+
self.new_items = [
529+
MessageOutputItem(agent=agent, raw_item=older_message),
530+
ToolCallOutputItem(
531+
agent=agent,
532+
raw_item={
533+
"call_id": "call_current",
534+
"output": "Newest tool output",
535+
"type": "function_call_output",
536+
},
537+
output="Newest tool output",
538+
),
539+
]
540+
541+
run_result = DummyResult()
542+
543+
async def fake_run(
544+
cls,
545+
starting_agent,
546+
input,
547+
*,
548+
context,
549+
max_turns,
550+
hooks,
551+
run_config,
552+
previous_response_id,
553+
conversation_id,
554+
session,
555+
):
556+
del (
557+
cls,
558+
starting_agent,
559+
input,
560+
context,
561+
max_turns,
562+
hooks,
563+
run_config,
564+
previous_response_id,
565+
conversation_id,
566+
session,
567+
)
568+
return run_result
569+
570+
monkeypatch.setattr(Runner, "run", classmethod(fake_run))
571+
572+
tool = agent.as_tool(
573+
tool_name="summary_tool",
574+
tool_description="Summarize current run output",
575+
)
576+
tool_context = ToolContext(
577+
context=None,
578+
tool_name="summary_tool",
579+
tool_call_id="call_1",
580+
tool_arguments='{"input": "hello"}',
581+
)
582+
583+
output = await tool.on_invoke_tool(tool_context, '{"input": "hello"}')
584+
585+
assert output == "Newest tool output"
586+
587+
504588
@pytest.mark.asyncio
505589
async def test_agent_as_tool_extractor_can_access_agent_tool_invocation(
506590
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)