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(azure_ai_inference): end streaming spans on exhaustion and early close (#1594)

Dmitry Voropaev6767f087582f
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
index 00feb5d1..b4768a7b 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
@@ -39,7 +39,7 @@ def async_complete(
Generates a telemetry wrapper for GenAI function call
"""
- class TracedAsyncStream:
+ class TracedAsyncStream: # pylint: disable=too-many-instance-attributes
"""
Wrapper for streaming responses to collect telemetry.
"""
@@ -80,36 +80,122 @@ def async_complete(
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__()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
- await self.__wrapped__.__aexit__(exc_type, exc_val, exc_tb)
- process_streaming_chat_response(
- self,
- pricing_info,
- environment,
- application_name,
- metrics,
- capture_message_content,
- disable_metrics,
- version,
- event_provider=self._event_provider,
- )
+ try:
+ await self.__wrapped__.__aexit__(exc_type, exc_val, exc_tb)
+ finally:
+ # Exhaustion already ended the span. Leave that export alone,
+ # including when the body raises after the stream is consumed.
+ # Do not return from this finally: a return would swallow an
+ # exception raised by the wrapped __aexit__.
+ if not self._streaming_response_processed:
+ stream_error = exc_val if exc_type is not None else None
+ # A break never hits StopAsyncIteration, so a clean exit is a cancel.
+ self._finalize_streaming_span(False, stream_error)
def __aiter__(self):
return self
async def __anext__(self):
- chunk = await self.__wrapped__.__anext__()
- process_chunk(self, chunk)
+ try:
+ chunk = await self.__wrapped__.__anext__()
+ except StopAsyncIteration:
+ self._finalize_streaming_span(True)
+ raise
+ except Exception as exc:
+ await self._abort_stream(exc, close_wrapped=True)
+ raise
+ try:
+ process_chunk(self, chunk)
+ except Exception as exc:
+ # The chunk itself failed instrumentation. Leave the SDK
+ # stream open so a surrounding ``async with`` can still close it.
+ await self._abort_stream(exc, close_wrapped=False)
+ raise
return chunk
def __getattr__(self, name):
return getattr(self.__wrapped__, name)
+ async def _close_wrapped_quietly(self):
+ """Close the SDK stream without masking the error that aborted it."""
+ try:
+ await self.__wrapped__.aclose()
+ except Exception as exc:
+ logger.warning("Failed to close Azure AI Inference stream: %s", exc)
+
+ async def _abort_stream(self, exc, close_wrapped):
+ """End the span for a stream or chunk failure."""
+ if close_wrapped:
+ await self._close_wrapped_quietly()
+ self._finalize_streaming_span(False, exc)
+
+ def _finalize_streaming_span(self, ended_cleanly, stream_error=None):
+ """Complete and end the span exactly once.
+
+ ``ended_cleanly`` is True only for StopAsyncIteration. Any other
+ exit with no finish reason is ``incomplete``, or ``error`` when
+ ``stream_error`` is set. Partial text and usage are written first.
+ A failed stream does not set OK, because the SDK ignores a later
+ ERROR. The error status is applied while the span is still recording.
+ """
+ if self._streaming_response_processed:
+ return
+ self._streaming_response_processed = True
+ if stream_error is not None:
+ # common_chat_logic reads this for the metrics error.type.
+ self._stream_error = stream_error
+ if 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,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=self._event_provider,
+ )
+ except Exception as exc:
+ if stream_error is None:
+ stream_error = exc
+ self._stream_error = exc
+ raise
+ finally:
+ if stream_error is not None:
+ handle_exception(self._span, stream_error)
+ except Exception as exc:
+ if self._span.is_recording():
+ handle_exception(self._span, exc)
+ self._span.end()
+
+ async def aclose(self):
+ """Close the wrapped stream and finalize the span if not ended.
+
+ azure-ai-inference's AsyncStreamingChatCompletions exposes
+ ``aclose()``, not ``close()``. A wrapped aclose that raises still
+ exports the span. That close error is not a model failure, so it
+ is not stored as one.
+ """
+ try:
+ await self.__wrapped__.aclose()
+ finally:
+ self._finalize_streaming_span(False)
+
async def wrapper(wrapped, instance, args, kwargs):
"""
Wraps the GenAI function call.
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
index 7d2841de..5c9da4c4 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
@@ -39,7 +39,7 @@ def complete(
Generates a telemetry wrapper for GenAI function call
"""
- class TracedSyncStream:
+ class TracedSyncStream: # pylint: disable=too-many-instance-attributes
"""
Wrapper for streaming responses to collect telemetry.
"""
@@ -80,36 +80,120 @@ def complete(
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__()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
- self.__wrapped__.__exit__(exc_type, exc_val, exc_tb)
- process_streaming_chat_response(
- self,
- pricing_info,
- environment,
- application_name,
- metrics,
- capture_message_content,
- disable_metrics,
- version,
- event_provider=self._event_provider,
- )
+ try:
+ self.__wrapped__.__exit__(exc_type, exc_val, exc_tb)
+ finally:
+ # Exhaustion already ended the span. Leave that export alone,
+ # including when the body raises after the stream is consumed.
+ # Do not return from this finally: a return would swallow an
+ # exception raised by the wrapped __exit__.
+ if not self._streaming_response_processed:
+ stream_error = exc_val if exc_type is not None else None
+ # A break never hits StopIteration, so a clean exit is a cancel.
+ self._finalize_streaming_span(False, stream_error)
def __iter__(self):
return self
def __next__(self):
- chunk = next(self.__wrapped__)
- process_chunk(self, chunk)
+ try:
+ chunk = next(self.__wrapped__)
+ except StopIteration:
+ self._finalize_streaming_span(True)
+ raise
+ except Exception as exc:
+ self._abort_stream(exc, close_wrapped=True)
+ raise
+ try:
+ process_chunk(self, chunk)
+ except Exception as exc:
+ # The chunk itself failed instrumentation. Leave the SDK
+ # stream open so a surrounding ``with`` can still close it.
+ self._abort_stream(exc, close_wrapped=False)
+ raise
return chunk
def __getattr__(self, name):
return getattr(self.__wrapped__, name)
+ def _close_wrapped_quietly(self):
+ """Close the SDK stream without masking the error that aborted it."""
+ try:
+ self.__wrapped__.close()
+ except Exception as exc:
+ logger.warning("Failed to close Azure AI Inference stream: %s", exc)
+
+ def _abort_stream(self, exc, close_wrapped):
+ """End the span for a stream or chunk failure."""
+ if close_wrapped:
+ self._close_wrapped_quietly()
+ self._finalize_streaming_span(False, exc)
+
+ def _finalize_streaming_span(self, ended_cleanly, stream_error=None):
+ """Complete and end the span exactly once.
+
+ ``ended_cleanly`` is True only for StopIteration. Any other exit
+ with no finish reason is ``incomplete``, or ``error`` when
+ ``stream_error`` is set. Partial text and usage are written first.
+ A failed stream does not set OK, because the SDK ignores a later
+ ERROR. The error status is applied while the span is still recording.
+ """
+ if self._streaming_response_processed:
+ return
+ self._streaming_response_processed = True
+ if stream_error is not None:
+ # common_chat_logic reads this for the metrics error.type.
+ self._stream_error = stream_error
+ if 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,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=self._event_provider,
+ )
+ except Exception as exc:
+ if stream_error is None:
+ stream_error = exc
+ self._stream_error = exc
+ raise
+ finally:
+ if stream_error is not None:
+ handle_exception(self._span, stream_error)
+ except Exception as exc:
+ if self._span.is_recording():
+ handle_exception(self._span, exc)
+ self._span.end()
+
+ def close(self):
+ """Close the wrapped stream and finalize the span if not ended.
+
+ A wrapped close that raises still exports the span. That close
+ error is not a model failure, so it is not stored as one.
+ """
+ try:
+ self.__wrapped__.close()
+ finally:
+ self._finalize_streaming_span(False)
+
def wrapper(wrapped, instance, args, kwargs):
"""
Wraps the GenAI function call.
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
index 34449491..f0f045c3 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
@@ -364,6 +364,16 @@ def process_chunk(scope, chunk):
scope._ttft = calculate_ttft(scope._timestamps, scope._start_time)
chunked = response_as_dict(chunk)
+ # Identity is on every StreamingChatCompletionsUpdate, including deltas
+ # that have not carried usage yet. Non-streaming copies these in
+ # process_chat_response; streaming has to copy them here or exported
+ # spans and metrics keep an empty gen_ai.response.model.
+ response_id = chunked.get("id")
+ if response_id:
+ scope._response_id = response_id
+ response_model = chunked.get("model")
+ if response_model:
+ scope._response_model = response_model
# Collect message IDs and aggregated response from events
choices = chunked.get("choices", [])
@@ -699,7 +709,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: set_status(ERROR) after OK is ignored.
+ # Leave the status unset when the stream already failed so the caller
+ # can record ERROR while the span is still recording.
+ if getattr(scope, "_stream_error", None) is None:
+ scope._span.set_status(Status(StatusCode.OK))
# Metrics
if not disable_metrics:
@@ -709,6 +723,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,
@@ -726,6 +742,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_azure_ai_inference_stream_span_lifecycle.py b/sdk/python/tests/test_azure_ai_inference_stream_span_lifecycle.py
new file mode 100644
index 00000000..27d71fdf
--- /dev/null
+++ b/sdk/python/tests/test_azure_ai_inference_stream_span_lifecycle.py
@@ -0,0 +1,553 @@
+# pylint: disable=protected-access, duplicate-code, missing-function-docstring
+"""Regression tests: the Azure AI Inference streaming wrappers must end their
+span exactly once on exhaustion, context-manager exit and explicit close.
+
+`TracedSyncStream`/`TracedAsyncStream` never ended the span they were handed.
+`__next__`/`__anext__` had no `StopIteration`/`StopAsyncIteration` handler,
+`__exit__`/`__aexit__` populated the span attributes but never ended it, and
+there was no `close()`/`aclose()`. So no span was exported for any streamed
+completion: not for a fully consumed stream (with or without a `with` block),
+not for an early `break`, and not for an explicit `close()`/`aclose()` — the
+whole call (span, cost, tokens) was lost. This is the Azure AI Inference
+instance of the streaming-span lifecycle fixed for Groq in #1516 and Mistral
+in #1517.
+
+These tests drive the real `complete`/`async_complete` wrapper factories with
+a synthetic stream shaped like azure-ai-inference's `StreamingChatCompletions`
+and `AsyncStreamingChatCompletions`, and assert exactly one span is exported
+on exhaustion, context-manager exit (clean or with an exception), explicit
+`close()`/`aclose()`, and a failure raised by the iterator itself. Cancels
+export finish reason ``incomplete``. Failures keep the partial text, set
+finish reason ``error``, and record metrics with ``error.type``. Chunk
+``id`` and ``model`` are copied onto the span.
+"""
+
+import pytest
+from opentelemetry.trace import StatusCode
+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 openlit._config import OpenlitConfig
+from openlit.instrumentation.azure_ai_inference import azure_ai_inference as sync_mod
+from openlit.instrumentation.azure_ai_inference import (
+ async_azure_ai_inference as async_mod,
+)
+from openlit.instrumentation.azure_ai_inference import utils as azure_utils
+from openlit.semcov import SemanticConvention
+
+REQUEST_KWARGS = {
+ "model": "gpt-4o",
+ "stream": True,
+ "messages": [{"role": "user", "content": "Monitor LLM Applications"}],
+}
+
+# Two Azure-shaped updates: a content delta, then a final update with usage.
+CHUNKS = [
+ {
+ "id": "chatcmpl-1",
+ "model": "gpt-4o",
+ "choices": [{"index": 0, "delta": {"content": "partial"}, "finish_reason": None}],
+ },
+ {
+ "id": "chatcmpl-1",
+ "model": "gpt-4o",
+ "choices": [{"index": 0, "delta": {"content": " answer"}, "finish_reason": "stop"}],
+ "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
+ },
+]
+
+
+def _tracer_with_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, disable_metrics=True):
+ mod = async_mod if is_async else sync_mod
+ make = mod.async_complete if is_async else mod.complete
+ return make(
+ version="test",
+ environment="test",
+ application_name="test",
+ tracer=tracer,
+ pricing_info={},
+ capture_message_content=True,
+ metrics=None,
+ disable_metrics=disable_metrics,
+ event_provider=None,
+ )
+
+
+class FakeSyncStream:
+ """Sync stream shaped like azure-ai-inference's StreamingChatCompletions."""
+
+ def __init__(self, raise_on_close=False, raise_after=None):
+ self._it = iter(CHUNKS)
+ self._raise_on_close = raise_on_close
+ self._raise_after = raise_after
+ self._seen = 0
+ self.closed = False
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *exc):
+ self.close()
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if self._raise_after is not None and self._seen >= self._raise_after:
+ raise RuntimeError("stream failed")
+ self._seen += 1
+ return next(self._it)
+
+ def close(self):
+ self.closed = True
+ if self._raise_on_close:
+ raise RuntimeError("wrapped close failed")
+
+
+class FakeAsyncStream:
+ """Async stream shaped like azure-ai-inference's AsyncStreamingChatCompletions."""
+
+ def __init__(self, raise_on_close=False, raise_after=None):
+ self._it = iter(CHUNKS)
+ self._raise_on_close = raise_on_close
+ self._raise_after = raise_after
+ self._seen = 0
+ self.closed = False
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *exc):
+ await self.aclose()
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ if self._raise_after is not None and self._seen >= self._raise_after:
+ raise RuntimeError("stream failed")
+ self._seen += 1
+ try:
+ return next(self._it)
+ except StopIteration:
+ raise StopAsyncIteration from None
+
+ async def aclose(self):
+ self.closed = True
+ if self._raise_on_close:
+ raise RuntimeError("wrapped close failed")
+
+
+def _acreate(raw):
+ async def create(*_a, **_k):
+ """Async stand-in for azure.ai.inference.aio ChatCompletionsClient.complete."""
+ return raw
+
+ return create
+
+
+def _count_calls(monkeypatch, mod, name):
+ calls = []
+ original = getattr(mod, name)
+
+ def counting(*args, **kwargs):
+ calls.append(1)
+ return original(*args, **kwargs)
+
+ monkeypatch.setattr(mod, name, counting)
+ return calls
+
+
+def _assert_one_span_with_tokens(exporter, *, input_tokens, output_tokens, finish_reason):
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1, "expected exactly one exported span"
+ span = spans[0]
+ assert span.status.status_code == StatusCode.OK
+ attrs = span.attributes
+ assert attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == input_tokens
+ assert attrs[SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS] == output_tokens
+ assert tuple(attrs[SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON]) == (finish_reason,)
+ assert attrs[SemanticConvention.GEN_AI_RESPONSE_ID] == "chatcmpl-1"
+ assert attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL] == "gpt-4o"
+ assert "partial" in attrs[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]
+
+
+def _assert_one_error_span(exporter):
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1, "expected exactly one exported span"
+ span = spans[0]
+ assert span.status.status_code == StatusCode.ERROR
+ attrs = span.attributes
+ assert attrs[SemanticConvention.ERROR_TYPE] == "RuntimeError"
+ assert tuple(attrs[SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON]) == ("error",)
+ assert attrs[SemanticConvention.GEN_AI_RESPONSE_ID] == "chatcmpl-1"
+ assert attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL] == "gpt-4o"
+ assert "partial" in attrs[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]
+ assert attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 0
+
+
+def _spy_metrics(monkeypatch):
+ recorded = []
+
+ def spy(*_args, **kwargs):
+ recorded.append(kwargs)
+
+ monkeypatch.setattr(azure_utils, "record_completion_metrics", spy)
+ return recorded
+
+
+def test_sync_full_consumption_without_with_exports_exactly_one_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ stream = wrapper(lambda *a, **k: FakeSyncStream(), None, (), REQUEST_KWARGS)
+ for _ in stream:
+ pass
+
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+
+
+def test_sync_full_consumption_inside_with_exports_exactly_one_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ stream = wrapper(lambda *a, **k: FakeSyncStream(), None, (), REQUEST_KWARGS)
+ with stream as s:
+ for _ in s:
+ pass
+
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+
+
+def test_sync_early_break_inside_with_ends_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ stream = wrapper(lambda *a, **k: FakeSyncStream(), None, (), REQUEST_KWARGS)
+ with stream as s:
+ for _ in s:
+ break # consume one update, then leave the block early
+
+ # The final usage-bearing update was not consumed, so an early exit must
+ # export the span without inventing token counts.
+ _assert_one_span_with_tokens(
+ exporter, input_tokens=0, output_tokens=0, finish_reason="incomplete"
+ )
+
+
+def test_sync_close_finalizes_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ raw = FakeSyncStream()
+ stream = wrapper(lambda *a, **k: raw, None, (), REQUEST_KWARGS)
+ next(stream)
+ stream.close()
+
+ assert raw.closed
+ _assert_one_span_with_tokens(
+ exporter, input_tokens=0, output_tokens=0, finish_reason="incomplete"
+ )
+
+
+def test_sync_close_finalizes_span_when_wrapped_close_raises():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ raw = FakeSyncStream(raise_on_close=True)
+ stream = wrapper(lambda *a, **k: raw, None, (), REQUEST_KWARGS)
+ next(stream)
+ with pytest.raises(RuntimeError, match="wrapped close failed"):
+ stream.close()
+
+ _assert_one_span_with_tokens(
+ exporter, input_tokens=0, output_tokens=0, finish_reason="incomplete"
+ )
+
+
+def test_sync_exception_inside_with_ends_error_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ stream = wrapper(lambda *a, **k: FakeSyncStream(), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="stream failed"):
+ with stream as s:
+ next(s)
+ raise RuntimeError("stream failed")
+
+ _assert_one_error_span(exporter)
+
+
+def test_sync_exception_after_exhaustion_keeps_finalized_span(monkeypatch):
+ error_calls = _count_calls(monkeypatch, sync_mod, "handle_exception")
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ stream = wrapper(lambda *a, **k: FakeSyncStream(), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="caller failed"):
+ with stream as s:
+ for _ in s:
+ pass
+ raise RuntimeError("caller failed")
+
+ # The span already ended on exhaustion; the caller's later error must not
+ # be written onto it. OTel silently drops writes to an ended span, so the
+ # exported span cannot show this: count the calls instead.
+ assert not error_calls, "the finalized span must not take the later error"
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+ assert exporter.get_finished_spans()[0].status.status_code == StatusCode.OK
+
+
+def test_sync_finalizes_once_across_exhaustion_exit_and_close(monkeypatch):
+ calls = _count_calls(monkeypatch, sync_mod, "process_streaming_chat_response")
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ stream = wrapper(lambda *a, **k: FakeSyncStream(), None, (), REQUEST_KWARGS)
+ with stream as s:
+ for _ in s:
+ pass
+ stream.close()
+
+ assert len(calls) == 1, "span telemetry must be processed exactly once"
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+
+
+def test_sync_iterator_error_without_with_exports_error_span(monkeypatch):
+ recorded = _spy_metrics(monkeypatch)
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False, disable_metrics=False)
+
+ raw = FakeSyncStream(raise_after=1)
+ stream = wrapper(lambda *a, **k: raw, None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="stream failed"):
+ for _ in stream:
+ pass
+
+ assert raw.closed
+ _assert_one_error_span(exporter)
+ assert len(recorded) == 1
+ assert recorded[0]["error_type"] == "RuntimeError"
+
+
+def test_sync_iterator_error_exports_span_when_wrapped_close_raises():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ raw = FakeSyncStream(raise_on_close=True, raise_after=1)
+ stream = wrapper(lambda *a, **k: raw, None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="stream failed"):
+ for _ in stream:
+ pass
+
+ assert raw.closed
+ _assert_one_error_span(exporter)
+
+
+def test_sync_process_chunk_error_exports_span_without_closing(monkeypatch):
+ def boom(*_args, **_kwargs):
+ raise RuntimeError("chunk failed")
+
+ monkeypatch.setattr(sync_mod, "process_chunk", boom)
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=False)
+
+ raw = FakeSyncStream()
+ stream = wrapper(lambda *a, **k: raw, None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="chunk failed"):
+ next(stream)
+
+ assert not raw.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"
+ finish = spans[0].attributes[SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON]
+ assert tuple(finish) == ("error",)
+
+
+async def test_async_full_consumption_without_with_exports_exactly_one_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ stream = await wrapper(_acreate(FakeAsyncStream()), None, (), REQUEST_KWARGS)
+ async for _ in stream:
+ pass
+
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+
+
+async def test_async_full_consumption_inside_with_exports_exactly_one_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ stream = await wrapper(_acreate(FakeAsyncStream()), None, (), REQUEST_KWARGS)
+ async with stream as s:
+ async for _ in s:
+ pass
+
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+
+
+async def test_async_early_break_inside_with_ends_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ stream = await wrapper(_acreate(FakeAsyncStream()), None, (), REQUEST_KWARGS)
+ async with stream as s:
+ async for _ in s:
+ break
+
+ _assert_one_span_with_tokens(
+ exporter, input_tokens=0, output_tokens=0, finish_reason="incomplete"
+ )
+
+
+async def test_async_aclose_finalizes_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ raw = FakeAsyncStream()
+ stream = await wrapper(_acreate(raw), None, (), REQUEST_KWARGS)
+ await anext(stream)
+ await stream.aclose()
+
+ assert raw.closed
+ _assert_one_span_with_tokens(
+ exporter, input_tokens=0, output_tokens=0, finish_reason="incomplete"
+ )
+
+
+async def test_async_aclose_finalizes_span_when_wrapped_aclose_raises():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ raw = FakeAsyncStream(raise_on_close=True)
+ stream = await wrapper(_acreate(raw), None, (), REQUEST_KWARGS)
+ await anext(stream)
+ with pytest.raises(RuntimeError, match="wrapped close failed"):
+ await stream.aclose()
+
+ _assert_one_span_with_tokens(
+ exporter, input_tokens=0, output_tokens=0, finish_reason="incomplete"
+ )
+
+
+async def test_async_exception_inside_with_ends_error_span():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ stream = await wrapper(_acreate(FakeAsyncStream()), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="stream failed"):
+ async with stream as s:
+ await anext(s)
+ raise RuntimeError("stream failed")
+
+ _assert_one_error_span(exporter)
+
+
+async def test_async_exception_after_exhaustion_keeps_finalized_span(monkeypatch):
+ error_calls = _count_calls(monkeypatch, async_mod, "handle_exception")
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ stream = await wrapper(_acreate(FakeAsyncStream()), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="caller failed"):
+ async with stream as s:
+ async for _ in s:
+ pass
+ raise RuntimeError("caller failed")
+
+ assert not error_calls, "the finalized span must not take the later error"
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+ assert exporter.get_finished_spans()[0].status.status_code == StatusCode.OK
+
+
+async def test_async_finalizes_once_across_exhaustion_exit_and_aclose(monkeypatch):
+ calls = _count_calls(monkeypatch, async_mod, "process_streaming_chat_response")
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ stream = await wrapper(_acreate(FakeAsyncStream()), None, (), REQUEST_KWARGS)
+ async with stream as s:
+ async for _ in s:
+ pass
+ await stream.aclose()
+
+ assert len(calls) == 1, "span telemetry must be processed exactly once"
+ _assert_one_span_with_tokens(exporter, input_tokens=10, output_tokens=5, finish_reason="stop")
+
+
+async def test_async_iterator_error_without_with_exports_error_span(monkeypatch):
+ recorded = _spy_metrics(monkeypatch)
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True, disable_metrics=False)
+
+ raw = FakeAsyncStream(raise_after=1)
+ stream = await wrapper(_acreate(raw), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="stream failed"):
+ async for _ in stream:
+ pass
+
+ assert raw.closed
+ _assert_one_error_span(exporter)
+ assert len(recorded) == 1
+ assert recorded[0]["error_type"] == "RuntimeError"
+
+
+async def test_async_iterator_error_exports_span_when_wrapped_aclose_raises():
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ raw = FakeAsyncStream(raise_on_close=True, raise_after=1)
+ stream = await wrapper(_acreate(raw), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="stream failed"):
+ async for _ in stream:
+ pass
+
+ assert raw.closed
+ _assert_one_error_span(exporter)
+
+
+async def test_async_process_chunk_error_exports_span_without_closing(monkeypatch):
+ def boom(*_args, **_kwargs):
+ raise RuntimeError("chunk failed")
+
+ monkeypatch.setattr(async_mod, "process_chunk", boom)
+ tracer, exporter = _tracer_with_exporter()
+ wrapper = _factory(tracer, is_async=True)
+
+ raw = FakeAsyncStream()
+ stream = await wrapper(_acreate(raw), None, (), REQUEST_KWARGS)
+ with pytest.raises(RuntimeError, match="chunk failed"):
+ await anext(stream)
+
+ assert not raw.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"
+ finish = spans[0].attributes[SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON]
+ assert tuple(finish) == ("error",)