Python: Fix: AgentTelemetryLayer captures context-provider-extended instructions in gen_ai.system_instructions#5294
Open
benke520 wants to merge 2 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes telemetry capture so gen_ai.system_instructions reflects context-provider-extended instructions (not just the base instructions) for both streaming and non-streaming agent invocations.
Changes:
- Introduces a new
ContextVarto carry final merged instructions from agent execution to telemetry. - Re-captures
gen_ai.system_instructionson the invoke_agent span after execution/stream consumption. - Publishes merged instructions from
_prepare_session_and_messages()after context providers have contributed.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/observability.py | Adds AGENT_MERGED_INSTRUCTIONS ContextVar and updates telemetry to re-capture final system instructions post-execution (streaming + non-streaming). |
| python/packages/core/agent_framework/_agents.py | Sets AGENT_MERGED_INSTRUCTIONS after providers have extended and merged instructions into chat_options. |
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
|||||||||||||||||||||||||||||||||||
206a06b to
94ce280
Compare
added 2 commits
April 16, 2026 11:22
…uctions AgentTelemetryLayer._trace_agent_invocation() recorded gen_ai.system_instructions from merged_options before calling execute(), which runs before context providers extend instructions via context.extend_instructions(). This meant the invoke_agent span only showed base instructions, not the full instructions sent to the LLM. Fix: introduce AGENT_MERGED_INSTRUCTIONS ContextVar that is set by _prepare_session_and_messages() after context providers run and instructions are merged. AgentTelemetryLayer reads this after execution and re-captures gen_ai.system_instructions with the complete instructions. Both streaming and non-streaming paths are fixed. Fixes microsoft#5291
94ce280 to
8c650f5
Compare
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.
Summary
Fixes #5291
AgentTelemetryLayer._trace_agent_invocation()recordsgen_ai.system_instructionsfrommerged_optionsbefore callingexecute(), but context providers run insideexecute()and extend instructions viacontext.extend_instructions(). This means theinvoke_agentspan only captures the base instructions, not the dynamically extended ones that are actually sent to the LLM.Changes
observability.pyAGENT_MERGED_INSTRUCTIONSContextVar to carry the final merged instructions from the agent layer to the telemetry layerawait super_run(), reads the ContextVar and re-setsgen_ai.system_instructionson the span with the full instructions (base + provider-extended)_finalize_stream()after the stream is consumedfinallyblocks_agents.py_prepare_session_and_messages(), setsAGENT_MERGED_INSTRUCTIONSafter context providers have run and their instructions are merged intochat_optionsHow it works
Testing
INNER_ACCUMULATED_USAGEandINNER_RESPONSE_TELEMETRY_CAPTURED_FIELDS