Skip to content

Fix openai_agents tracing leaving temporal:startActivity as the current span - #1860

Closed
DABH wants to merge 1 commit into
mainfrom
agents-start-activity-restore-current
Closed

DABH wants to merge 1 commit into
mainfrom
agents-start-activity-restore-current

Conversation

@DABH

@DABH DABH commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

In a workflow-hosted agent loop, spans created after a model call within the same turn (tool calls, handoffs, guardrails) appeared nested under the model call's temporal:startActivity span instead of as siblings under the turn, in both the OpenAI platform and OpenTelemetry tracing modes. The workflow outbound interceptor started that span with mark_as_current=True so the outbound header could capture its id, and nothing restored the previous current span; the done callback only finishes the span, and it may run in a different contextvars context, so resetting there is not safe. This change makes the span current only while the header is captured, using the Agents SDK's set/reset token pair, and still leaves it open until the handle completes. The same applies to temporal:startChildWorkflow and temporal:startLocalActivity. Adds a tracing test with one tool call that fails without the fix and passes with it; the existing tracing tests still pass.

Fixes #1855

…nt span

The workflow outbound interceptor marked the temporal:startActivity,
temporal:startChildWorkflow, and temporal:startLocalActivity spans current
so the outbound header could capture them, and never restored the previous
span. Spans created later in the same context, such as the tool calls that
follow a model call within an agent turn, were parented to them. Make the
span current only while the header is captured; it still stays open until
the handle completes.
@DABH

DABH commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1862. That change touches the same three methods to fix #1853 by starting each handle span in a copied contextvars Context, and as a result the span no longer stays current after the call returns, which is exactly this fix. Its regression test moved there.

@DABH DABH closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openai_agents tracing: spans after a model call nest under its temporal:startActivity span instead of the turn

1 participant