feat(anthropic): Emit AI Client Spans for synchronous messages.stream()
#5565
6 issues
High
Runtime ValueError: _collect_ai_data returns 4 values but only 3 are unpacked - `sentry_sdk/integrations/anthropic.py:423-432`
The _collect_ai_data function returns a 4-tuple (model, usage, content_blocks, response_id) but this code only unpacks 3 values. This will cause a ValueError: too many values to unpack at runtime when iterating over stream events. The unpacking should include response_id as the 4th value.
Also found at:
sentry_sdk/integrations/anthropic.py:484-493
Tuple unpacking mismatch causes ValueError in synchronous message iterator - `sentry_sdk/integrations/anthropic.py:423-432`
The new _wrap_synchronous_message_iterator function at line 394 unpacks only 3 values (model, usage, content_blocks) from _collect_ai_data, but that function returns a 4-tuple that includes response_id. This causes a ValueError: too many values to unpack at runtime when iterating the stream. The existing version at line 517 correctly unpacks all 4 values.
Also found at:
sentry_sdk/integrations/anthropic.py:484-493sentry_sdk/integrations/anthropic.py:394-514
Low
Test missing assertion for GEN_AI_SYSTEM span attribute - `tests/integrations/anthropic/test_anthropic.py:421-424`
The new test_stream_messages test is missing an assertion for SPANDATA.GEN_AI_SYSTEM == "anthropic" which is present in the similar test_streaming_create_message test (line 306). This reduces test coverage for the new messages.stream() functionality, as the test doesn't verify that the gen_ai.system attribute is correctly set on the span.
Also found at:
tests/integrations/anthropic/test_anthropic.py:840-843
Test missing assertion for GEN_AI_RESPONSE_ID span attribute - `tests/integrations/anthropic/test_anthropic.py:440-443`
The new test_stream_messages test is missing an assertion for SPANDATA.GEN_AI_RESPONSE_ID which is present in the similar test_streaming_create_message test (line 325). This means the test doesn't verify that the response ID is correctly captured from the streaming response, reducing coverage for this important traceability attribute.
Copy-paste error corrupts comment with function name - `tests/integrations/anthropic/test_anthropic.py:2919`
The comment on line 2919 incorrectly contains the function name test_stream_messages_input_tokens_include_cache_read_streaming instead of the expected numerical result 2865. This appears to be a copy-paste error during editing. While this doesn't affect runtime behavior, it makes the comment confusing and misleading for developers reading the test.
Test missing assertion for GEN_AI_SYSTEM span attribute - `tests/integrations/anthropic/test_anthropic.py:421-423`
The new test_stream_messages test does not verify that SPANDATA.GEN_AI_SYSTEM is set to "anthropic". Both test_streaming_create_message (line 306) and test_streaming_create_message_async (line 540) include this assertion, indicating this is an oversight. If the implementation breaks setting this attribute, the test won't catch it.
Also found at:
tests/integrations/anthropic/test_anthropic.py:439
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 4 | 4m 7s | $2.21 |
| find-bugs | 2 | 6m 59s | $3.31 |
| skill-scanner | 0 | 1m 25s | $0.69 |
| security-review | 0 | 2m 28s | $0.87 |
Duration: 14m 59s · Tokens: 4.6M in / 52.7k out · Cost: $7.13 (+extraction: $0.01, +merge: $0.00, +fix_gate: $0.01, +dedup: $0.02)