Capture the agent's system prompt as the gen_ai.system_instructions span attribute in the LangChain instrumentation - #232
Open
rads-1996 wants to merge 3 commits into
Conversation
…pan attribute in the LangChain instrumentation
Performance comparisonThreshold: regressions >15.0% on gating scenarios fail the build. Higher ops/s is better; positive Δ means the PR is slower.
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the LangChain GenAI instrumentation to capture an agent/LLM “system prompt” into the OTel semantic convention attribute gen_ai.system_instructions, including routing system messages out of gen_ai.input.messages and gating emission behind the existing sensitive-content controls.
Changes:
- Add
_extract_system_instructionsupport for extracting system content frominputs["messages"](in addition toinputs["prompts"]), and routesystemmessages out of structuredgen_ai.input.messages. - Emit
gen_ai.system_instructionson both LLM spans (_update_span) and agent wrapper spans (_finalize_agent_span) when content capture is enabled. - Add unit tests for extraction, routing/no-leak guarantees, and gating behavior; update changelog for the new feature.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/langchain/test_utils.py | Adds tests for system-instruction extraction and ensuring system prompts don’t leak into gen_ai.input.messages. |
| tests/langchain/test_tracer.py | Adds tests to verify gating and serialization shape of gen_ai.system_instructions on LLM and agent spans. |
| src/microsoft/opentelemetry/_genai/_langchain/_utils.py | Implements system-instruction extraction and routes system messages out of structured input messages. |
| src/microsoft/opentelemetry/_genai/_langchain/_tracer.py | Emits gen_ai.system_instructions on spans and aggregates first-child system instructions onto agent spans. |
| CHANGELOG.md | Documents the new unreleased feature. |
Comments suppressed due to low confidence (1)
src/microsoft/opentelemetry/_genai/_langchain/_utils.py:1355
_extract_structured_input_messagesusesnext(iter(multiple_messages)), which causes only the first item to be processed when LangChain provides a flat{"messages": [...]}list. With the newroute_system_outbehavior, a flat list starting with a system message would be fully dropped (system skipped, subsequent user messages never reached). Normalize flat-vs-nestedmessageslikeinput_messages()to ensure all messages are processed.
if multiple_messages and isinstance(multiple_messages, Iterable):
first_messages = next(iter(multiple_messages), None)
if first_messages is not None:
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
rads-1996
marked this pull request as ready for review
July 28, 2026 22:41
rads-1996
requested review from
JacksonWeber,
MSNev,
hectorhdzg,
jeremydvoss and
lzchen
as code owners
July 28, 2026 22:41
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.
No description provided.