Fix LangChain tracer main-agent attribute propagation timing#171
Merged
hectorhdzg merged 3 commits intoMay 27, 2026
Merged
Conversation
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
rads-1996
approved these changes
May 27, 2026
Contributor
There was a problem hiding this comment.
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 aton_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.
rads-1996
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: