77
88from openai .types .responses import ResponseCompletedEvent
99
10- from agents .tool import Tool
11-
1210from ._run_impl import (
1311 AgentToolUseTracker ,
1412 NextStepFinalOutput ,
4038from .result import RunResult , RunResultStreaming
4139from .run_context import RunContextWrapper , TContext
4240from .stream_events import AgentUpdatedStreamEvent , RawResponsesStreamEvent
41+ from .tool import Tool
4342from .tracing import Span , SpanError , agent_span , get_current_trace , trace
4443from .tracing .span_data import AgentSpanData
4544from .usage import Usage
@@ -182,8 +181,6 @@ async def run(
182181 # agent changes, or if the agent loop ends.
183182 if current_span is None :
184183 handoff_names = [h .agent_name for h in cls ._get_handoffs (current_agent )]
185- all_tools = await cls ._get_all_tools (current_agent )
186- tool_names = [t .name for t in all_tools ]
187184 if output_schema := cls ._get_output_schema (current_agent ):
188185 output_type_name = output_schema .output_type_name ()
189186 else :
@@ -192,11 +189,13 @@ async def run(
192189 current_span = agent_span (
193190 name = current_agent .name ,
194191 handoffs = handoff_names ,
195- tools = tool_names ,
196192 output_type = output_type_name ,
197193 )
198194 current_span .start (mark_as_current = True )
199195
196+ all_tools = await cls ._get_all_tools (current_agent )
197+ current_span .span_data .tools = [t .name for t in all_tools ]
198+
200199 current_turn += 1
201200 if current_turn > max_turns :
202201 _error_tracing .attach_error_to_span (
@@ -504,7 +503,6 @@ async def _run_streamed_impl(
504503 # agent changes, or if the agent loop ends.
505504 if current_span is None :
506505 handoff_names = [h .agent_name for h in cls ._get_handoffs (current_agent )]
507- tool_names = [t .name for t in current_agent .tools ]
508506 if output_schema := cls ._get_output_schema (current_agent ):
509507 output_type_name = output_schema .output_type_name ()
510508 else :
@@ -513,11 +511,13 @@ async def _run_streamed_impl(
513511 current_span = agent_span (
514512 name = current_agent .name ,
515513 handoffs = handoff_names ,
516- tools = tool_names ,
517514 output_type = output_type_name ,
518515 )
519516 current_span .start (mark_as_current = True )
520517
518+ all_tools = await cls ._get_all_tools (current_agent )
519+ tool_names = [t .name for t in all_tools ]
520+ current_span .span_data .tools = tool_names
521521 current_turn += 1
522522 streamed_result .current_turn = current_turn
523523
@@ -553,6 +553,7 @@ async def _run_streamed_impl(
553553 run_config ,
554554 should_run_agent_start_hooks ,
555555 tool_use_tracker ,
556+ all_tools ,
556557 )
557558 should_run_agent_start_hooks = False
558559
@@ -621,6 +622,7 @@ async def _run_single_turn_streamed(
621622 run_config : RunConfig ,
622623 should_run_agent_start_hooks : bool ,
623624 tool_use_tracker : AgentToolUseTracker ,
625+ all_tools : list [Tool ],
624626 ) -> SingleStepResult :
625627 if should_run_agent_start_hooks :
626628 await asyncio .gather (
@@ -640,7 +642,6 @@ async def _run_single_turn_streamed(
640642 system_prompt = await agent .get_system_prompt (context_wrapper )
641643
642644 handoffs = cls ._get_handoffs (agent )
643- all_tools = await cls ._get_all_tools (agent )
644645 model = cls ._get_model (agent , run_config )
645646 model_settings = agent .model_settings .resolve (run_config .model_settings )
646647 model_settings = RunImpl .maybe_reset_tool_choice (agent , tool_use_tracker , model_settings )
0 commit comments