@@ -105,7 +105,7 @@ def setup_once() -> None:
105105 Both paths may run. For example, the context manager exit can follow iterator exhaustion.
106106 """
107107 Messages .create = _wrap_message_create (Messages .create )
108- Stream .close = _wrap_stream_close (Stream .close )
108+ Stream .close = _wrap_close (Stream .close )
109109
110110 AsyncMessages .create = _wrap_message_create_async (AsyncMessages .create )
111111
@@ -128,7 +128,7 @@ def setup_once() -> None:
128128 # Before https://github.com/anthropics/anthropic-sdk-python/commit/b1a1c0354a9aca450a7d512fdbdeb59c0ead688a
129129 # MessageStream inherits from Stream, so patching Stream is sufficient on these versions.
130130 if not issubclass (MessageStream , Stream ):
131- MessageStream .close = _wrap_message_stream_close (MessageStream .close )
131+ MessageStream .close = _wrap_close (MessageStream .close )
132132
133133 AsyncMessages .stream = _wrap_async_message_stream (AsyncMessages .stream )
134134 AsyncMessageStreamManager .__aenter__ = (
@@ -794,15 +794,14 @@ def _finish_streaming_span(
794794 )
795795
796796
797- def _wrap_stream_close (
797+ def _wrap_close (
798798 f : "Callable[..., None]" ,
799799) -> "Callable[..., None]" :
800800 """
801- Closes the AI Client Span, unless the finally block in `_wrap_synchronous_message_iterator()` or
802- the except block in the `__next__()` patch runs first.
801+ Closes the AI Client Span, unless the finally block in `_wrap_synchronous_message_iterator()` runs first.
803802 """
804803
805- def close (self : "Stream" ) -> None :
804+ def close (self : "Union[ Stream, MessageStream] " ) -> None :
806805 if not hasattr (self , "_span" ):
807806 return f (self )
808807
@@ -948,39 +947,6 @@ def _sentry_patched_enter(self: "MessageStreamManager") -> "MessageStream":
948947 return _sentry_patched_enter
949948
950949
951- def _wrap_message_stream_close (
952- f : "Callable[..., None]" ,
953- ) -> "Callable[..., None]" :
954- """
955- Closes the AI Client Span, unless the finally block in `_wrap_synchronous_message_iterator()` or
956- the except block in the `__next__()` patch runs first.
957- """
958-
959- def close (self : "MessageStream" ) -> None :
960- if not hasattr (self , "_span" ):
961- return f (self )
962-
963- if not hasattr (self , "_model" ):
964- self ._span .__exit__ (None , None , None )
965- del self ._span
966- return f (self )
967-
968- _finish_streaming_span (
969- self ._span ,
970- self ._integration ,
971- self ._model ,
972- self ._usage ,
973- self ._content_blocks ,
974- self ._response_id ,
975- self ._finish_reason ,
976- )
977- del self ._span
978-
979- return f (self )
980-
981- return close
982-
983-
984950def _wrap_async_message_stream (f : "Any" ) -> "Any" :
985951 """
986952 Attaches user-provided arguments to the returned context manager.
0 commit comments