diff --git a/src/agents/run.py b/src/agents/run.py index 66d3d9da..ee8125dd 100644 --- a/src/agents/run.py +++ b/src/agents/run.py @@ -1972,6 +1972,7 @@ class AgentRunner: conversation_id=conversation_id, session=session, run_state=run_state, + trace_workflow_name=trace_workflow_name, is_resumed_state=is_resumed_state, sandbox_runtime=sandbox_runtime, ) diff --git a/src/agents/run_internal/run_loop.py b/src/agents/run_internal/run_loop.py index e1e1bd84..65d4e794 100644 --- a/src/agents/run_internal/run_loop.py +++ b/src/agents/run_internal/run_loop.py @@ -606,6 +606,7 @@ async def start_streaming( session: Session | None, run_state: RunState[TContext] | None = None, *, + trace_workflow_name: str, is_resumed_state: bool = False, sandbox_runtime: SandboxRuntime[TContext] | None = None, ): @@ -628,10 +629,9 @@ async def start_streaming( auto_previous_response_id=auto_previous_response_id, ) - current_trace = streamed_result.trace or get_current_trace() use_task_and_turn_spans = include_task_and_turn_spans(run_config.tracing) current_task_span: Span[TaskSpanData] | None = ( - task_span(name=current_trace.name) if current_trace and use_task_and_turn_spans else None + task_span(name=trace_workflow_name) if use_task_and_turn_spans else None ) if current_task_span: current_task_span.start(mark_as_current=True) diff --git a/tests/test_agent_tracing.py b/tests/test_agent_tracing.py index c60477cf..2a629926 100644 --- a/tests/test_agent_tracing.py +++ b/tests/test_agent_tracing.py @@ -992,6 +992,31 @@ async def test_wrapped_streaming_run_creates_root_task_span(): assert generation_spans[0].parent_id == turn_spans[0]["id"] +@pytest.mark.asyncio +async def test_wrapped_run_task_span_uses_run_workflow_name(): + def _make_agent() -> Agent[None]: + return Agent( + name="test_agent", + model=FakeModel(initial_output=[get_text_message("first_test")]), + ) + + run_config = RunConfig(workflow_name="inner_workflow") + + with trace(workflow_name="outer_workflow"): + await Runner.run(_make_agent(), input="first_test", run_config=run_config) + result = Runner.run_streamed(_make_agent(), input="first_test", run_config=run_config) + async for _ in result.stream_events(): + pass + + task_spans = [span.export() for span in fetch_ordered_spans() if span.span_data.type == "task"] + # A task span names one Runner invocation, so both runs must use their own workflow name + # rather than the enclosing trace's name. + assert [span["span_data"]["data"]["name"] for span in task_spans if span] == [ + "inner_workflow", + "inner_workflow", + ] + + @pytest.mark.asyncio async def test_wrapped_streaming_run_can_disable_task_and_turn_spans(): agent = Agent(