Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix(python-sdk): end ai21 streaming spans on early exit (#1554)
Chen Yufeiyangd653d0dca98d
diff --git a/sdk/python/src/openlit/instrumentation/ai21/ai21.py b/sdk/python/src/openlit/instrumentation/ai21/ai21.pyindex 14cbfc7e..953707f0 100644--- a/sdk/python/src/openlit/instrumentation/ai21/ai21.py+++ b/sdk/python/src/openlit/instrumentation/ai21/ai21.py@@ -69,13 +69,21 @@ def chat(self._tbt = 0self._server_address = server_addressself._server_port = server_port+ self._streaming_response_processed = False+ self._stream_error = Nonedef __enter__(self):self.__wrapped__.__enter__()return selfdef __exit__(self, exc_type, exc_value, traceback):- self.__wrapped__.__exit__(exc_type, exc_value, traceback)+ # AI21 Stream.__exit__ closes the HTTP response. Finalize even+ # when the body raised: a caller abort is incomplete, and a+ # failure raised by the stream is recorded from __next__.+ try:+ return self.__wrapped__.__exit__(exc_type, exc_value, traceback)+ finally:+ self._finalize_streaming_span(ended_cleanly=False)def __iter__(self):return self@@ -84,14 +92,47 @@ def chat("""Delegate attribute access to the wrapped object."""return getattr(self.__wrapped__, name)- def __next__(self):+ def close(self):+ """Close the wrapped stream and finalize the span if it has not ended yet."""try:- chunk = self.__wrapped__.__next__()- process_chunk(self, chunk)- return chunk- except StopIteration:- try:- with self._span:+ self.__wrapped__.close()+ finally:+ self._finalize_streaming_span(ended_cleanly=False)++ def __del__(self):+ """End a span abandoned by a bare ``for`` loop.++ ``__iter__`` returns self, so ``for chunk in stream: break``+ never calls ``close()`` or ``__exit__``. AI21's ``Stream`` has+ no destructor either, and ``__del__`` must not call ``close()``:+ the async twin's ``close`` cannot be awaited here. The socket+ stays on the explicit close path.+ """+ try:+ self._finalize_streaming_span(ended_cleanly=False)+ except Exception:+ pass++ def _finalize_streaming_span(self, ended_cleanly):+ """End the span once.++ ``ended_cleanly`` is True only for StopIteration. An empty finish+ reason is otherwise a cancel: the output-message mapper would+ store it as ``stop``. A failure raised by the wrapped stream is+ recorded after attributes are set, so partial text stays on the+ span and the status is ERROR rather than the OK that+ ``process_streaming_chat_response`` always writes.+ """+ if self._streaming_response_processed:+ return+ self._streaming_response_processed = True+ if self._stream_error is not None and not self._finish_reason:+ self._finish_reason = "error"+ elif not ended_cleanly and not self._finish_reason:+ self._finish_reason = "incomplete"+ try:+ with self._span:+ try:process_streaming_chat_response(self,pricing_info=pricing_info,@@ -103,10 +144,22 @@ def chat(version=version,event_provider=event_provider,)+ finally:+ if self._stream_error is not None:+ handle_exception(self._span, self._stream_error)+ except Exception as e:+ handle_exception(self._span, e)- except Exception as e:- handle_exception(self._span, e)-+ def __next__(self):+ try:+ chunk = self.__wrapped__.__next__()+ process_chunk(self, chunk)+ return chunk+ except StopIteration:+ self._finalize_streaming_span(ended_cleanly=True)+ raise+ except Exception as exc:+ self._stream_error = excraisedef wrapper(wrapped, instance, args, kwargs):diff --git a/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py b/sdk/python/src/openlit/instrumentation/ai21/async_ai21.pyindex b493908a..fa604737 100644--- a/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py+++ b/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py@@ -69,13 +69,21 @@ def async_chat(self._tbt = 0self._server_address = server_addressself._server_port = server_port+ self._streaming_response_processed = False+ self._stream_error = Noneasync def __aenter__(self):await self.__wrapped__.__aenter__()return selfasync def __aexit__(self, exc_type, exc_value, traceback):- await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)+ # AsyncStream.__aexit__ awaits close(). Finalize even when the+ # body raised: a caller abort is incomplete, and a failure+ # raised by the stream is recorded from __anext__.+ try:+ return await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)+ finally:+ self._finalize_streaming_span(ended_cleanly=False)def __aiter__(self):return self@@ -84,14 +92,49 @@ def async_chat("""Delegate attribute access to the wrapped object."""return getattr(self.__wrapped__, name)- async def __anext__(self):+ async def close(self):+ """Close the wrapped stream and finalize the span if it has not ended yet.++ AI21's async stream defines ``async close``, not ``aclose``.+ Leaving this to ``__getattr__`` closes the socket and skips the span.+ """try:- chunk = await self.__wrapped__.__anext__()- process_chunk(self, chunk)- return chunk- except StopAsyncIteration:- try:- with self._span:+ await self.__wrapped__.close()+ finally:+ self._finalize_streaming_span(ended_cleanly=False)++ def __del__(self):+ """End a span abandoned by a bare ``async for`` loop.++ Dealloc cannot await ``close()``. Ending the span here matches+ the moment the wrapper is released. The HTTP response stays on+ ``AsyncStream.close``, which ``__aexit__`` and ``close`` call.+ """+ try:+ self._finalize_streaming_span(ended_cleanly=False)+ except Exception:+ pass++ def _finalize_streaming_span(self, ended_cleanly):+ """End the span once.++ ``ended_cleanly`` is True only for StopAsyncIteration. An empty+ finish reason is otherwise a cancel: the output-message mapper+ would store it as ``stop``. A failure raised by the wrapped+ stream is recorded after attributes are set, so partial text+ stays on the span and the status is ERROR rather than the OK+ that ``process_streaming_chat_response`` always writes.+ """+ if self._streaming_response_processed:+ return+ self._streaming_response_processed = True+ if self._stream_error is not None and not self._finish_reason:+ self._finish_reason = "error"+ elif not ended_cleanly and not self._finish_reason:+ self._finish_reason = "incomplete"+ try:+ with self._span:+ try:process_streaming_chat_response(self,pricing_info=pricing_info,@@ -103,10 +146,22 @@ def async_chat(version=version,event_provider=event_provider,)+ finally:+ if self._stream_error is not None:+ handle_exception(self._span, self._stream_error)+ except Exception as e:+ handle_exception(self._span, e)- except Exception as e:- handle_exception(self._span, e)-+ async def __anext__(self):+ try:+ chunk = await self.__wrapped__.__anext__()+ process_chunk(self, chunk)+ return chunk+ except StopAsyncIteration:+ self._finalize_streaming_span(ended_cleanly=True)+ raise+ except Exception as exc:+ self._stream_error = excraiseasync def wrapper(wrapped, instance, args, kwargs):diff --git a/sdk/python/src/openlit/instrumentation/ai21/utils.py b/sdk/python/src/openlit/instrumentation/ai21/utils.pyindex 24ef06ae..7b77aed3 100644--- a/sdk/python/src/openlit/instrumentation/ai21/utils.py+++ b/sdk/python/src/openlit/instrumentation/ai21/utils.py@@ -564,7 +564,11 @@ def common_chat_logic(except Exception as e:logger.warning("Failed to emit inference event: %s", e, exc_info=True)- scope._span.set_status(Status(StatusCode.OK))+ # OK is sticky in the OTel SDK: a later set_status(ERROR) is ignored.+ # Stream failures set scope._stream_error before this runs, then+ # handle_exception records ERROR once these attributes are written.+ if getattr(scope, "_stream_error", None) is None:+ scope._span.set_status(Status(StatusCode.OK))# Metricsif not disable_metrics:diff --git a/sdk/python/tests/test_ai21_stream_span_lifecycle.py b/sdk/python/tests/test_ai21_stream_span_lifecycle.pynew file mode 100644index 00000000..1b539ba8--- /dev/null+++ b/sdk/python/tests/test_ai21_stream_span_lifecycle.py@@ -0,0 +1,609 @@+"""Tests that an early-exited AI21 chat stream still ends and exports its span.++AI21 returns ``ai21.stream.stream.Stream`` / ``AsyncStream``. Both are context+managers whose exit calls ``close()``. Neither is a generator, and the async+class defines ``async close`` rather than ``aclose``. A bare ``for`` / ``async+for`` therefore never closes the wrapper, and ``__getattr__`` would forward+``close`` past the span.+"""++import gc++import pytest+from opentelemetry.sdk.trace import TracerProvider+from opentelemetry.sdk.trace.export import SimpleSpanProcessor+from opentelemetry.sdk.trace.export.in_memory_span_exporter import (+ InMemorySpanExporter,+)+from opentelemetry.trace import StatusCode++from openlit._config import OpenlitConfig+from openlit.instrumentation.ai21 import ai21 as sync_mod+from openlit.instrumentation.ai21 import async_ai21 as async_mod+++def _tracer_and_exporter():+ OpenlitConfig.reset_to_defaults()+ exporter = InMemorySpanExporter()+ tracer_provider = TracerProvider()+ tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))+ return tracer_provider.get_tracer("test-ai21-stream-lifecycle"), exporter+++def _chat_kwargs():+ return {+ "model": "jamba-1.5-mini",+ "messages": [{"role": "user", "content": "ping"}],+ "stream": True,+ }+++def _content_chunk(text):+ return {+ "id": "chatcmpl-ai21",+ "choices": [{"delta": {"content": text}, "finish_reason": None}],+ }+++def _usage_chunk():+ """Last chunk shape that ``process_chunk`` will record as finish reason stop."""+ return {+ "id": "chatcmpl-ai21",+ "choices": [{"delta": {}, "finish_reason": "stop"}],+ "usage": {"prompt_tokens": 3, "completion_tokens": 2},+ }+++class FakeRawSyncStream:+ """Stand-in for ``ai21.stream.stream.Stream``.++ ``__exit__`` calls ``close()``. A bare ``for`` does not. ``raise_on_close``+ reproduces a connection error from ``close()``, which must not drop the span.+ """++ def __init__(self, chunks, raise_on_close=False, raise_after=None, on_close=None):+ self._chunks = list(chunks)+ self._index = 0+ self.closed = False+ self._raise_on_close = raise_on_close+ self._raise_after = raise_after+ self._on_close = on_close++ def __iter__(self):+ return self++ def __next__(self):+ if self._raise_after is not None and self._index >= self._raise_after:+ raise RuntimeError("stream failed")+ if self._index >= len(self._chunks):+ raise StopIteration+ chunk = self._chunks[self._index]+ self._index += 1+ return chunk++ def __enter__(self):+ return self++ def __exit__(self, exc_type, exc_value, traceback):+ self.close()+ return False++ def close(self):+ """Mark closed, matching ``Stream.close``."""+ self.closed = True+ if self._on_close is not None:+ self._on_close()+ if self._raise_on_close:+ raise RuntimeError("wrapped close failed")+++class FakeRawAsyncStream:+ """Async twin of ``FakeRawSyncStream``.++ Matches ``AsyncStream``: ``async close``, no ``aclose``, and ``__aexit__``+ awaits ``close``.+ """++ def __init__(self, chunks, raise_on_close=False, raise_after=None, on_close=None):+ self._chunks = list(chunks)+ self._index = 0+ self.closed = False+ self._raise_on_close = raise_on_close+ self._raise_after = raise_after+ self._on_close = on_close++ def __aiter__(self):+ return self++ async def __anext__(self):+ if self._raise_after is not None and self._index >= self._raise_after:+ raise RuntimeError("stream failed")+ if self._index >= len(self._chunks):+ raise StopAsyncIteration+ chunk = self._chunks[self._index]+ self._index += 1+ return chunk++ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc_value, traceback):+ await self.close()+ return False++ async def close(self):+ """Mark closed, matching ``AsyncStream.close``."""+ self.closed = True+ if self._on_close is not None:+ self._on_close()+ if self._raise_on_close:+ raise RuntimeError("wrapped close failed")+++def _sync_wrapper(tracer, capture_message_content=False):+ return sync_mod.chat(+ version="test-version",+ environment="test-env",+ application_name="test-app",+ tracer=tracer,+ pricing_info={},+ capture_message_content=capture_message_content,+ metrics=None,+ disable_metrics=True,+ )+++def _async_wrapper(tracer, capture_message_content=False):+ return async_mod.async_chat(+ version="test-version",+ environment="test-env",+ application_name="test-app",+ tracer=tracer,+ pricing_info={},+ capture_message_content=capture_message_content,+ metrics=None,+ disable_metrics=True,+ )+++def _finish_reasons(span):+ return tuple(span.attributes["gen_ai.response.finish_reasons"])+++def test_sync_stream_closed_via_context_manager_break_still_exports_span():+ """An early break inside ``with stream`` must end the span and close the socket."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawSyncStream([_content_chunk("hi"), _content_chunk("there")])++ def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())++ with traced_stream as stream:+ for _chunk in stream:+ break++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ output = spans[0].attributes["gen_ai.output.messages"]+ assert "hi" in output+ assert '"finish_reason": "incomplete"' in output+ assert '"finish_reason": "stop"' not in output+++def test_sync_stream_explicit_close_still_exports_span():+ """Calling ``.close()`` directly, with no context manager, must finalize."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawSyncStream([_content_chunk("hi")])++ def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())+ next(traced_stream)+ traced_stream.close()++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+++def test_sync_stream_full_consumption_exports_exactly_one_span():+ """StopIteration inside ``with`` must not double-finalize or rewrite stop."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawSyncStream([_content_chunk("hi"), _usage_chunk()])++ def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())++ with traced_stream as stream:+ for _chunk in stream:+ pass++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert _finish_reasons(spans[0]) == ("stop",)+ assert '"finish_reason": "stop"' in spans[0].attributes["gen_ai.output.messages"]+++def test_sync_stream_close_still_exports_span_when_wrapped_close_raises():+ """A failure from the wrapped ``close()`` must still export the span."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer)+ raw_stream = FakeRawSyncStream([_content_chunk("hi")], raise_on_close=True)++ def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())+ next(traced_stream)++ with pytest.raises(RuntimeError, match="wrapped close failed"):+ traced_stream.close()++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+++def test_sync_bare_for_break_exports_span_when_wrapper_is_released():+ """A bare ``for`` plus ``break`` never calls ``close()``.++ The span ends when the wrapper is released. ``__del__`` must not close+ the socket: AI21 only closes it from ``Stream.close``.+ """++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return FakeRawSyncStream(+ [_content_chunk("hi"), _content_chunk("there")],+ on_close=lambda: box.__setitem__("closed", True),+ )++ def run():+ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())+ for _item in traced_stream:+ break++ run()+ gc.collect()++ assert box["closed"] is False+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ assert "hi" in spans[0].attributes["gen_ai.output.messages"]+++def test_sync_held_stream_stays_open_until_released():+ """Keeping the stream object must leave it resumable.++ The span ends when that name is dropped, not at the ``break``. Dropping+ it still does not close the socket.+ """++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ chunks = [_content_chunk("one"), _content_chunk("two"), _content_chunk("three")]+ raw_stream = FakeRawSyncStream(chunks)++ def fake_create(*_args, **_kwargs):+ return raw_stream++ response = wrapper(fake_create, object(), [], _chat_kwargs())+ for item in response:+ assert item == chunks[0]+ break++ assert len(exporter.get_finished_spans()) == 0+ assert raw_stream.closed is False+ assert next(response) == chunks[1]+ assert len(exporter.get_finished_spans()) == 0++ del response+ gc.collect()++ assert raw_stream.closed is False+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ output = spans[0].attributes["gen_ai.output.messages"]+ assert "onetwo" in output+ assert "three" not in output+++def test_sync_stream_error_inside_with_marks_span_error():+ """A failure from the stream is a failed span, with text already seen."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawSyncStream(+ [_content_chunk("hi"), _content_chunk("nope")],+ raise_after=1,+ )++ def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())++ with pytest.raises(RuntimeError, match="stream failed"):+ with traced_stream as stream:+ for _item in stream:+ pass++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.ERROR+ assert spans[0].attributes["error.type"] == "RuntimeError"+ assert _finish_reasons(spans[0]) == ("error",)+ assert "hi" in spans[0].attributes["gen_ai.output.messages"]+++def test_sync_caller_exception_inside_with_stays_ok():+ """A raise from user code is not an AI21 failure. The cancel is incomplete."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _sync_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawSyncStream([_content_chunk("hi"), _content_chunk("more")])++ def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())++ with pytest.raises(ValueError, match="caller"):+ with traced_stream as stream:+ for _item in stream:+ raise ValueError("caller")++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ assert "hi" in spans[0].attributes["gen_ai.output.messages"]+++async def test_async_stream_closed_via_context_manager_break_still_exports_span():+ """Async twin of the sync early-break test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawAsyncStream([_content_chunk("hi"), _content_chunk("there")])++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())++ async with traced_stream as stream:+ async for _chunk in stream:+ break++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ output = spans[0].attributes["gen_ai.output.messages"]+ assert "hi" in output+ assert '"finish_reason": "stop"' not in output+++async def test_async_stream_explicit_close_still_exports_span():+ """``await close()`` is AI21's async cleanup. It must finalize the span.++ ``aclose`` is not part of ``AsyncStream`` and must not appear on the wrapper.+ """++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawAsyncStream([_content_chunk("hi")])++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())+ assert not hasattr(traced_stream, "aclose")+ await anext(traced_stream)+ await traced_stream.close()++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert _finish_reasons(spans[0]) == ("incomplete",)+++async def test_async_stream_full_consumption_exports_exactly_one_span():+ """Async twin of the full-consumption control test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer)+ raw_stream = FakeRawAsyncStream([_content_chunk("hi"), _usage_chunk()])++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())++ async with traced_stream as stream:+ async for _chunk in stream:+ pass++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert _finish_reasons(spans[0]) == ("stop",)+++async def test_async_stream_close_still_exports_span_when_wrapped_close_raises():+ """Async twin of the wrapped-close failure test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer)+ raw_stream = FakeRawAsyncStream([_content_chunk("hi")], raise_on_close=True)++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())+ await anext(traced_stream)++ with pytest.raises(RuntimeError, match="wrapped close failed"):+ await traced_stream.close()++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+++async def test_async_bare_for_break_exports_span_when_wrapper_is_released():+ """Async twin of the bare-loop release test.++ The span ends in ``__del__`` without closing the socket and without+ waiting on an ``aclose`` the SDK does not define.+ """++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return FakeRawAsyncStream(+ [_content_chunk("hi"), _content_chunk("there")],+ on_close=lambda: box.__setitem__("closed", True),+ )++ async def run():+ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())+ async for _item in traced_stream:+ break++ await run()+ gc.collect()++ assert box["closed"] is False+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ assert "hi" in spans[0].attributes["gen_ai.output.messages"]+++async def test_async_held_stream_stays_open_until_released():+ """Async twin of the held-reference resume test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer, capture_message_content=True)+ chunks = [_content_chunk("one"), _content_chunk("two"), _content_chunk("three")]+ raw_stream = FakeRawAsyncStream(chunks)++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ response = await wrapper(fake_create, object(), [], _chat_kwargs())+ async for item in response:+ assert item == chunks[0]+ break++ assert len(exporter.get_finished_spans()) == 0+ assert raw_stream.closed is False+ assert await anext(response) == chunks[1]+ assert len(exporter.get_finished_spans()) == 0++ del response+ gc.collect()++ assert raw_stream.closed is False+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ output = spans[0].attributes["gen_ai.output.messages"]+ assert "onetwo" in output+ assert "three" not in output+++async def test_async_stream_error_inside_with_marks_span_error():+ """Async twin of the stream-failure status test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawAsyncStream(+ [_content_chunk("hi"), _content_chunk("nope")],+ raise_after=1,+ )++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())++ with pytest.raises(RuntimeError, match="stream failed"):+ async with traced_stream as stream:+ async for _item in stream:+ pass++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.ERROR+ assert spans[0].attributes["error.type"] == "RuntimeError"+ assert _finish_reasons(spans[0]) == ("error",)+ assert "hi" in spans[0].attributes["gen_ai.output.messages"]+++async def test_async_caller_exception_inside_with_stays_ok():+ """Async twin of the caller-abort status test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _async_wrapper(tracer, capture_message_content=True)+ raw_stream = FakeRawAsyncStream([_content_chunk("hi"), _content_chunk("more")])++ async def fake_create(*_args, **_kwargs):+ return raw_stream++ traced_stream = await wrapper(fake_create, object(), [], _chat_kwargs())++ with pytest.raises(ValueError, match="caller"):+ async with traced_stream as stream:+ async for _item in stream:+ raise ValueError("caller")++ assert raw_stream.closed+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+ assert "hi" in spans[0].attributes["gen_ai.output.messages"]