fix(tracing): name streamed task spans after the run's own workflow (#4167)

This commit is contained in:
Shaurya Singh
2026-08-03 21:57:16 -07:00
committed by GitHub
parent 7b7587425a
commit 72e7c6e549
3 changed files with 28 additions and 2 deletions
+1
View File
@@ -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,
)
+2 -2
View File
@@ -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)
+25
View File
@@ -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(