1111 AgentToolUseTracker ,
1212 NextStepFinalOutput ,
1313 NextStepHandoff ,
14+ NextStepHandoffReturnControl ,
1415 NextStepRunAgain ,
1516 QueueCompleteSentinel ,
1617 RunImpl ,
@@ -119,6 +120,7 @@ async def run(
119120 hooks : RunHooks [TContext ] | None = None ,
120121 run_config : RunConfig | None = None ,
121122 previous_response_id : str | None = None ,
123+ previous_agents : list [Agent [TContext ]] | None = None ,
122124 ) -> RunResult :
123125 """Run a workflow starting at the given agent. The agent will run in a loop until a final
124126 output is generated. The loop runs like so:
@@ -154,6 +156,8 @@ async def run(
154156 hooks = RunHooks [Any ]()
155157 if run_config is None :
156158 run_config = RunConfig ()
159+ if previous_agents is None :
160+ previous_agents = []
157161
158162 tool_use_tracker = AgentToolUseTracker ()
159163
@@ -235,6 +239,7 @@ async def run(
235239 should_run_agent_start_hooks = should_run_agent_start_hooks ,
236240 tool_use_tracker = tool_use_tracker ,
237241 previous_response_id = previous_response_id ,
242+ previous_agents = previous_agents ,
238243 ),
239244 )
240245 else :
@@ -249,6 +254,7 @@ async def run(
249254 should_run_agent_start_hooks = should_run_agent_start_hooks ,
250255 tool_use_tracker = tool_use_tracker ,
251256 previous_response_id = previous_response_id ,
257+ previous_agents = previous_agents ,
252258 )
253259 should_run_agent_start_hooks = False
254260
@@ -273,8 +279,13 @@ async def run(
273279 output_guardrail_results = output_guardrail_results ,
274280 context_wrapper = context_wrapper ,
275281 )
276- elif isinstance (turn_result .next_step , NextStepHandoff ):
277- current_agent = cast (Agent [TContext ], turn_result .next_step .new_agent )
282+ elif isinstance (turn_result .next_step , NextStepHandoff ) or isinstance (
283+ turn_result .next_step , NextStepHandoffReturnControl
284+ ):
285+ if isinstance (turn_result .next_step , NextStepHandoffReturnControl ):
286+ current_agent = turn_result .next_step .previous_agent
287+ else :
288+ current_agent = cast (Agent [TContext ], turn_result .next_step .new_agent )
278289 current_span .finish (reset_current = True )
279290 current_span = None
280291 should_run_agent_start_hooks = True
@@ -367,6 +378,7 @@ def run_streamed(
367378 hooks : RunHooks [TContext ] | None = None ,
368379 run_config : RunConfig | None = None ,
369380 previous_response_id : str | None = None ,
381+ previous_agents : list [Agent [TContext ]] | None = None ,
370382 ) -> RunResultStreaming :
371383 """Run a workflow starting at the given agent in streaming mode. The returned result object
372384 contains a method you can use to stream semantic events as they are generated.
@@ -402,6 +414,8 @@ def run_streamed(
402414 hooks = RunHooks [Any ]()
403415 if run_config is None :
404416 run_config = RunConfig ()
417+ if previous_agents is None :
418+ previous_agents = []
405419
406420 # If there's already a trace, we don't create a new one. In addition, we can't end the
407421 # trace here, because the actual work is done in `stream_events` and this method ends
@@ -450,6 +464,7 @@ def run_streamed(
450464 context_wrapper = context_wrapper ,
451465 run_config = run_config ,
452466 previous_response_id = previous_response_id ,
467+ previous_agents = previous_agents ,
453468 )
454469 )
455470 return streamed_result
@@ -508,6 +523,7 @@ async def _run_streamed_impl(
508523 context_wrapper : RunContextWrapper [TContext ],
509524 run_config : RunConfig ,
510525 previous_response_id : str | None ,
526+ previous_agents : list [Agent [TContext ]],
511527 ):
512528 if streamed_result .trace :
513529 streamed_result .trace .start (mark_as_current = True )
@@ -581,6 +597,7 @@ async def _run_streamed_impl(
581597 tool_use_tracker ,
582598 all_tools ,
583599 previous_response_id ,
600+ previous_agents ,
584601 )
585602 should_run_agent_start_hooks = False
586603
@@ -590,8 +607,14 @@ async def _run_streamed_impl(
590607 streamed_result .input = turn_result .original_input
591608 streamed_result .new_items = turn_result .generated_items
592609
593- if isinstance (turn_result .next_step , NextStepHandoff ):
594- current_agent = turn_result .next_step .new_agent
610+ if isinstance (turn_result .next_step , NextStepHandoff ) or isinstance (
611+ turn_result .next_step , NextStepHandoffReturnControl
612+ ):
613+ if isinstance (turn_result .next_step , NextStepHandoff ):
614+ current_agent = turn_result .next_step .new_agent
615+ else :
616+ current_agent = turn_result .next_step .previous_agent
617+
595618 current_span .finish (reset_current = True )
596619 current_span = None
597620 should_run_agent_start_hooks = True
@@ -666,6 +689,7 @@ async def _run_single_turn_streamed(
666689 tool_use_tracker : AgentToolUseTracker ,
667690 all_tools : list [Tool ],
668691 previous_response_id : str | None ,
692+ previous_agents : list [Agent [TContext ]],
669693 ) -> SingleStepResult :
670694 if should_run_agent_start_hooks :
671695 await asyncio .gather (
@@ -746,6 +770,7 @@ async def _run_single_turn_streamed(
746770 context_wrapper = context_wrapper ,
747771 run_config = run_config ,
748772 tool_use_tracker = tool_use_tracker ,
773+ previous_agents = previous_agents ,
749774 )
750775
751776 RunImpl .stream_step_result_to_queue (single_step_result , streamed_result ._event_queue )
@@ -765,6 +790,7 @@ async def _run_single_turn(
765790 should_run_agent_start_hooks : bool ,
766791 tool_use_tracker : AgentToolUseTracker ,
767792 previous_response_id : str | None ,
793+ previous_agents : list [Agent [TContext ]],
768794 ) -> SingleStepResult :
769795 # Ensure we run the hooks before anything else
770796 if should_run_agent_start_hooks :
@@ -809,6 +835,7 @@ async def _run_single_turn(
809835 context_wrapper = context_wrapper ,
810836 run_config = run_config ,
811837 tool_use_tracker = tool_use_tracker ,
838+ previous_agents = previous_agents ,
812839 )
813840
814841 @classmethod
@@ -826,6 +853,7 @@ async def _get_single_step_result_from_response(
826853 context_wrapper : RunContextWrapper [TContext ],
827854 run_config : RunConfig ,
828855 tool_use_tracker : AgentToolUseTracker ,
856+ previous_agents : list [Agent [TContext ]],
829857 ) -> SingleStepResult :
830858 processed_response = RunImpl .process_model_response (
831859 agent = agent ,
@@ -847,6 +875,7 @@ async def _get_single_step_result_from_response(
847875 hooks = hooks ,
848876 context_wrapper = context_wrapper ,
849877 run_config = run_config ,
878+ previous_agents = previous_agents ,
850879 )
851880
852881 @classmethod
0 commit comments