Skip to content

Keep OpenAI Agents traces connected across Temporal execution - #1856

Open
1fanwang wants to merge 3 commits into
temporalio:mainfrom
1fanwang:1fannnw/fix-agents-trace-context
Open

Keep OpenAI Agents traces connected across Temporal execution#1856
1fanwang wants to merge 3 commits into
temporalio:mainfrom
1fanwang:1fannnw/fix-agents-trace-context

Conversation

@1fanwang

Copy link
Copy Markdown
Contributor

What was changed

OpenAI Agents traces keep their root metadata and parent links when a workflow and its activities share a worker. Workers use remote parent context instead of recording caller-span replicas. Callbacks finish in their starting Context, and trace completion restores the caller's context.

Why?

Previously, workers could replace the caller's spans and leave missing parents. Callbacks also logged Failed to detach context and left finished spans current, so later work could attach to the wrong parent. Child-workflow command ordering stays unchanged.

Checklist

  1. Fixes temporalio/sdk-python#1852, temporalio/sdk-python#1853, and temporalio/sdk-python#1855.
  2. The live repro uses TestModel with Temporal Server 1.31.2, Python 3.12.13, SDK 1.32.0, Agents 0.19.4 and OpenInference 1.6.1. It makes no model API calls.
  3. The changelog is updated.

After the documented SDK build, start the server and run the test in another terminal. The before run applies this PR's tests to unchanged main; the after run uses this PR.

temporal server start-dev --ip 127.0.0.1 --port 17352 --headless
env -u OPENAI_API_KEY PYTHONUNBUFFERED=1 uv run --no-sync python -m pytest tests/contrib/openai_agents/test_openai_tracing.py -k 'otel_remote_context_preserves_root or otel_callback_spans_restore_context' -E 127.0.0.1:17352 -q -s --tb=short --color=no
Raw logs

Before:

E   AssertionError: The original client root was not exported
ValueError: <Token var=<ContextVar name='current_context' default={} at 0x10a5412b0> at 0x111763a40> was created in a different Context
Retained roots/spans/tokens: [(0, 1, 1), (0, 2, 2), (0, 3, 3)]
Caller context restored: [False, False, False]
         3 failed

After:

Missing parents: []
Retained roots/spans/tokens: [(0, 0, 0), (0, 0, 0), (0, 0, 0)]
Caller context restored: [True, True, True]
         3 passed

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang requested review from a team as code owners September 11, 2026 22:22
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 OTel bridge: started trace/span replicas corrupt OpenInference parents and leak per task

1 participant