feat(anthropic): Emit AI Client Spans for synchronous messages.stream()
#5565
5 issues
Medium
Span never closed if exception occurs after span.__enter__() - `sentry_sdk/integrations/anthropic.py:789-809`
In _wrap_message_stream_manager_enter, span.__enter__() is called at line 789, but there's no try/finally block to ensure the span is closed if an exception occurs before the iterator wrapper's finally block runs. If _set_common_input_data() or the iterator assignment at lines 791-809 throws, the span remains open indefinitely. Other similar functions like _sentry_patched_create_sync have a finally block to close the span on errors.
Low
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 is set to 'anthropic'. The existing test_streaming_create_message test (line 306) includes this assertion. Since _set_common_input_data is called by _wrap_message_stream_manager_enter and does set this attribute (in anthropic.py line 289), the assertion should be included for test completeness.
Also found at:
tests/integrations/anthropic/test_anthropic.py:842-843tests/integrations/anthropic/test_anthropic.py:1788-1791
Test missing assertion for GEN_AI_RESPONSE_ID span data - `tests/integrations/anthropic/test_anthropic.py:439`
The new test_stream_messages test does not assert that SPANDATA.GEN_AI_RESPONSE_ID is set. The existing test_streaming_create_message test (line 325) includes this assertion. Since the same iterator wrapper (_wrap_synchronous_message_iterator) is used for both streaming paths and the response_id is captured in _collect_ai_data and set via _set_output_data, this assertion should be included for test coverage parity.
Malformed comment contains copy-paste error - `tests/integrations/anthropic/test_anthropic.py:2919`
The comment on line 2919 says # input_tokens should be total: 19 + 2846 = test_stream_messages_input_tokens_include_cache_read_streaming but should say = 2865. This appears to be a copy-paste error where the new test function name was accidentally inserted into the comment. While this doesn't affect runtime behavior, it may confuse future maintainers.
Test comment contains copy-paste error with function name instead of expected value - `tests/integrations/anthropic/test_anthropic.py:2919`
Line 2919 has a comment that reads # input_tokens should be total: 19 + 2846 = test_stream_messages_input_tokens_include_cache_read_streaming instead of the correct # input_tokens should be total: 19 + 2846 = 2865. This appears to be a copy-paste error where the function name was accidentally included. While this doesn't affect test behavior, it could confuse future developers trying to understand the expected calculation.
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 4 | 4m 9s | $2.34 |
| find-bugs | 1 | 7m 7s | $3.22 |
| skill-scanner | 0 | 4m 31s | $0.46 |
| security-review | 0 | 2m 32s | $0.72 |
Duration: 18m 17s · Tokens: 4.9M in / 53.1k out · Cost: $6.79 (+extraction: $0.01, +merge: $0.00, +fix_gate: $0.01, +dedup: $0.01)