Skip to content

Fix LangChain tracer main-agent attribute propagation timing#171

Merged
hectorhdzg merged 3 commits into
microsoft:mainfrom
hectorhdzg:fix/langchain-main-agent-propagation-timing
May 27, 2026
Merged

Fix LangChain tracer main-agent attribute propagation timing#171
hectorhdzg merged 3 commits into
microsoft:mainfrom
hectorhdzg:fix/langchain-main-agent-propagation-timing

Conversation

@hectorhdzg
Copy link
Copy Markdown
Member

The LangChain tracer's _start_trace() was setting gen_ai.agent.* attributes on the wrapper span AFTER creating the inner child span. This meant GenAIMainAgentSpanProcessor.on_start() fired for the child before the parent had any agent attributes, so microsoft.gen_ai.main_agent.* never propagated to LLM/tool spans.

Fix: move all agent attribute set_attribute() calls on the wrapper span to immediately after its creation, before the inner span is started.

Also adds:

  • tests/genai/main_agent/test_sdk_propagation.py: real SDK span tests covering all GenAI span patterns (invoke_agent -> chat/tool, multi-agent, two-span wrapper, siblings, timing bug demonstration)
  • tests/langchain/test_main_agent_propagation.py: LangChain-specific end-to-end integration tests and attribute key compatibility checks

The LangChain tracer's _start_trace() was setting gen_ai.agent.*
attributes on the wrapper span AFTER creating the inner child span.
This meant GenAIMainAgentSpanProcessor.on_start() fired for the child
before the parent had any agent attributes, so microsoft.gen_ai.main_agent.*
never propagated to LLM/tool spans.

Fix: move all agent attribute set_attribute() calls on the wrapper span
to immediately after its creation, before the inner span is started.

Also adds:
- tests/genai/main_agent/test_sdk_propagation.py: real SDK span tests
  covering all GenAI span patterns (invoke_agent -> chat/tool, multi-agent,
  two-span wrapper, siblings, timing bug demonstration)
- tests/langchain/test_main_agent_propagation.py: LangChain-specific
  end-to-end integration tests and attribute key compatibility checks
Copilot AI review requested due to automatic review settings May 27, 2026 21:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a timing issue in the LangChain tracer where gen_ai.agent.* attributes were being set on the agent wrapper span after the inner (child) span was created, preventing GenAIMainAgentSpanProcessor.on_start() from propagating microsoft.gen_ai.main_agent.* onto downstream spans. The change ensures agent attributes are applied to the wrapper span immediately after wrapper creation and before starting the inner span, restoring correct main-agent propagation.

Changes:

  • Move wrapper-span agent attribute setting earlier in LangChainTracer._start_trace() so child spans see the parent’s agent attributes at on_start.
  • Add real OpenTelemetry SDK propagation tests covering multiple span-hierarchy patterns and the original timing bug.
  • Add LangChain tracer integration tests validating end-to-end main-agent propagation and constant/key compatibility.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/microsoft/opentelemetry/_genai/_langchain/_tracer.py Sets agent attributes on the wrapper span before creating the inner span to allow GenAIMainAgentSpanProcessor.on_start() propagation.
tests/genai/main_agent/test_sdk_propagation.py Adds real-SDK tests validating main-agent propagation across common GenAI span patterns and reproducing the timing failure scenario.
tests/langchain/test_main_agent_propagation.py Adds LangChain tracer end-to-end propagation tests plus attribute-key compatibility assertions against a365 constants.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Inline pylint disable on long import lines was not recognized by CI's
pylint version. Switch to block-level disable/enable around all
post-importorskip imports.
@hectorhdzg hectorhdzg merged commit e7da407 into microsoft:main May 27, 2026
10 checks passed
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.

3 participants