@@ -854,10 +854,9 @@ async def _run_single_turn_streamed(
854854 raise ModelBehaviorError ("Model did not produce a final response!" )
855855
856856 # 3. Now, we can process the turn as we do in the non-streaming case
857- single_step_result = await cls ._get_single_step_result_from_response (
857+ return await cls ._get_single_step_result_from_streamed_response (
858858 agent = agent ,
859- original_input = streamed_result .input ,
860- pre_step_items = streamed_result .new_items ,
859+ streamed_result = streamed_result ,
861860 new_response = final_response ,
862861 output_schema = output_schema ,
863862 all_tools = all_tools ,
@@ -868,9 +867,6 @@ async def _run_single_turn_streamed(
868867 tool_use_tracker = tool_use_tracker ,
869868 )
870869
871- RunImpl .stream_step_result_to_queue (single_step_result , streamed_result ._event_queue )
872- return single_step_result
873-
874870 @classmethod
875871 async def _run_single_turn (
876872 cls ,
@@ -973,6 +969,57 @@ async def _get_single_step_result_from_response(
973969 run_config = run_config ,
974970 )
975971
972+ @classmethod
973+ async def _get_single_step_result_from_streamed_response (
974+ cls ,
975+ * ,
976+ agent : Agent [TContext ],
977+ all_tools : list [Tool ],
978+ streamed_result : RunResultStreaming ,
979+ new_response : ModelResponse ,
980+ output_schema : AgentOutputSchemaBase | None ,
981+ handoffs : list [Handoff ],
982+ hooks : RunHooks [TContext ],
983+ context_wrapper : RunContextWrapper [TContext ],
984+ run_config : RunConfig ,
985+ tool_use_tracker : AgentToolUseTracker ,
986+ ) -> SingleStepResult :
987+
988+ original_input = streamed_result .input
989+ pre_step_items = streamed_result .new_items
990+ event_queue = streamed_result ._event_queue
991+
992+ processed_response = RunImpl .process_model_response (
993+ agent = agent ,
994+ all_tools = all_tools ,
995+ response = new_response ,
996+ output_schema = output_schema ,
997+ handoffs = handoffs ,
998+ )
999+ new_items_processed_response = processed_response .new_items
1000+ tool_use_tracker .add_tool_use (agent , processed_response .tools_used )
1001+ RunImpl .stream_step_items_to_queue (new_items_processed_response , event_queue )
1002+
1003+ single_step_result = await RunImpl .execute_tools_and_side_effects (
1004+ agent = agent ,
1005+ original_input = original_input ,
1006+ pre_step_items = pre_step_items ,
1007+ new_response = new_response ,
1008+ processed_response = processed_response ,
1009+ output_schema = output_schema ,
1010+ hooks = hooks ,
1011+ context_wrapper = context_wrapper ,
1012+ run_config = run_config ,
1013+ )
1014+ new_step_items = [
1015+ item
1016+ for item in single_step_result .new_step_items
1017+ if item not in new_items_processed_response
1018+ ]
1019+ RunImpl .stream_step_items_to_queue (new_step_items , event_queue )
1020+
1021+ return single_step_result
1022+
9761023 @classmethod
9771024 async def _run_input_guardrails (
9781025 cls ,
0 commit comments