ref(anthropic): Skip accumulation logic for unexpected types in streamed response #5564
1 issue
find-bugs: Found 1 issue (1 high)
High
NameError when Raw* types fail to import but are still referenced in isinstance checks - `sentry_sdk/integrations/anthropic.py:42-54`
When the RawMessageStartEvent and related types fail to import (older anthropic SDK versions), message_types_have_raw_prefix is set to True. However, the streaming iterator logic then checks message_types_have_raw_prefix and not isinstance(event, (RawMessageStartEvent, ...)), which will cause a NameError because RawMessageStartEvent is undefined when the import failed. The boolean flag semantics are inverted - it's True when Raw types DON'T exist, yet the code tries to use those types in that branch.
Also found at:
sentry_sdk/integrations/anthropic.py:434-447sentry_sdk/integrations/anthropic.py:499-522
Duration: 2m 7s · Tokens: 166.5k in / 5.9k out · Cost: $0.43 (+extraction: $0.01, +merge: $0.00)
Annotations
Check failure on line 54 in sentry_sdk/integrations/anthropic.py
github-actions / warden: find-bugs
NameError when Raw* types fail to import but are still referenced in isinstance checks
When the `RawMessageStartEvent` and related types fail to import (older anthropic SDK versions), `message_types_have_raw_prefix` is set to `True`. However, the streaming iterator logic then checks `message_types_have_raw_prefix and not isinstance(event, (RawMessageStartEvent, ...))`, which will cause a `NameError` because `RawMessageStartEvent` is undefined when the import failed. The boolean flag semantics are inverted - it's `True` when Raw types DON'T exist, yet the code tries to use those types in that branch.
Check failure on line 447 in sentry_sdk/integrations/anthropic.py
github-actions / warden: find-bugs
[BPG-XWY] NameError when Raw* types fail to import but are still referenced in isinstance checks (additional location)
When the `RawMessageStartEvent` and related types fail to import (older anthropic SDK versions), `message_types_have_raw_prefix` is set to `True`. However, the streaming iterator logic then checks `message_types_have_raw_prefix and not isinstance(event, (RawMessageStartEvent, ...))`, which will cause a `NameError` because `RawMessageStartEvent` is undefined when the import failed. The boolean flag semantics are inverted - it's `True` when Raw types DON'T exist, yet the code tries to use those types in that branch.
Check failure on line 522 in sentry_sdk/integrations/anthropic.py
github-actions / warden: find-bugs
[BPG-XWY] NameError when Raw* types fail to import but are still referenced in isinstance checks (additional location)
When the `RawMessageStartEvent` and related types fail to import (older anthropic SDK versions), `message_types_have_raw_prefix` is set to `True`. However, the streaming iterator logic then checks `message_types_have_raw_prefix and not isinstance(event, (RawMessageStartEvent, ...))`, which will cause a `NameError` because `RawMessageStartEvent` is undefined when the import failed. The boolean flag semantics are inverted - it's `True` when Raw types DON'T exist, yet the code tries to use those types in that branch.