Skip to content

Commit a7c539f

Browse files
authored
Fix as_tool returning blank string on early tool termination (#2112)
1 parent 0dae2f0 commit a7c539f

File tree

2 files changed

+5
-21
lines changed

2 files changed

+5
-21
lines changed

src/agents/agent.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from .agent_output import AgentOutputSchemaBase
1414
from .guardrail import InputGuardrail, OutputGuardrail
1515
from .handoffs import Handoff
16-
from .items import ItemHelpers
1716
from .logger import logger
1817
from .mcp import MCPUtil
1918
from .model_settings import ModelSettings
@@ -417,7 +416,7 @@ def as_tool(
417416
description_override=tool_description or "",
418417
is_enabled=is_enabled,
419418
)
420-
async def run_agent(context: RunContextWrapper, input: str) -> str:
419+
async def run_agent(context: RunContextWrapper, input: str) -> Any:
421420
from .run import DEFAULT_MAX_TURNS, Runner
422421

423422
resolved_max_turns = max_turns if max_turns is not None else DEFAULT_MAX_TURNS
@@ -436,7 +435,7 @@ async def run_agent(context: RunContextWrapper, input: str) -> str:
436435
if custom_output_extractor:
437436
return await custom_output_extractor(output)
438437

439-
return ItemHelpers.text_message_outputs(output.new_items)
438+
return output.final_output
440439

441440
return run_agent
442441

tests/test_agent_as_tool.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -225,30 +225,15 @@ async def custom_extractor(result):
225225

226226

227227
@pytest.mark.asyncio
228-
async def test_agent_as_tool_returns_concatenated_text(monkeypatch: pytest.MonkeyPatch) -> None:
229-
"""Agent tool should use default text aggregation when no custom extractor is provided."""
228+
async def test_agent_as_tool_returns_final_output(monkeypatch: pytest.MonkeyPatch) -> None:
229+
"""Agent tool should return final_output when no custom extractor is provided."""
230230

231231
agent = Agent(name="storyteller")
232232

233-
message = ResponseOutputMessage(
234-
id="msg_1",
235-
role="assistant",
236-
status="completed",
237-
type="message",
238-
content=[
239-
ResponseOutputText(
240-
annotations=[],
241-
text="Hello world",
242-
type="output_text",
243-
logprobs=[],
244-
)
245-
],
246-
)
247-
248233
result = type(
249234
"DummyResult",
250235
(),
251-
{"new_items": [MessageOutputItem(agent=agent, raw_item=message)]},
236+
{"final_output": "Hello world"},
252237
)()
253238

254239
async def fake_run(

0 commit comments

Comments
 (0)