@@ -104,13 +104,15 @@ def setup_once() -> None:
104104 """
105105 client.messages.create(stream=True) can return an instance of the Stream class, which implements the iterator protocol.
106106 Analogously, an AsyncStream instance can be returned, which implements the asynchronous iterator protocol.
107-
108107 The underlying stream can be consumed using either __iter__ or __next__, so both are patched to intercept
109- streamed events (and analogously, asynchronous iterators are consumed using __aiter__ or __anext__). The
110- streamed events are used to populate output attributes on the AI Client Span.
108+ streamed events (and analogously, asynchronous iterators are consumed using __aiter__ or __anext__).
109+ The streamed events are used to populate output attributes on the AI Client Span.
110+
111+ The span is finished in two possible places:
112+ - When the user exits the context manager or directly calls close(), the patched close() ends the span.
113+ - When iteration ends, the finally block in the __iter__/__aiter__ patch or the except block in the __next__/__anext__ patch finishes the span.
111114
112- The close() method is patched for situations in which the method is directly invoked by the user, and otherwise
113- the finally block in the __iter__/__aiter__ patch closes the span.
115+ Both paths may run, for example, when the iterator is exhausted and then the context manager exits.
114116 """
115117 Messages .create = _wrap_message_create (Messages .create )
116118 Stream .__iter__ = _wrap_stream_iter (Stream .__iter__ )
@@ -123,12 +125,17 @@ def setup_once() -> None:
123125 AsyncStream .close = _wrap_async_stream_close (AsyncStream .close )
124126
125127 """
126- client.messages.stream() returns an instance of the MessageStream class, which implements the iterator protocol.
128+ client.messages.stream() can return an instance of the MessageStream class, which implements the iterator protocol.
129+ Analogously, an AsyncMessageStream instance can be returned, which implements the asynchronous iterator protocol.
127130 The underlying stream can be consumed using either __iter__ or __next__, so both are patched to intercept
128- streamed events. The streamed events are used to populate output attributes on the AI Client Span.
131+ streamed events (and analogously, asynchronous iterators are consumed using __aiter__ or __anext__).
132+ The streamed events are used to populate output attributes on the AI Client Span.
133+
134+ The span is finished in two possible places:
135+ - When the user exits the context manager or directly calls close(), the patched close() ends the span.
136+ - When iteration ends, the finally block in the __iter__/__aiter__ patch or the except block in the __next__/__anext__ patch finishes the span.
129137
130- The close() method is patched for situations in which the method is directly invoked by the user, and otherwise
131- the finally block in the __iter__ patch closes the span.
138+ Both paths may run, for example, when the iterator is exhausted and then the context manager exits.
132139 """
133140 Messages .stream = _wrap_message_stream (Messages .stream )
134141 MessageStreamManager .__enter__ = _wrap_message_stream_manager_enter (
0 commit comments