@@ -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
544594async def test_runner_preserves_direct_default_timeout_for_schema_backed_tool () -> None :
545595 model = ScriptedModel ()
0 commit comments