@@ -904,10 +904,9 @@ async def _run_single_turn_streamed(
904904 raise ModelBehaviorError ("Model did not produce a final response!" )
905905
906906 # 3. Now, we can process the turn as we do in the non-streaming case
907- single_step_result = await cls ._get_single_step_result_from_response (
907+ return await cls ._get_single_step_result_from_streamed_response (
908908 agent = agent ,
909- original_input = streamed_result .input ,
910- pre_step_items = streamed_result .new_items ,
909+ streamed_result = streamed_result ,
911910 new_response = final_response ,
912911 output_schema = output_schema ,
913912 all_tools = all_tools ,
@@ -918,9 +917,6 @@ async def _run_single_turn_streamed(
918917 tool_use_tracker = tool_use_tracker ,
919918 )
920919
921- RunImpl .stream_step_result_to_queue (single_step_result , streamed_result ._event_queue )
922- return single_step_result
923-
924920 @classmethod
925921 async def _run_single_turn (
926922 cls ,
@@ -1023,6 +1019,57 @@ async def _get_single_step_result_from_response(
10231019 run_config = run_config ,
10241020 )
10251021
1022+ @classmethod
1023+ async def _get_single_step_result_from_streamed_response (
1024+ cls ,
1025+ * ,
1026+ agent : Agent [TContext ],
1027+ all_tools : list [Tool ],
1028+ streamed_result : RunResultStreaming ,
1029+ new_response : ModelResponse ,
1030+ output_schema : AgentOutputSchemaBase | None ,
1031+ handoffs : list [Handoff ],
1032+ hooks : RunHooks [TContext ],
1033+ context_wrapper : RunContextWrapper [TContext ],
1034+ run_config : RunConfig ,
1035+ tool_use_tracker : AgentToolUseTracker ,
1036+ ) -> SingleStepResult :
1037+
1038+ original_input = streamed_result .input
1039+ pre_step_items = streamed_result .new_items
1040+ event_queue = streamed_result ._event_queue
1041+
1042+ processed_response = RunImpl .process_model_response (
1043+ agent = agent ,
1044+ all_tools = all_tools ,
1045+ response = new_response ,
1046+ output_schema = output_schema ,
1047+ handoffs = handoffs ,
1048+ )
1049+ new_items_processed_response = processed_response .new_items
1050+ tool_use_tracker .add_tool_use (agent , processed_response .tools_used )
1051+ RunImpl .stream_step_items_to_queue (new_items_processed_response , event_queue )
1052+
1053+ single_step_result = await RunImpl .execute_tools_and_side_effects (
1054+ agent = agent ,
1055+ original_input = original_input ,
1056+ pre_step_items = pre_step_items ,
1057+ new_response = new_response ,
1058+ processed_response = processed_response ,
1059+ output_schema = output_schema ,
1060+ hooks = hooks ,
1061+ context_wrapper = context_wrapper ,
1062+ run_config = run_config ,
1063+ )
1064+ new_step_items = [
1065+ item
1066+ for item in single_step_result .new_step_items
1067+ if item not in new_items_processed_response
1068+ ]
1069+ RunImpl .stream_step_items_to_queue (new_step_items , event_queue )
1070+
1071+ return single_step_result
1072+
10261073 @classmethod
10271074 async def _run_input_guardrails (
10281075 cls ,
0 commit comments