@@ -438,7 +438,7 @@ async def stream(
438438 _output_schema = ctx .deps .output_schema ,
439439 _model_request_parameters = model_request_parameters ,
440440 _output_validators = ctx .deps .output_validators ,
441- _run_ctx = build_run_context ( ctx ) ,
441+ _run_ctx = run_context ,
442442 _usage_limits = ctx .deps .usage_limits ,
443443 _tool_manager = ctx .deps .tool_manager ,
444444 )
@@ -646,6 +646,7 @@ async def _run_stream() -> AsyncIterator[_messages.HandleResponseEvent]: # noqa
646646
647647 if text_processor := output_schema .text_processor :
648648 if text :
649+ # TODO (DouweM): This could call an output function that yields custom events, but we're not in an event stream here?
649650 self ._next_node = await self ._handle_text_response (ctx , text , text_processor )
650651 return
651652 alternatives .insert (0 , 'return text' )
@@ -1072,7 +1073,8 @@ async def _call_tool(
10721073 except ToolRetryError as e :
10731074 return e .tool_retry , None
10741075
1075- if isinstance (tool_result , _messages .ToolReturn ):
1076+ tool_return : _messages .Return | None = None
1077+ if isinstance (tool_result , _messages .Return ):
10761078 tool_return = tool_result
10771079 else :
10781080 result_is_list = isinstance (tool_result , list )
@@ -1083,8 +1085,8 @@ async def _call_tool(
10831085 for content in contents :
10841086 if isinstance (content , _messages .ToolReturn ):
10851087 raise exceptions .UserError (
1086- f'The return value of tool { tool_call .tool_name !r} contains invalid nested `ToolReturn ` objects. '
1087- f'`ToolReturn ` should be used directly.'
1088+ f'The return value of tool { tool_call .tool_name !r} contains invalid nested `Return ` objects. '
1089+ f'`Return ` should be used directly.'
10881090 )
10891091 elif isinstance (content , _messages .MultiModalContent ):
10901092 identifier = content .identifier
@@ -1116,10 +1118,13 @@ async def _call_tool(
11161118 tool_name = tool_call .tool_name ,
11171119 tool_call_id = tool_call .tool_call_id ,
11181120 content = tool_return .return_value , # type: ignore
1119- metadata = tool_return .metadata ,
11201121 )
11211122
1122- return return_part , tool_return .content or None
1123+ if isinstance (tool_return , _messages .ToolReturn ):
1124+ return_part .metadata = tool_return .metadata
1125+ return return_part , tool_return .content or None
1126+ else :
1127+ return return_part , None
11231128
11241129
11251130@dataclasses .dataclass
0 commit comments