Skip to content

Conversation

@claude89757
Copy link
Contributor

Summary

  • Fix LLM Span missing system messages when using instructions parameter in ChatOptions
  • Add system_instructions parameter to _capture_messages() calls in _trace_get_response() and _trace_get_streaming_response()
  • Add unit tests to verify the fix

Related Issue

Fixes #3163

Problem

When using ChatClient with ChatOptions.instructions, the OpenTelemetry LLM span is missing:

  • System messages in gen_ai.input.messages
  • The gen_ai.system_instructions attribute

The root cause is that _trace_get_response() and _trace_get_streaming_response() don't pass the system_instructions parameter to _capture_messages(), unlike the Agent-level tracing (_trace_agent_run()) which correctly does this.

Solution

Extract chat_options from kwargs and pass instructions to _capture_messages():

chat_options = kwargs.get("chat_options")
_capture_messages(
    span=span,
    provider_name=provider_name,
    messages=messages,
    system_instructions=getattr(chat_options, "instructions", None) if chat_options else None,
)

Test plan

  • Add unit test for non-streaming ChatClient with instructions
  • Add unit test for streaming ChatClient with instructions
  • Add unit test to verify no system_instructions when not provided
  • Run uv run poe fmt - formatting check passed
  • Run uv run poe lint - all checks passed
  • Run uv run --directory packages/core poe test - 1349 tests passed

🤖 Generated with Claude Code

- Add system_instructions parameter to _capture_messages() calls in
  _trace_get_response() and _trace_get_streaming_response()
- Extract instructions from chat_options in kwargs
- Add unit tests to verify system_instructions are captured correctly

When using ChatClient with ChatOptions.instructions, the OpenTelemetry
LLM span was missing system messages in gen_ai.input.messages and the
gen_ai.system_instructions attribute was not being set.

This fix aligns the ChatClient-level tracing with the Agent-level
tracing which already correctly passes system_instructions.

Fixes microsoft#3163

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions github-actions bot changed the title Fix: Add system_instructions to ChatClient LLM span tracing Python: Fix: Add system_instructions to ChatClient LLM span tracing Jan 11, 2026
claude89757 and others added 2 commits January 12, 2026 00:18
- Add test for empty string instructions (should not set attribute)
- Add test for list-type instructions (verify multiple items captured)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   observability.py64415076%244, 312–317, 319, 321–322, 324, 326–328, 331–333, 338–339, 345–346, 352–353, 360, 362–364, 367–369, 374–375, 381–382, 388–389, 396, 433, 436, 439–441, 444, 447–448, 451–453, 455–457, 460, 547, 549, 631, 649–650, 652, 655, 663–664, 667–670, 672, 675–677, 680–681, 694–700, 702–711, 714–718, 721–724, 726–729, 732–733, 741, 842, 844, 869–871, 993, 995, 999–1004, 1006, 1009–1013, 1015, 1293, 1373–1375, 1447–1449, 1622, 1630, 1634, 1638, 1644, 1646, 1648, 1656, 1666, 1724, 1726, 1733, 1749, 1752, 1812, 1828, 1832, 1966, 1968
TOTAL17630288183% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2622 154 💤 0 ❌ 0 🔥 59.320s ⏱️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Python] LLM Span missing system message in gen_ai.input.messages when using instructions parameter

2 participants