Skip to content

fix(langchain): support explicit context and async context propagation (#513) - #758

Open
lmolkova wants to merge 5 commits into
open-telemetry:mainfrom
lmolkova:fix-async-context-propagation-513
Open

lmolkova wants to merge 5 commits into
open-telemetry:mainfrom
lmolkova:fix-async-context-propagation-513

Conversation

@lmolkova

Copy link
Copy Markdown
Member

What does this change do?

Fixes #513 by adding optional context and attach_to_context parameters to TelemetryHandler and passing parent context down in the LangChain callback handler. In async event loops, ambient context attachment is disabled to avoid cross-context detach errors.

Any known gaps or concerns

Context propagation to nested calls (such as auto-instrumented HTTP clients or database queries within tools) is not supported when using LangChain async API.

@lmolkova
lmolkova requested a review from a team as a code owner September 21, 2026 04:55
Copilot AI lite review requested due to automatic review settings September 21, 2026 04:55
@lmolkova lmolkova changed the title fix: support explicit context and async context propagation (#513) fix(langchain): support explicit context and async context propagation (#513) Sep 21, 2026
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 21, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-09-21 19:21 UTC

Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):

  • Inline threads: 1
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Critical and moderate findings remain unresolved in async callback handling and cross-context detach safety.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Adds explicit parent-context propagation for GenAI invocations and LangChain callback spans, with async-context handling improvements.

Changes:

  • Adds context and attach_to_context support across invocation APIs.
  • Propagates parent contexts through LangChain run hierarchies.
  • Adds tests, documentation, and changelog entries.
File Summary
util/​opentelemetry-util-genai/​tests/​test_utils.py Tests utility context behavior.
util/​opentelemetry-util-genai/​tests/​test_toolcall.py Tests detached tool invocations.
util/​opentelemetry-util-genai/​tests/​test_handler_workflow.py Tests workflow context propagation.
util/​opentelemetry-util-genai/​tests/​test_handler_retrieval.py Tests retrieval context propagation.
util/​opentelemetry-util-genai/​tests/​test_handler_fetch_response.py Tests fetch-response context propagation.
util/​opentelemetry-util-genai/​tests/​test_handler_agent.py Tests agent context behavior.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​handler.py Exposes context controls.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_workflow_invocation.py Supports workflow context propagation.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_tool_invocation.py Supports tool context propagation.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_retrieval_invocation.py Supports retrieval context propagation.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_invocation.py Implements context attachment and lifecycle handling.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_inference_invocation.py Supports inference context propagation.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_fetch_response_invocation.py Supports fetch-response context propagation.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_embedding_invocation.py Supports embedding context propagation.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​_agent_invocation.py Supports agent context propagation.
util/​opentelemetry-util-genai/​.changelog/​758.added Documents the utility API addition.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_tools.py Tests LangChain tool span parenting.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_callback_handler.py Tests callback context propagation.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_agent_classification_corpus.py Tests nested async spans.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​src/​opentelemetry/​instrumentation/​genai/​langchain/​invocation_manager.py Resolves ancestor invocation contexts.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​src/​opentelemetry/​instrumentation/​genai/​langchain/​callback_handler.py Applies parent contexts to callback spans.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​README.rst Documents async propagation limitations.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​.changelog/​758.fixed Documents the LangChain fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +188 to +192
def _should_attach_to_context() -> bool:
try:
asyncio.get_running_loop()
return False
except RuntimeError:
@lmolkova
lmolkova force-pushed the fix-async-context-propagation-513 branch from 9d06a45 to 7735877 Compare September 21, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

langchain: spans are not nested — every step becomes its own root span/trace (async), and attach/detach raises "was created in a different Context"

2 participants