Skip to content

fix(anthropic): Preserve cached _iterator property - #8163

Open
alexander-alderman-webb wants to merge 3 commits into
masterfrom
webb/anthropic/preserve-cached-property
Open

alexander-alderman-webb wants to merge 3 commits into
masterfrom
webb/anthropic/preserve-cached-property

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

See anthropics/anthropic-sdk-python@abd6424.
Since _iterator was changed from a member initialized in __init__ to a cached property, the SDK can and should use a global patch so we don't destroy the laziness of accessing the cached property.

A global patch is also more maintainable because we don't have to worry about double patching, etc ...

I've changed the generic parameter for Stream and AsyncStream to Any, since users can technically provide a custom parser.

Also fixed the following mypy errors when upgrading anthropic in uv.lock:

sentry_sdk/integrations/anthropic.py:786: error: Property "_iterator" defined in "Stream" is read-only  [misc]
sentry_sdk/integrations/anthropic.py:884: error: Property "_iterator" defined in "AsyncStream" is read-only  [misc]

Issues

Closes #8166

Reminders

@alexander-alderman-webb
alexander-alderman-webb force-pushed the webb/anthropic/preserve-cached-property branch from 8e1712b to 054e95c Compare October 30, 2026 08:47
@alexander-alderman-webb
alexander-alderman-webb force-pushed the webb/anthropic/preserve-cached-property branch from 054e95c to fe7b26b Compare October 30, 2026 08:52
@alexander-alderman-webb
alexander-alderman-webb force-pushed the webb/anthropic/preserve-cached-property branch from 6812235 to a215e50 Compare October 30, 2026 09:04
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 136856 passed | ⏭️ 7456 skipped | Total: 144312 | Pass Rate: 94.83% | Execution Time: 452m 57s

All tests are passing successfully.

✅ Patch coverage is 93.75% (30 of 32 changed executable lines covered; target 80%).
Project statement coverage is 90.33% (unchanged from base (7ad4e1a) to head (a215e50)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/integrations/anthropic.py 93.75% 30/32 covered; missed: 279, 280
Coverage diff
@@            Coverage Diff             @@
##        master     #8163       +/-##
==========================================
  Coverage    90.33%    90.33%        —%
==========================================
  Files          203       203         —
  Tracked lines     26872     26900       +28
  Branches     10044     10056       +12
==========================================
+ Hits         24272     24298       +26
- Misses        2600      2602        +2
  Partials      1520      1520         —

Generated by Coverage Action

@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review October 30, 2026 10:18
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner October 30, 2026 10:18

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a215e50. Configure here.

result._iterator = _wrap_synchronous_message_iterator( # type: ignore[misc]
cast("_PatchedRawMessageStream", result),
result._iterator,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version gate skips iterator wrapping

High Severity

The global _iterator patch is applied only when _iterator is a functools.cached_property, but per-instance wrapping is skipped for every Anthropic version >= 1.14. Those two checks are not complementary, so a 1.14+ install whose _iterator is still an eager attribute (or a different descriptor than functools.cached_property) gets neither wrap. Streaming spans then miss accumulated output data, and if the caller never calls close(), the span never finishes.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a215e50. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they ever change it from being a cached property we want to degrade gracefully (and that's more important than finishing the span).
I know this looks weird but I use the version gate for the old behavior because we know that works without checking the type of _iterator and check that _iterator is a cached property otherwise to ensure forwards compatibility.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve cached _iterator in anthropic

1 participant