Skip to content

Commit 648a576

Browse files
fix(anthropic): Preserve cached _iterator property (#8163)
Use a global patch to preserve laziness of `_iterator` property. 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.
1 parent 71094ac commit 648a576

2 files changed

Lines changed: 2312 additions & 2249 deletions

File tree

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787

8888
from sentry_sdk._types import TextPart
8989

90-
class _PatchedRawMessageStream(Stream[RawMessageStreamEvent]):
90+
class _PatchedRawMessageStream(Stream[Any]):
9191
_span: Union[Span, StreamedSpan]
9292
_integration: "AnthropicIntegration"
9393

@@ -107,7 +107,7 @@ class _PatchedMessageStream(MessageStream):
107107
_response_id: Optional[str]
108108
_finish_reason: Optional[str]
109109

110-
class _PatchedRawAsyncMessageStream(AsyncStream[RawMessageStreamEvent]):
110+
class _PatchedRawAsyncMessageStream(AsyncStream[Any]):
111111
_span: Union[Span, StreamedSpan]
112112
_integration: "AnthropicIntegration"
113113

@@ -220,6 +220,7 @@ def __exit__(
220220
class AnthropicIntegration(Integration):
221221
identifier = "anthropic"
222222
origin = f"auto.ai.{identifier}"
223+
_version: "Union[tuple[int, ...], None]" = None
223224

224225
def __init__(self: "AnthropicIntegration", include_prompts: bool = True) -> None:
225226
self.include_prompts = include_prompts
@@ -228,6 +229,7 @@ def __init__(self: "AnthropicIntegration", include_prompts: bool = True) -> None
228229
def setup_once() -> None:
229230
version = package_version("anthropic")
230231
_check_minimum_version(AnthropicIntegration, version)
232+
AnthropicIntegration._version = version
231233

232234
"""
233235
client.messages.create(stream=True) can return an instance of the Stream class, which implements the iterator protocol.
@@ -272,6 +274,46 @@ def setup_once() -> None:
272274
if not issubclass(AsyncMessageStream, AsyncStream):
273275
AsyncMessageStream.close = _wrap_async_close(AsyncMessageStream.close) # type: ignore[method-assign]
274276

277+
try:
278+
from functools import cached_property
279+
except ImportError:
280+
return
281+
282+
# In newer anthropic SDK versions, _iterator is a cached_property instead of being eagerly assigned in __init__.
283+
# This is good for us, since we do a global patch instead of patching _iterator on every instance.
284+
# We also should apply a global patch to keep the cached "_iterator" lazy.
285+
if isinstance(Stream.__dict__.get("_iterator"), cached_property):
286+
_original_stream_iterator = Stream._iterator.func # type: ignore[attr-defined]
287+
288+
def _sentry_patched_stream_iterator(self: "Stream[Any]") -> "Any":
289+
iterator = _original_stream_iterator(self)
290+
if not hasattr(self, "_span"):
291+
return iterator
292+
return _wrap_synchronous_message_iterator(
293+
cast("_PatchedRawMessageStream", self), iterator
294+
)
295+
296+
patched = cached_property(_sentry_patched_stream_iterator)
297+
patched.__set_name__(Stream, "_iterator")
298+
Stream._iterator = patched # type: ignore[assignment,method-assign]
299+
300+
if isinstance(AsyncStream.__dict__.get("_iterator"), cached_property):
301+
_original_async_stream_iterator = AsyncStream._iterator.func # type: ignore[attr-defined]
302+
303+
def _sentry_patched_async_stream_iterator(
304+
self: "AsyncStream[Any]",
305+
) -> "Any":
306+
iterator = _original_async_stream_iterator(self)
307+
if not hasattr(self, "_span"):
308+
return iterator
309+
return _wrap_asynchronous_message_iterator(
310+
cast("_PatchedRawAsyncMessageStream", self), iterator
311+
)
312+
313+
patched = cached_property(_sentry_patched_async_stream_iterator)
314+
patched.__set_name__(AsyncStream, "_iterator")
315+
AsyncStream._iterator = patched # type: ignore[assignment,method-assign]
316+
275317

276318
def _capture_exception(exc: "Any") -> None:
277319
event, hint = event_from_exception(
@@ -786,10 +828,19 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
786828
result._integration = integration # type: ignore[attr-defined]
787829

788830
_initialize_data_accumulation_state(result)
789-
result._iterator = _wrap_synchronous_message_iterator(
790-
cast("_PatchedRawMessageStream", result),
791-
result._iterator,
792-
)
831+
# New versions use global patch since _iterator was changed to a cached property.
832+
if (
833+
AnthropicIntegration._version is not None
834+
and AnthropicIntegration._version
835+
< (
836+
1,
837+
14,
838+
)
839+
):
840+
result._iterator = _wrap_synchronous_message_iterator( # type: ignore[misc]
841+
cast("_PatchedRawMessageStream", result),
842+
result._iterator,
843+
)
793844

794845
return result
795846

@@ -884,10 +935,19 @@ async def _sentry_patched_create_async(
884935
result._integration = integration # type: ignore[attr-defined]
885936

886937
_initialize_data_accumulation_state(result)
887-
result._iterator = _wrap_asynchronous_message_iterator(
888-
cast("_PatchedRawAsyncMessageStream", result),
889-
result._iterator,
890-
)
938+
# New versions use global patch since _iterator was changed to a cached property.
939+
if (
940+
AnthropicIntegration._version is not None
941+
and AnthropicIntegration._version
942+
< (
943+
1,
944+
14,
945+
)
946+
):
947+
result._iterator = _wrap_asynchronous_message_iterator( # type: ignore[misc]
948+
cast("_PatchedRawAsyncMessageStream", result),
949+
result._iterator,
950+
)
891951

892952
return result
893953

@@ -1150,7 +1210,7 @@ def _sentry_patched_enter(self: "MessageStreamManager") -> "MessageStream":
11501210

11511211
_initialize_data_accumulation_state(stream)
11521212
stream._iterator = _wrap_synchronous_message_iterator(
1153-
stream,
1213+
cast("_PatchedMessageStream", stream),
11541214
stream._iterator,
11551215
)
11561216

@@ -1263,7 +1323,7 @@ async def _sentry_patched_aenter(
12631323

12641324
_initialize_data_accumulation_state(stream)
12651325
stream._iterator = _wrap_asynchronous_message_iterator(
1266-
stream,
1326+
cast("_PatchedAsyncMessageStream", stream),
12671327
stream._iterator,
12681328
)
12691329

0 commit comments

Comments
 (0)