ref(anthropic): Factor out streamed result handling #5563
1 issue
find-bugs: Found 1 issue (1 medium)
Medium
Unprotected _iterator access can crash user applications when stream=True response lacks _iterator attribute - `sentry_sdk/integrations/anthropic.py:514-516`
The refactored code accesses result._iterator directly (lines 403, 481) without verifying the attribute exists, and the call to _patch_streaming_response_iterator (line 515) is not wrapped in capture_internal_exceptions(). Previously, the streaming path was inside capture_internal_exceptions() with hasattr(result, "_iterator") check. If stream=True is passed but the response object doesn't have _iterator (e.g., API version mismatch or unexpected response type), an unhandled AttributeError will propagate to the user's application instead of being gracefully logged.
Duration: 1m 58s · Tokens: 545.3k in / 4.4k out · Cost: $0.60 (+extraction: $0.00)
Annotations
Check warning on line 516 in sentry_sdk/integrations/anthropic.py
github-actions / warden: find-bugs
Unprotected _iterator access can crash user applications when stream=True response lacks _iterator attribute
The refactored code accesses `result._iterator` directly (lines 403, 481) without verifying the attribute exists, and the call to `_patch_streaming_response_iterator` (line 515) is not wrapped in `capture_internal_exceptions()`. Previously, the streaming path was inside `capture_internal_exceptions()` with `hasattr(result, "_iterator")` check. If `stream=True` is passed but the response object doesn't have `_iterator` (e.g., API version mismatch or unexpected response type), an unhandled `AttributeError` will propagate to the user's application instead of being gracefully logged.