Skip to content

feat(anthropic): Set gen_ai.response.id span attribute#5662

Merged
ericapisani merged 3 commits intomasterfrom
ep/py-2137-set-gen-ai-response-id-47p
Mar 13, 2026
Merged

feat(anthropic): Set gen_ai.response.id span attribute#5662
ericapisani merged 3 commits intomasterfrom
ep/py-2137-set-gen-ai-response-id-47p

Conversation

@ericapisani
Copy link
Member

Set the gen_ai.response.id property on spans created by the Anthropic integration.

For non-streaming responses, the ID is read from result.id on the Message object. For streaming responses, it's captured from event.message.id in the message_start event and threaded through the iterator to be set when the stream completes.

The _collect_ai_data function's return tuple is extended with the new response_id field, and _set_output_data accepts an optional response_id parameter to set on the span.

Refs PY-2137
Closes #5659

Extract the response ID from Anthropic API responses and set it as
the gen_ai.response.id span attribute. For non-streaming responses,
read result.id directly. For streaming responses, capture
event.message.id from the message_start event.

Refs PY-2137

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@linear-code
Copy link

linear-code bot commented Mar 13, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Anthropic

  • Set gen_ai.response.id span attribute by ericapisani in #5662
  • Add gen_ai.system attribute to spans by ericapisani in #5661

Pydantic Ai

  • Support ImageUrl content type in span instrumentation by ericapisani in #5629
  • Add tool description to execute_tool spans by ericapisani in #5596

Other

  • (crons) Add owner field to MonitorConfig by julwhitney13 in #5610
  • (otlp) Add collector_url option to OTLPIntegration by sl0thentr0py in #5603

Bug Fixes 🐛

  • (celery) Propagate user-set headers by sentrivana in #5581
  • (utils) Avoid double serialization of strings in safe_serialize by ericapisani in #5587
  • Enable unused import ruff check and fix unused imports by sentrivana in #5652

Documentation 📚

  • (openai-agents) Remove inapplicable comment by alexander-alderman-webb in #5495
  • Add AGENTS.md by sentrivana in #5579
  • Add set_attribute example to changelog by sentrivana in #5578

Internal Changes 🔧

Docs

  • Remove agentic codebase documentation workflows by dingsdax in #5655
  • Switch agentic workflows from Copilot to Claude engine by dingsdax in #5654
  • Add agentic workflows for codebase documentation by dingsdax in #5649

Openai Agents

  • Do not fail on new tool fields by alexander-alderman-webb in #5625
  • Stop expecting a specific function name by alexander-alderman-webb in #5623
  • Set streaming header when library uses with_streaming_response() by alexander-alderman-webb in #5583
  • Replace mocks with httpx for streamed responses by alexander-alderman-webb in #5580
  • Replace mocks with httpx in non-MCP tool tests by alexander-alderman-webb in #5602
  • Replace mocks with httpx in MCP tool tests by alexander-alderman-webb in #5605
  • Replace mocks with httpx in handoff tests by alexander-alderman-webb in #5604
  • Replace mocks with httpx in API error test by alexander-alderman-webb in #5601
  • Replace mocks with httpx in non-error single-response tests by alexander-alderman-webb in #5600
  • Remove test for unreachable state by alexander-alderman-webb in #5584
  • Expect namespace tool field for new openai versions by alexander-alderman-webb in #5599

Other

  • (graphene) Simplify span creation by sentrivana in #5648
  • (httpx) Resolve type checking failures by alexander-alderman-webb in #5626
  • (pyramid) Support alpha suffixes in version parsing by alexander-alderman-webb in #5618
  • (rust) Don't implement separate scope management by sentrivana in #5639
  • (strawberry) Simplify span creation by sentrivana in #5647
  • Remove custom warden action by sentrivana in #5653
  • Add httpx to linting requirements by alexander-alderman-webb in #5644
  • Remove CodeQL action by sentrivana in #5616
  • Normalize dots in package names in populate_tox.py by alexander-alderman-webb in #5574
  • Do not run actions on potel-base by sentrivana in #5614

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.40s

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 14064 uncovered lines.

Files with missing lines (1)
File Patch % Lines
anthropic.py 8.87% ⚠️ 257 Missing

Generated by Codecov Action

Replace the generic "id" value with a realistic Anthropic message ID
format to make test assertions more robust and prevent false positives
from coincidental matches.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ericapisani ericapisani marked this pull request as ready for review March 13, 2026 11:04
@ericapisani ericapisani requested a review from a team as a code owner March 13, 2026 11:04
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for non-streaming messages, for the streaming messages I am not sure the ID of the mesasge_start event is useful or that it clearly maps to SPANDATA.GEN_AI_RESPONSE_ID

@ericapisani ericapisani merged commit 2faa4b1 into master Mar 13, 2026
158 checks passed
@ericapisani ericapisani deleted the ep/py-2137-set-gen-ai-response-id-47p branch March 13, 2026 12:16
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.

[Anthropic integration] Set the gen_ai.response.id property on the span

2 participants