Skip to content

feat(telemetry): emit system prompt on chat spans per GenAI semconv#1818

Merged
lizradway merged 2 commits intostrands-agents:mainfrom
sanjeed5:feat/chat-span-system-prompt
Mar 27, 2026
Merged

feat(telemetry): emit system prompt on chat spans per GenAI semconv#1818
lizradway merged 2 commits intostrands-agents:mainfrom
sanjeed5:feat/chat-span-system-prompt

Conversation

@sanjeed5
Copy link
Copy Markdown
Contributor

@sanjeed5 sanjeed5 commented Mar 5, 2026

Description

System prompts provided separately from chat history are currently missing from chat spans, which means downstream OpenTelemetry consumers cannot reconstruct the full prompt context for individual model calls.

This PR is the model/chat-span follow-up to the earlier agent-span discussion in #1452 and PR #1455. The direct motivation here is the unmet chat-span need called out in #822.

This change updates start_model_invoke_span to accept optional system_prompt and system_prompt_content arguments and emits semconv-compliant system prompt data for both supported GenAI convention modes:

  • legacy conventions: gen_ai.system.message
  • gen_ai_latest_experimental: gen_ai.system_instructions on gen_ai.client.inference.operation.details

This is backward compatible. Callers that omit the new arguments keep the existing behavior.

# Before
tracer.start_model_invoke_span(
    messages=messages,
    parent_span=span,
    model_id=model_id,
)

# After
tracer.start_model_invoke_span(
    messages=messages,
    parent_span=span,
    model_id=model_id,
    system_prompt=agent.system_prompt,
    system_prompt_content=agent._system_prompt_content,
)

Related Issues

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare
  • I ran hatch test tests/strands/telemetry/test_tracer.py tests/strands/event_loop/test_event_loop.py
  • I attempted AWS_DEFAULT_REGION=us-west-2 hatch run test-integ locally; the suite is broadly red in this environment across unrelated areas, so I did not use it as the primary signal for this patch

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@lizradway

This comment was marked as outdated.

@github-actions
Copy link
Copy Markdown

Assessment: Approve (pending minor fix)

This PR properly implements system prompt emission on chat spans per OTel GenAI semantic conventions, addressing #822. The implementation correctly handles both legacy and experimental convention modes while maintaining backward compatibility.

Review Summary
  • Code Quality: Well-structured with clear separation of concerns. The new _add_system_prompt_event method follows existing patterns.
  • Testing: Comprehensive coverage including both convention modes, edge cases (no system prompt, content blocks vs string), and event loop integration.
  • Documentation: N/A is appropriate as this is internal telemetry infrastructure with no user-facing API changes.

The falsy check issue lizradway noted on line 839 should be addressed before merge. Otherwise, this looks ready to go! 🎉

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions
Copy link
Copy Markdown

Follow-up Review: Tests are passing ✅

I verified the latest commit (e5ffedf) which addresses the falsy check issue. All 76 tracer tests and the event loop span test pass locally.

The concern about test_start_model_invoke_span_latest_conventions breaking appears to be resolved - the test passes with the current code. This may have been a transient CI issue or resolved by a rebase.

This PR is ready to merge from my perspective.

@lizradway lizradway deployed to manual-approval March 27, 2026 18:47 — with GitHub Actions Active
@lizradway lizradway self-requested a review March 27, 2026 18:50
@lizradway lizradway merged commit 194c69b into strands-agents:main Mar 27, 2026
65 of 76 checks passed
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.

4 participants