Skip to content

Commit 160ed62

Browse files
fix: preserve SDK error metadata after late tool timeouts (#5194)
1 parent 5cdcf84 commit 160ed62

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎src/agents/tool.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2253,7 +2253,10 @@ async def _invoke_function_tool_with_metadata(
22532253
if tool_task.done() and not tool_task.cancelled():
22542254
tool_exception = tool_task.exception()
22552255
if tool_exception is None:
2256-
return _FunctionToolInvocationResult(tool_task.result())
2256+
return _FunctionToolInvocationResult(
2257+
tool_task.result(),
2258+
is_sdk_generated_error=_consume_function_tool_default_failure(context),
2259+
)
22572260
raise tool_exception from None
22582261

22592262
timeout_error = ToolTimeoutError(

‎tests/test_programmatic_tool_calling.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,56 @@ def lookup_inventory(sku: str) -> InventoryOutput:
540540
assert cast(dict[str, Any], function_output.raw_item)["output"] == expected_error
541541

542542

543+
@pytest.mark.asyncio
544+
@pytest.mark.parametrize("custom_failure", [False, True])
545+
async def test_runner_validates_only_user_output_after_late_tool_timeout(
546+
monkeypatch: pytest.MonkeyPatch, custom_failure: bool
547+
) -> None:
548+
expected_error = "An error occurred while running the tool. Please try again."
549+
550+
async def lookup_inventory() -> InventoryOutput:
551+
raise RuntimeError("inventory unavailable")
552+
553+
tool_options: dict[str, Any] = {}
554+
if custom_failure:
555+
tool_options["failure_error_function"] = lambda _context, _error: expected_error
556+
inventory_tool = function_tool(
557+
lookup_inventory, timeout=1.0, output_type=InventoryOutput, **tool_options
558+
)
559+
560+
async def wait_for_after_task_finishes(
561+
future: asyncio.Future[Any], timeout: float | None = None
562+
) -> Any:
563+
# Reproduce a deadline firing after the task settles but before its waiter resumes.
564+
await future
565+
raise asyncio.TimeoutError
566+
567+
monkeypatch.setattr(asyncio, "wait_for", wait_for_after_task_finishes)
568+
direct_call = ResponseFunctionToolCall(
569+
id="function_item",
570+
call_id=FUNCTION_CALL_ID,
571+
name=inventory_tool.name,
572+
arguments="{}",
573+
type="function_call",
574+
)
575+
model = ScriptedModel([[direct_call], [get_text_message("inventory lookup failed")]])
576+
agent = Agent(name="inventory", model=model, tools=[inventory_tool])
577+
578+
if custom_failure:
579+
with pytest.raises(UserError, match="does not match its declared output schema"):
580+
await Runner.run(agent, "Check inventory")
581+
assert len(model.calls) == 1
582+
return
583+
584+
result = await Runner.run(agent, "Check inventory")
585+
assert result.final_output == "inventory lookup failed"
586+
next_input = model.calls[-1].input
587+
assert isinstance(next_input, list)
588+
outputs = [item for item in next_input if item.get("type") == "function_call_output"]
589+
assert len(outputs) == 1
590+
assert outputs[0]["output"] == expected_error
591+
592+
543593
@pytest.mark.asyncio
544594
async def test_runner_preserves_direct_default_timeout_for_schema_backed_tool() -> None:
545595
model = ScriptedModel()

0 commit comments

Comments
 (0)