Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(google_ai_studio): end streaming span on early break/close (#1607)

Paco Cartones51311be12f69
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
index 3d0b365e..6d58628c 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
@@ -9,6 +9,7 @@ from openlit.__helpers import (
handle_exception,
set_server_address_and_port,
record_completion_metrics,
+ safe_detach,
)
from openlit.instrumentation.google_ai_studio.utils import (
process_chat_response,
@@ -154,13 +155,16 @@ def async_generate_stream(
self._server_address = server_address
self._server_port = server_port
self._event_provider = event_provider
+ self._streaming_response_processed = False
+ self._stream_error = None
async def __aenter__(self):
- await self.__wrapped__.__aenter__()
+ # AsyncModels.generate_content_stream returns an async generator.
+ # Those have no __aenter__.
return self
async 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
@@ -169,14 +173,47 @@ def async_generate_stream(
"""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,
@@ -188,9 +225,22 @@ def async_generate_stream(
version=version,
event_provider=self._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 = exc
raise
async def wrapper(wrapped, instance, args, kwargs):
@@ -212,10 +262,10 @@ def async_generate_stream(
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/google_ai_studio/google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
index ce28d0f4..a580ece2 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
@@ -9,6 +9,7 @@ from openlit.__helpers import (
handle_exception,
set_server_address_and_port,
record_completion_metrics,
+ safe_detach,
)
from openlit.instrumentation.google_ai_studio.utils import (
process_chat_response,
@@ -154,13 +155,15 @@ def generate_stream(
self._server_address = server_address
self._server_port = server_port
self._event_provider = event_provider
+ self._streaming_response_processed = False
+ self._stream_error = None
def __enter__(self):
- self.__wrapped__.__enter__()
+ # Models.generate_content_stream yields. Generators have no __enter__.
return self
def __exit__(self, exc_type, exc_value, traceback):
- self.__wrapped__.__exit__(exc_type, exc_value, traceback)
+ self.close()
def __iter__(self):
return self
@@ -169,14 +172,43 @@ def generate_stream(
"""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,
@@ -188,9 +220,22 @@ def generate_stream(
version=version,
event_provider=self._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 = exc
raise
def wrapper(wrapped, instance, args, kwargs):
@@ -212,10 +257,10 @@ def generate_stream(
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/src/openlit/instrumentation/google_ai_studio/utils.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
index 4d54ef50..a22deb81 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
@@ -829,7 +829,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 metrics
if not disable_metrics:
@@ -839,6 +844,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 None
record_completion_metrics(
metrics,
SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
@@ -856,6 +863,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/tests/test_google_ai_studio_stream_span_lifecycle.py b/sdk/python/tests/test_google_ai_studio_stream_span_lifecycle.py
new file mode 100644
index 00000000..99707555
--- /dev/null
+++ b/sdk/python/tests/test_google_ai_studio_stream_span_lifecycle.py
@@ -0,0 +1,543 @@
+# pylint: disable=protected-access, too-few-public-methods
+"""Google AI Studio streaming spans must export on every generator exit.
+
+``Models.generate_content_stream`` yields. ``AsyncModels.generate_content_stream``
+returns an async generator. Neither object 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.google_ai_studio import (
+ async_google_ai_studio as async_mod,
+)
+from openlit.instrumentation.google_ai_studio import google_ai_studio as sync_mod
+from openlit.semcov import SemanticConvention
+
+REQUEST_KWARGS = {"model": "gemini-2.0-flash"}
+
+
+def _usage():
+ """Shaped like google-genai usage_metadata."""
+ return {
+ "prompt_token_count": 10,
+ "candidates_token_count": 5,
+ "thoughts_token_count": 0,
+ "cached_content_token_count": 0,
+ "cache_creation_input_tokens": 0,
+ }
+
+
+class FakeChunk:
+ """Shaped like a google-genai streaming chunk.
+
+ ``process_chunk`` reads ``.text`` and ``model_dump()``.
+ """
+
+ def __init__(self, text, usage=False, finish_reason=None):
+ self.text = text
+ self._usage = _usage() if usage else None
+ self._finish_reason = finish_reason
+
+ def model_dump(self, *_args, **_kwargs):
+ """Return the dict shape ``response_as_dict`` reads from google-genai."""
+ candidates = []
+ if self._finish_reason:
+ candidates = [
+ {
+ "finish_reason": self._finish_reason,
+ "content": {"parts": []},
+ }
+ ]
+ return {
+ "response_id": "resp-123",
+ "model_version": "gemini-2.0-flash",
+ "usage_metadata": self._usage,
+ "candidates": candidates,
+ }
+
+
+def _chunk(text, usage=False, finish_reason=None):
+ return FakeChunk(text, usage=usage, finish_reason=finish_reason)
+
+
+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_generate_stream if is_async else mod.generate_stream
+ 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 ``Models.generate_content_stream``."""
+
+ 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 generator returned after await."""
+
+ 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, object(), (), 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, object(), (), 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.
+
+ The terminal chunk's finish reason stays on the span. A clean drain is
+ not rewritten to 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("partial"),
+ _chunk(" answer", usage=True, finish_reason="STOP"),
+ ],
+ box,
+ )
+
+ traced_stream = wrapper(fake_create, object(), (), 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 _finish_reasons(spans[0]) == ("STOP",)
+ 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, object(), (), 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, object(), (), 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, object(), (), 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 Gemini 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, object(), (), 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, object(), (), 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, object(), (), 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, object(), (), 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 keep the model finish reason."""
+
+ 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, finish_reason="STOP"),
+ ],
+ box,
+ )
+
+ traced_stream = await wrapper(fake_create, object(), (), 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 _finish_reasons(spans[0]) == ("STOP",)
+ 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, object(), (), 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, object(), (), 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, object(), (), 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, object(), (), 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
+ )