Skip to content

fix(openai_agents): isolate concurrent workflow state - #20870

Open
yshapiro-57 wants to merge 1 commit into
mainfrom
yakov.shapiro/fix-openai-agents-concurrent-trace-state
Open

yshapiro-57 wants to merge 1 commit into
mainfrom
yakov.shapiro/fix-openai-agents-concurrent-trace-state

Conversation

@yshapiro-57

@yshapiro-57 yshapiro-57 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Human Summary

This is intended to fix the following errors in the SDK:

Error preparing LLMObs span event for span <Span(id=2273126637415999657,trace_id=141943098994275675798334088410531553281,parent_id=17672040544517427409,name=Agent workflow)>, missing span kind in span context.

I had initially put up a different PR for this, #20846. Both PRs are agent-coded. @Yun-Kim pointed out that this other PR fixes the symptom but not the root cause of the following problem:

I think your executor code starts concurrent Runner.run_streamed() asyncio tasks in on experiment span, which means both agent workflow traces then share the same APM trace ID. The problem is that our integration internally stores per-run state keyed by APM trace ID, and there's likely a race of that unwittingly shared entry that ends up being wiped before the last run finishes.
This is definitely an instrumentation issue where we assumed agent runs should be 1:1 with APM trace IDs. When someone chooses to fork shouldn't break our LLMO tracing. We just need to use the OpenAI trace ID (instead of the APM or LLMO trace IDs) as keys in the internal llmobs_traces map to solve this issue.

AI Summary

This fixes cross-talk between concurrent OpenAI Agents workflows that execute within the same APM trace. Workflow lifecycle bookkeeping now follows each OpenAI Agents trace identity, keeping LLM Observability metadata isolated through completion. Regression coverage exercises two concurrent agent runs under one traced operation.

AI Verification

  • Targeted concurrency regression:
    scripts/run-tests --venv 213dcfe -- tests/contrib/openai_agents/test_openai_agents_llmobs.py -k test_llmobs_concurrent_agents_in_same_apm_trace
  • Full OpenAI Agents integration suite:
    scripts/run-tests -s --venv 213dcfe
  • Targeted type checking:
    scripts/lint typing -- ddtrace/contrib/internal/openai_agents/processor.py ddtrace/llmobs/_integrations/openai_agents.py
  • Repository lint suite:
    scripts/lint checks

Human Verification

I have installed dd-trace-py from local and confirmed that the "missing span kind" error still happens for me on trunk, but no longer happens on this branch.

Made with Cursor

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

ddtrace/contrib/internal/openai_agents/processor.py                     @DataDog/ml-observability
ddtrace/llmobs/_integrations/openai_agents.py                           @DataDog/ml-observability
releasenotes/notes/fix-openai-agents-concurrent-traces-f1f791d2e925dc92.yaml  @DataDog/apm-python
tests/contrib/openai_agents/test_openai_agents_llmobs.py                @DataDog/ml-observability

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

There are 1 circular imports that already exist on the base branch and have not been changed by this PR.

ddtrace.errortracking._handled_exceptions.bytecode_injector -> ddtrace.errortracking._handled_exceptions.callbacks -> ddtrace.errortracking._handled_exceptions.collector -> ddtrace.errortracking._handled_exceptions.bytecode_reporting -> ddtrace.errortracking._handled_exceptions.bytecode_injector

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 201 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 201 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=132)
ddtrace.internal.ci_visibility.git_client -×-> ddtrace.trace  (product:ci_visibility -> product:tracing, score=130)
ddtrace.llmobs._integrations.litellm -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=130)
ddtrace.appsec._listeners -×-> ddtrace.trace  (product:appsec -> product:tracing, score=130)
ddtrace.internal.ci_visibility.recorder -×-> ddtrace.trace  (product:ci_visibility -> product:tracing, score=130)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@yshapiro-57
yshapiro-57 marked this pull request as ready for review October 9, 2026 20:07
@yshapiro-57
yshapiro-57 requested review from a team as code owners October 9, 2026 20:07
@yshapiro-57
yshapiro-57 requested review from duncanista and removed request for a team October 9, 2026 20:07
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T20:10:07.923140Z 62b2428 Draft marked ready
🔒 Security Review ✅ Completed 2026-10-09T20:11:21.012062Z 62b2428 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Workflow metadata creation, lookup, and cleanup consistently use the OpenAI trace ID, keeping concurrent workflows independent when they share an APM trace.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 62b2428

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.

1 participant