fix(anthropic): record stream finish_reasons when content is empty - #4446
fix(anthropic): record stream finish_reasons when content is empty#4446BetterAndBetterII wants to merge 1 commit into
Conversation
Empty-content streams never populate events, so message_delta stop_reason was discarded and both gen_ai.response.finish_reasons and gen_ai.output.messages were dropped.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughThe Anthropic streaming instrumentation now preserves message-level ChangesAnthropic streaming telemetry
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to This localized change preserves finish reasons for empty-content Anthropic streams and records the empty assistant message when content tracing is enabled; no actionable merge-blocking risk remains beyond normal checks and review. Sequence Diagram(s)sequenceDiagram
participant AnthropicStream
participant complete_response
participant SpanUtils
participant Span
AnthropicStream->>complete_response: store message-level stop_reason
complete_response->>SpanUtils: pass events and stop_reason
SpanUtils->>Span: record finish reason
SpanUtils->>Span: record empty assistant message when enabled
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The changes satisfy issue
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
I have read the CLA Document and I hereby sign the CLA |
Empty-content Anthropic streaming turns drop
gen_ai.response.finish_reasonsandgen_ai.output.messages.message_deltaonly copiedstop_reasononto per-event entries, which is a no-op wheneventsis empty, andset_streaming_response_attributesthen returned early.Store the message-level
stop_reasononmessage_deltaand use it as thefinish_reasonsfallback. When content tracing is enabled, emit an empty assistantgen_ai.output.messagesentry so empty-content streams still record the turn.Fixes #4362
Test plan
message_deltawithstop_reason="end_turn"and no content blocks recordsgen_ai.response.finish_reasons=["stop"]and an empty assistantgen_ai.output.messagesentryTRACELOOP_TRACE_CONTENT=falsestill recordsfinish_reasonsand omitsoutput.messagescd packages/opentelemetry-instrumentation-anthropic && uv run pytest tests/test_semconv_span_attrs.py -vSummary by CodeRabbit
Bug Fixes
Tests