77
88from openai .types .responses import ResponseCompletedEvent
99
10+ from agents .tool import Tool
11+
1012from ._run_impl import (
1113 NextStepFinalOutput ,
1214 NextStepHandoff ,
@@ -177,7 +179,8 @@ async def run(
177179 # agent changes, or if the agent loop ends.
178180 if current_span is None :
179181 handoff_names = [h .agent_name for h in cls ._get_handoffs (current_agent )]
180- tool_names = [t .name for t in current_agent .tools ]
182+ all_tools = await cls ._get_all_tools (current_agent )
183+ tool_names = [t .name for t in all_tools ]
181184 if output_schema := cls ._get_output_schema (current_agent ):
182185 output_type_name = output_schema .output_type_name ()
183186 else :
@@ -217,6 +220,7 @@ async def run(
217220 ),
218221 cls ._run_single_turn (
219222 agent = current_agent ,
223+ all_tools = all_tools ,
220224 original_input = original_input ,
221225 generated_items = generated_items ,
222226 hooks = hooks ,
@@ -228,6 +232,7 @@ async def run(
228232 else :
229233 turn_result = await cls ._run_single_turn (
230234 agent = current_agent ,
235+ all_tools = all_tools ,
231236 original_input = original_input ,
232237 generated_items = generated_items ,
233238 hooks = hooks ,
@@ -627,7 +632,7 @@ async def _run_single_turn_streamed(
627632 system_prompt = await agent .get_system_prompt (context_wrapper )
628633
629634 handoffs = cls ._get_handoffs (agent )
630-
635+ all_tools = await cls . _get_all_tools ( agent )
631636 model = cls ._get_model (agent , run_config )
632637 model_settings = agent .model_settings .resolve (run_config .model_settings )
633638 final_response : ModelResponse | None = None
@@ -640,7 +645,7 @@ async def _run_single_turn_streamed(
640645 system_prompt ,
641646 input ,
642647 model_settings ,
643- agent . tools ,
648+ all_tools ,
644649 output_schema ,
645650 handoffs ,
646651 get_model_tracing_impl (
@@ -677,6 +682,7 @@ async def _run_single_turn_streamed(
677682 pre_step_items = streamed_result .new_items ,
678683 new_response = final_response ,
679684 output_schema = output_schema ,
685+ all_tools = all_tools ,
680686 handoffs = handoffs ,
681687 hooks = hooks ,
682688 context_wrapper = context_wrapper ,
@@ -691,6 +697,7 @@ async def _run_single_turn(
691697 cls ,
692698 * ,
693699 agent : Agent [TContext ],
700+ all_tools : list [Tool ],
694701 original_input : str | list [TResponseInputItem ],
695702 generated_items : list [RunItem ],
696703 hooks : RunHooks [TContext ],
@@ -721,6 +728,7 @@ async def _run_single_turn(
721728 system_prompt ,
722729 input ,
723730 output_schema ,
731+ all_tools ,
724732 handoffs ,
725733 context_wrapper ,
726734 run_config ,
@@ -732,6 +740,7 @@ async def _run_single_turn(
732740 pre_step_items = generated_items ,
733741 new_response = new_response ,
734742 output_schema = output_schema ,
743+ all_tools = all_tools ,
735744 handoffs = handoffs ,
736745 hooks = hooks ,
737746 context_wrapper = context_wrapper ,
@@ -743,6 +752,7 @@ async def _get_single_step_result_from_response(
743752 cls ,
744753 * ,
745754 agent : Agent [TContext ],
755+ all_tools : list [Tool ],
746756 original_input : str | list [TResponseInputItem ],
747757 pre_step_items : list [RunItem ],
748758 new_response : ModelResponse ,
@@ -754,6 +764,7 @@ async def _get_single_step_result_from_response(
754764 ) -> SingleStepResult :
755765 processed_response = RunImpl .process_model_response (
756766 agent = agent ,
767+ all_tools = all_tools ,
757768 response = new_response ,
758769 output_schema = output_schema ,
759770 handoffs = handoffs ,
@@ -853,6 +864,7 @@ async def _get_new_response(
853864 system_prompt : str | None ,
854865 input : list [TResponseInputItem ],
855866 output_schema : AgentOutputSchema | None ,
867+ all_tools : list [Tool ],
856868 handoffs : list [Handoff ],
857869 context_wrapper : RunContextWrapper [TContext ],
858870 run_config : RunConfig ,
@@ -863,7 +875,7 @@ async def _get_new_response(
863875 system_instructions = system_prompt ,
864876 input = input ,
865877 model_settings = model_settings ,
866- tools = agent . tools ,
878+ tools = all_tools ,
867879 output_schema = output_schema ,
868880 handoffs = handoffs ,
869881 tracing = get_model_tracing_impl (
@@ -892,6 +904,10 @@ def _get_handoffs(cls, agent: Agent[Any]) -> list[Handoff]:
892904 handoffs .append (handoff (handoff_item ))
893905 return handoffs
894906
907+ @classmethod
908+ async def _get_all_tools (cls , agent : Agent [Any ]) -> list [Tool ]:
909+ return await agent .get_all_tools ()
910+
895911 @classmethod
896912 def _get_model (cls , agent : Agent [Any ], run_config : RunConfig ) -> Model :
897913 if isinstance (run_config .model , Model ):
0 commit comments