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(vertexai): end streaming spans on early exit (#1605)
Paco Cartonesfe0c2dd2e47c
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py b/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.pyindex e9f60e89..baf81da2 100644--- a/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py+++ b/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py@@ -9,6 +9,7 @@ from opentelemetry.trace import SpanKindfrom openlit.__helpers import (handle_exception,record_completion_metrics,+ safe_detach,)from openlit.instrumentation.vertexai.utils import (process_chunk,@@ -72,13 +73,19 @@ def async_send_message(self._server_port = server_portself._request_model = request_modelself._args = args+ self._streaming_response_processed = False+ self._stream_error = None+ # None stays off the span. "" would emit an empty finish reason+ # on a clean drain, because process_chunk never sets one.+ self._finish_reason = Noneasync def __aenter__(self):- await self.__wrapped__.__aenter__()+ # generate_content_async(stream=True) returns an async generator.+ # Those have no __aenter__.return selfasync def __aexit__(self, exc_type, exc_value, traceback):- await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)+ await self.aclose()def __aiter__(self):return self@@ -87,14 +94,48 @@ def async_send_message("""Delegate attribute access to the wrapped object."""return getattr(self.__wrapped__, name)- async def __anext__(self):+ async def aclose(self):+ """Close the wrapped async generator and finalize the span if it has not ended yet."""try:- chunk = await self.__wrapped__.__anext__()- process_chunk(self, chunk)- return chunk- except StopAsyncIteration:- try:- with self._span:+ await self.__wrapped__.aclose()+ finally:+ self._finalize_streaming_span(ended_cleanly=False)++ async def close(self):+ """Alias for aclose(), kept for callers using the older name."""+ await self.aclose()++ def __del__(self):+ """Finalize a span abandoned by a bare ``async for`` loop.++ Dealloc cannot await ``aclose()``. The wrapped async generator's+ own dealloc hook still closes the RPC stream.+ """+ 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. 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`` writes when nothing+ failed.+ """+ 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,@@ -106,8 +147,22 @@ def async_send_message(version=version,event_provider=event_provider,)- except Exception as e:- handle_exception(self._span, e)+ finally:+ if self._stream_error is not None:+ handle_exception(self._span, self._stream_error)+ 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):@@ -128,10 +183,10 @@ def async_send_message(awaited_wrapped = await wrapped(*args, **kwargs)except Exception as e:handle_exception(span, e)- context_api.detach(token)+ safe_detach(token)span.end()raise- context_api.detach(token)+ safe_detach(token)return TracedAsyncStream(awaited_wrapped,diff --git a/sdk/python/src/openlit/instrumentation/vertexai/utils.py b/sdk/python/src/openlit/instrumentation/vertexai/utils.pyindex 9077eb40..b55bb5aa 100644--- a/sdk/python/src/openlit/instrumentation/vertexai/utils.py+++ b/sdk/python/src/openlit/instrumentation/vertexai/utils.py@@ -566,7 +566,12 @@ 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.+ # Non-streaming scopes never set _stream_error, so they still record OK.+ if getattr(scope, "_stream_error", None) is None:+ scope._span.set_status(Status(StatusCode.OK))# Record metricsif not disable_metrics:@@ -576,6 +581,8 @@ def common_chat_logic(scope._timestamps[i] - scope._timestamps[i - 1]for i in range(1, len(scope._timestamps))]+ stream_error = getattr(scope, "_stream_error", None)+ error_type = type(stream_error).__name__ if stream_error is not None else Nonerecord_completion_metrics(metrics,SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,@@ -593,6 +600,7 @@ def common_chat_logic(cost,scope._tbt,scope._ttft,+ error_type=error_type,is_stream=is_stream,time_per_chunk_observations=inter_chunk_durations,)diff --git a/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py b/sdk/python/src/openlit/instrumentation/vertexai/vertexai.pyindex 366fdf35..b336b6c2 100644--- a/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py+++ b/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py@@ -9,6 +9,7 @@ from opentelemetry.trace import SpanKindfrom openlit.__helpers import (handle_exception,record_completion_metrics,+ safe_detach,)from openlit.instrumentation.vertexai.utils import (process_chunk,@@ -72,13 +73,19 @@ def send_message(self._server_port = server_portself._request_model = request_modelself._args = args+ self._streaming_response_processed = False+ self._stream_error = None+ # None stays off the span. "" would emit an empty finish reason+ # on a clean drain, because process_chunk never sets one.+ self._finish_reason = Nonedef __enter__(self):- self.__wrapped__.__enter__()+ # generate_content(stream=True) and send_message(stream=True) yield.+ # Those generators have no __enter__.return selfdef __exit__(self, exc_type, exc_value, traceback):- self.__wrapped__.__exit__(exc_type, exc_value, traceback)+ self.close()def __iter__(self):return self@@ -87,14 +94,43 @@ def send_message("""Delegate attribute access to the wrapped object."""return getattr(self.__wrapped__, name)- def __next__(self):+ def close(self):+ """Close the wrapped generator 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):+ """Finalize a span abandoned by a bare ``for`` loop.++ The wrapper is the iterator, so ``break`` never calls ``close()``.+ The wrapped generator's dealloc still closes the RPC stream.+ """+ 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. 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`` writes when nothing failed.+ """+ 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,@@ -106,8 +142,22 @@ def send_message(version=version,event_provider=event_provider,)- except Exception as e:- handle_exception(self._span, e)+ finally:+ if self._stream_error is not None:+ handle_exception(self._span, self._stream_error)+ 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):@@ -128,10 +178,10 @@ def send_message(awaited_wrapped = wrapped(*args, **kwargs)except Exception as e:handle_exception(span, e)- context_api.detach(token)+ safe_detach(token)span.end()raise- context_api.detach(token)+ safe_detach(token)return TracedSyncStream(awaited_wrapped,diff --git a/sdk/python/tests/test_vertexai_stream_span_lifecycle.py b/sdk/python/tests/test_vertexai_stream_span_lifecycle.pynew file mode 100644index 00000000..81454e35--- /dev/null+++ b/sdk/python/tests/test_vertexai_stream_span_lifecycle.py@@ -0,0 +1,517 @@+# pylint: disable=protected-access, too-few-public-methods+"""Vertex AI streaming spans must export on every generator exit.++``GenerativeModel.generate_content(stream=True)`` and+``ChatSession.send_message(stream=True)`` return generators. The async twins+return async generators. Neither has ``__enter__`` / ``__aenter__``. A bare+``for`` does not call ``close()`` on this wrapper, so a ``break`` exports+only when the wrapper is released.+"""++import asyncio+import gc++import pytest+from opentelemetry import context as context_api+from opentelemetry import trace as trace_api+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.vertexai import async_vertexai as async_mod+from openlit.instrumentation.vertexai import vertexai as sync_mod+from openlit.semcov import SemanticConvention++REQUEST_KWARGS = {"stream": True}+++class FakeUsageMetadata:+ """Shaped like GenerationResponse.usage_metadata."""++ def __init__(self):+ self.prompt_token_count = 10+ self.candidates_token_count = 5+ self.thoughts_token_count = 0+ self.cached_content_token_count = 0+ self.cache_creation_input_tokens = 0+++class FakeChunk:+ """Shaped like a Vertex streaming chunk."""++ def __init__(self, text, usage=None):+ self.text = text+ self.usage_metadata = usage+++class FakeInstance:+ """Minimal instance so extract_vertexai_details() resolves a model name."""++ def __init__(self):+ self._location = "us-central1"+ self._model_name = (+ "projects/p/locations/us-central1/publishers/google/models/gemini-1.5-flash"+ )+++def _chunk(text, usage=False):+ return FakeChunk(text, FakeUsageMetadata() if usage else None)+++def _tracer_and_exporter():+ OpenlitConfig.reset_to_defaults()+ exporter = InMemorySpanExporter()+ provider = TracerProvider()+ provider.add_span_processor(SimpleSpanProcessor(exporter))+ return provider.get_tracer(__name__), exporter+++def _factory(tracer, *, is_async, capture_message_content=False):+ mod = async_mod if is_async else sync_mod+ make = mod.async_send_message if is_async else mod.send_message+ return make(+ version="test",+ environment="test",+ application_name="test",+ tracer=tracer,+ pricing_info={},+ capture_message_content=capture_message_content,+ metrics=None,+ disable_metrics=True,+ )+++def _sync_generator(chunks, box, raise_after=None):+ """Same shape as ``for chunk in response_stream: yield``."""++ def _generate():+ try:+ for index, chunk in enumerate(chunks):+ if raise_after is not None and index >= raise_after:+ raise RuntimeError("stream failed")+ yield chunk+ finally:+ box["closed"] = True++ return _generate()+++def _async_generator(chunks, box, raise_after=None):+ """Same shape as the async ``for chunk in response_stream: yield``."""++ async def _generate():+ try:+ for index, chunk in enumerate(chunks):+ if raise_after is not None and index >= raise_after:+ raise RuntimeError("stream failed")+ yield chunk+ finally:+ box["closed"] = True++ return _generate()+++def _finish_reasons(span):+ return tuple(span.attributes[SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON])+++def _assert_tokens(span):+ assert span.attributes[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 10+ assert span.attributes[SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS] == 5+++async def _wait_for_async_close(box):+ """Async generator aclose() is scheduled from GC and can take two turns."""+ for _ in range(5):+ if box["closed"]:+ return+ gc.collect()+ await asyncio.sleep(0)+++def test_sync_with_break_exports_partial_span():+ """An early break inside ``with`` must end the span and close the generator."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator([_chunk("hi", usage=True), _chunk("there")], box)++ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ with traced_stream as stream:+ for _item in stream:+ break++ assert box["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[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ assert "there" not in spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ _assert_tokens(spans[0])+++def test_sync_explicit_close_exports_span():+ """Calling ``.close()`` directly must finalize."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator([_chunk("hi", usage=True)], box)++ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ next(traced_stream)+ traced_stream.close()++ assert box["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_tokens(spans[0])+++def test_sync_full_consumption_exports_exactly_one_span():+ """StopIteration and the later close() must not export a second span."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator([_chunk("partial"), _chunk(" answer", usage=True)], box)++ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ with traced_stream as stream:+ for _item in stream:+ pass++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON not in spans[0].attributes+ output = spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ assert "partial answer" in output+ _assert_tokens(spans[0])+++def test_sync_bare_for_break_exports_span_when_wrapper_is_released():+ """A bare ``for`` plus ``break`` ends the span when the wrapper is released."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator([_chunk("hi", usage=True), _chunk("there")], box)++ def run():+ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ for _item in traced_stream:+ break++ run()+ gc.collect()++ assert box["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[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ assert "there" not in spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+++def test_sync_held_stream_stays_open_until_released():+ """Keeping the stream object must leave it resumable until that name is dropped."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}+ chunks = [_chunk("one", usage=True), _chunk("two"), _chunk("three")]++ def fake_create(*_args, **_kwargs):+ return _sync_generator(chunks, box)++ response = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ for item in response:+ assert item.text == "one"+ break++ assert len(exporter.get_finished_spans()) == 0+ assert box["closed"] is False+ assert next(response).text == "two"++ del response+ gc.collect()++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert _finish_reasons(spans[0]) == ("incomplete",)+ output = spans[0].attributes[SemanticConvention.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 generator is a failed span, with text already seen."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator(+ [_chunk("hi", usage=True), _chunk("nope")], box, raise_after=1+ )++ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ with pytest.raises(RuntimeError, match="stream failed"):+ with traced_stream as stream:+ for _item in stream:+ pass++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.ERROR+ assert spans[0].attributes[SemanticConvention.ERROR_TYPE] == "RuntimeError"+ assert _finish_reasons(spans[0]) == ("error",)+ assert "hi" in spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ _assert_tokens(spans[0])+++def test_sync_caller_exception_inside_with_stays_ok():+ """A raise from user code is not a Vertex failure. The cancel is incomplete."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False, capture_message_content=True)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator([_chunk("hi", usage=True), _chunk("more")], box)++ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ with pytest.raises(ValueError, match="caller"):+ with traced_stream as stream:+ for _item in stream:+ raise ValueError("caller")++ assert box["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[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+++def test_sync_context_detached_after_early_break():+ """Finalizing the span must not leave it attached to the OTel context."""++ tracer, _exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=False)+ box = {"closed": False}++ def fake_create(*_args, **_kwargs):+ return _sync_generator([_chunk("hi")], box)++ traced_stream = wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ with traced_stream as stream:+ next(stream)++ assert (+ trace_api.get_current_span(context_api.get_current()) is trace_api.INVALID_SPAN+ )+++async def test_async_with_break_exports_partial_span():+ """Async twin of the context-manager break test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=True, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator([_chunk("hi", usage=True), _chunk("there")], box)++ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ async with traced_stream as stream:+ async for _item in stream:+ break++ assert box["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[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ assert "there" not in spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ _assert_tokens(spans[0])+++async def test_async_aclose_exports_span():+ """``aclose`` is the async generator cleanup method and must finalize the span."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=True, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator([_chunk("hi", usage=True)], box)++ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ await anext(traced_stream)+ await traced_stream.aclose()++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert _finish_reasons(spans[0]) == ("incomplete",)+ _assert_tokens(spans[0])+++async def test_async_full_consumption_exports_exactly_one_span():+ """Async full drain must export one OK span and leave finish reason unset."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=True, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator(+ [_chunk("partial"), _chunk(" answer", usage=True)], box+ )++ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ async with traced_stream as stream:+ async for _item in stream:+ pass++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON not in spans[0].attributes+ output = spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]+ assert "partial answer" in output+ _assert_tokens(spans[0])+++async def test_async_bare_for_break_exports_span_when_wrapper_is_released():+ """A bare ``async for`` ends the span in ``__del__`` without awaiting ``aclose``."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=True, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator([_chunk("hi", usage=True), _chunk("there")], box)++ async def run():+ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ async for _item in traced_stream:+ break++ await run()+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert _finish_reasons(spans[0]) == ("incomplete",)+ assert "hi" in spans[0].attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]++ await _wait_for_async_close(box)+ assert box["closed"]+++async def test_async_stream_error_inside_with_marks_span_error():+ """Async twin of the generator-failure status test."""++ tracer, exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=True, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator(+ [_chunk("hi", usage=True), _chunk("nope")], box, raise_after=1+ )++ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ with pytest.raises(RuntimeError, match="stream failed"):+ async with traced_stream as stream:+ async for _item in stream:+ pass++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.ERROR+ assert spans[0].attributes[SemanticConvention.ERROR_TYPE] == "RuntimeError"+ assert _finish_reasons(spans[0]) == ("error",)+ assert "hi" in spans[0].attributes[SemanticConvention.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 = _factory(tracer, is_async=True, capture_message_content=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator([_chunk("hi", usage=True), _chunk("more")], box)++ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)++ with pytest.raises(ValueError, match="caller"):+ async with traced_stream as stream:+ async for _item in stream:+ raise ValueError("caller")++ assert box["closed"]+ spans = exporter.get_finished_spans()+ assert len(spans) == 1+ assert spans[0].status.status_code == StatusCode.OK+ assert _finish_reasons(spans[0]) == ("incomplete",)+++async def test_async_context_detached_after_early_break():+ """Async finalization must not leave the span attached to the OTel context."""++ tracer, _exporter = _tracer_and_exporter()+ wrapper = _factory(tracer, is_async=True)+ box = {"closed": False}++ async def fake_create(*_args, **_kwargs):+ return _async_generator([_chunk("hi")], box)++ traced_stream = await wrapper(fake_create, FakeInstance(), (), REQUEST_KWARGS)+ async with traced_stream as stream:+ await anext(stream)++ assert (+ trace_api.get_current_span(context_api.get_current()) is trace_api.INVALID_SPAN+ )