feat(anthropic): Emit AI Client Spans for synchronous messages.stream()
#5565
3 issues
find-bugs: Found 3 issues (1 high, 2 low)
High
ValueError at runtime - unpacking 3 values from a 4-tuple - `sentry_sdk/integrations/anthropic.py:422-431`
The new _wrap_synchronous_message_iterator (line 393-452) and _wrap_asynchronous_message_iterator (line 455-513) call _collect_ai_data but only unpack 3 values (model, usage, content_blocks). However, _collect_ai_data (line 149) returns a 4-tuple including response_id. This will raise ValueError: too many values to unpack when the code executes. The existing implementations at lines 516+ correctly unpack all 4 values.
Also found at:
sentry_sdk/integrations/anthropic.py:393-452sentry_sdk/integrations/anthropic.py:442-452
Low
Span may not be closed if exception occurs after span.__enter__() - `sentry_sdk/integrations/anthropic.py:912-932`
In _wrap_message_stream_manager_enter, span.__enter__() is called at line 912 but there's no try/except/finally block to ensure the span is properly closed if an exception occurs in subsequent operations (lines 914-932). If _set_common_input_data() or the stream._iterator assignment raises an exception, the span will remain open, causing a potential resource leak. Other wrapper functions in this file (e.g., _sentry_patched_create_sync) use a finally block to close the span on error.
Test missing assertion for GEN_AI_SYSTEM span data - `tests/integrations/anthropic/test_anthropic.py:420-423`
The new test_stream_messages test does not assert that SPANDATA.GEN_AI_SYSTEM == "anthropic" is set on the span, unlike the similar test_streaming_create_message test (line 306). The implementation sets this via _set_common_input_data, but the test doesn't verify it, which could mask regressions.
Also found at:
tests/integrations/anthropic/test_anthropic.py:439
Duration: 6m 32s · Tokens: 2.3M in / 24.9k out · Cost: $3.22 (+extraction: $0.01, +merge: $0.00)
Annotations
Check failure on line 431 in sentry_sdk/integrations/anthropic.py
sentry-warden / warden: find-bugs
ValueError at runtime - unpacking 3 values from a 4-tuple
The new `_wrap_synchronous_message_iterator` (line 393-452) and `_wrap_asynchronous_message_iterator` (line 455-513) call `_collect_ai_data` but only unpack 3 values (model, usage, content_blocks). However, `_collect_ai_data` (line 149) returns a 4-tuple including `response_id`. This will raise `ValueError: too many values to unpack` when the code executes. The existing implementations at lines 516+ correctly unpack all 4 values.
Check failure on line 452 in sentry_sdk/integrations/anthropic.py
sentry-warden / warden: find-bugs
[T23-9VG] ValueError at runtime - unpacking 3 values from a 4-tuple (additional location)
The new `_wrap_synchronous_message_iterator` (line 393-452) and `_wrap_asynchronous_message_iterator` (line 455-513) call `_collect_ai_data` but only unpack 3 values (model, usage, content_blocks). However, `_collect_ai_data` (line 149) returns a 4-tuple including `response_id`. This will raise `ValueError: too many values to unpack` when the code executes. The existing implementations at lines 516+ correctly unpack all 4 values.
Check failure on line 452 in sentry_sdk/integrations/anthropic.py
sentry-warden / warden: find-bugs
[T23-9VG] ValueError at runtime - unpacking 3 values from a 4-tuple (additional location)
The new `_wrap_synchronous_message_iterator` (line 393-452) and `_wrap_asynchronous_message_iterator` (line 455-513) call `_collect_ai_data` but only unpack 3 values (model, usage, content_blocks). However, `_collect_ai_data` (line 149) returns a 4-tuple including `response_id`. This will raise `ValueError: too many values to unpack` when the code executes. The existing implementations at lines 516+ correctly unpack all 4 values.