Skip to content

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
microsoft:mainfrom
rads-1996:support-system-instructions
Open

Capture the agent's system prompt as the gen_ai.system_instructions span attribute in the LangChain instrumentation#232
rads-1996 wants to merge 3 commits into
microsoft:mainfrom
rads-1996:support-system-instructions

Conversation

@rads-1996

Copy link
Copy Markdown
Member

No description provided.

…pan attribute in the LangChain instrumentation
@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown

Performance comparison

Threshold: regressions >15.0% on gating scenarios fail the build. Higher ops/s is better; positive Δ means the PR is slower.

Scenario Gating Baseline (ops/s) Candidate (ops/s) Δ % Status
azure_monitor_log yes 19,033.5 19,088.0 -0.29%
azure_monitor_span yes 154,990.7 154,750.9 +0.15%
otel_log no 21,956.3 22,424.5 -2.09%
otel_span no 38,507.5 38,974.2 -1.20%

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.

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_instruction support for extracting system content from inputs["messages"] (in addition to inputs["prompts"]), and route system messages out of structured gen_ai.input.messages.
  • Emit gen_ai.system_instructions on 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_messages uses next(iter(multiple_messages)), which causes only the first item to be processed when LangChain provides a flat {"messages": [...]} list. With the new route_system_out behavior, a flat list starting with a system message would be fully dropped (system skipped, subsequent user messages never reached). Normalize flat-vs-nested messages like input_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.

Comment thread src/microsoft/opentelemetry/_genai/_langchain/_utils.py
Comment thread src/microsoft/opentelemetry/_genai/_langchain/_tracer.py
@rads-1996
rads-1996 marked this pull request as ready for review July 28, 2026 22:41
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.

2 participants