@@ -196,42 +196,37 @@ async def validate_response_output( # noqa: C901
196196
197197 output_tool_name = final_result_event .tool_name
198198
199- if self ._output_schema .toolset and output_tool_name is not None :
200- tool_call = next (
201- (part for part in message .tool_calls if part .tool_name == output_tool_name ),
202- None ,
203- )
204- if tool_call is None :
205- raise exceptions .UnexpectedModelBehavior ( # pragma: no cover
206- f'Invalid response, unable to find tool call for { output_tool_name !r} '
199+ try :
200+ if self ._output_schema .toolset and output_tool_name is not None :
201+ tool_call = next (
202+ (part for part in message .tool_calls if part .tool_name == output_tool_name ),
203+ None ,
207204 )
208- try :
205+ if tool_call is None :
206+ raise exceptions .UnexpectedModelBehavior ( # pragma: no cover
207+ f'Invalid response, unable to find tool call for { output_tool_name !r} '
208+ )
209209 return await self ._tool_manager .handle_call (
210210 tool_call , allow_partial = allow_partial , wrap_validation_errors = False
211211 )
212- except (ValidationError , exceptions .ToolRetryError ) as e :
213- if not allow_partial :
214- raise exceptions .UnexpectedModelBehavior ('Invalid output' ) from e
215- raise
216- elif deferred_tool_requests := _get_deferred_tool_requests (message .tool_calls , self ._tool_manager ):
217- if not self ._output_schema .allows_deferred_tools :
218- raise exceptions .UserError (
219- 'A deferred tool call was present, but `DeferredToolRequests` is not among output types. To resolve this, add `DeferredToolRequests` to the list of output types for this agent.'
220- )
221- return cast (OutputDataT , deferred_tool_requests )
222- elif self ._output_schema .allows_image and message .images :
223- return cast (OutputDataT , message .images [0 ])
224- elif text_processor := self ._output_schema .text_processor :
225- text = ''
226- for part in message .parts :
227- if isinstance (part , _messages .TextPart ):
228- text += part .content
229- elif isinstance (part , _messages .BuiltinToolCallPart ):
230- # Text parts before a built-in tool call are essentially thoughts,
231- # not part of the final result output, so we reset the accumulated text
232- text = ''
212+ elif deferred_tool_requests := _get_deferred_tool_requests (message .tool_calls , self ._tool_manager ):
213+ if not self ._output_schema .allows_deferred_tools :
214+ raise exceptions .UserError (
215+ 'A deferred tool call was present, but `DeferredToolRequests` is not among output types. To resolve this, add `DeferredToolRequests` to the list of output types for this agent.'
216+ )
217+ return cast (OutputDataT , deferred_tool_requests )
218+ elif self ._output_schema .allows_image and message .images :
219+ return cast (OutputDataT , message .images [0 ])
220+ elif text_processor := self ._output_schema .text_processor :
221+ text = ''
222+ for part in message .parts :
223+ if isinstance (part , _messages .TextPart ):
224+ text += part .content
225+ elif isinstance (part , _messages .BuiltinToolCallPart ):
226+ # Text parts before a built-in tool call are essentially thoughts,
227+ # not part of the final result output, so we reset the accumulated text
228+ text = ''
233229
234- try :
235230 result_data = await text_processor .process (
236231 text ,
237232 run_context = replace (self ._run_ctx , partial_output = allow_partial ),
@@ -242,15 +237,15 @@ async def validate_response_output( # noqa: C901
242237 result_data = await validator .validate (
243238 result_data , replace (self ._run_ctx , partial_output = allow_partial )
244239 )
245- except ( ValidationError , exceptions . ToolRetryError ) as e :
246- if not allow_partial :
247- raise exceptions .UnexpectedModelBehavior ('Invalid output' ) from e
248- raise
249- return result_data
250- else :
251- raise exceptions . UnexpectedModelBehavior ( # pragma: no cover
252- 'Invalid response, unable to process text output'
253- )
240+ return result_data
241+ else :
242+ raise exceptions .UnexpectedModelBehavior ( # pragma: no cover
243+ 'Invalid response, unable to process text output'
244+ )
245+ except ( ValidationError , exceptions . ToolRetryError ) as e :
246+ if not allow_partial :
247+ raise exceptions . UnexpectedModelBehavior ( 'Invalid output' ) from e
248+ raise
254249
255250 async def _stream_response_text (
256251 self , * , delta : bool = False , debounce_by : float | None = 0.1
0 commit comments