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(python-sdk): end sarvam streaming spans on early exit (#1560)

Chen Yufeiyang9a7466ab370f
diff --git a/sdk/python/src/openlit/instrumentation/sarvam/async_sarvam.py b/sdk/python/src/openlit/instrumentation/sarvam/async_sarvam.py
index 211b9d63..3ff821d1 100644
--- a/sdk/python/src/openlit/instrumentation/sarvam/async_sarvam.py
+++ b/sdk/python/src/openlit/instrumentation/sarvam/async_sarvam.py
@@ -75,13 +75,15 @@ def async_chat_completions(
self._cache_read_input_tokens = 0
self._cache_creation_input_tokens = 0
self._event_provider = event_provider
+ self._streaming_response_processed = False
+ self._stream_error = None
async def __aenter__(self):
- await self.__wrapped__.__aenter__()
+ # Sarvam returns an async generator, which has 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
@@ -90,14 +92,49 @@ def async_chat_completions(
"""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 HTTP response.
+ """
+ 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`` 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,
@@ -109,8 +146,22 @@ def async_chat_completions(
version=version,
event_provider=self._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 = exc
raise
async def wrapper(wrapped, instance, args, kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/sarvam/sarvam.py b/sdk/python/src/openlit/instrumentation/sarvam/sarvam.py
index 3159ccdb..60fa4602 100644
--- a/sdk/python/src/openlit/instrumentation/sarvam/sarvam.py
+++ b/sdk/python/src/openlit/instrumentation/sarvam/sarvam.py
@@ -74,13 +74,15 @@ def chat_completions(
self._cache_read_input_tokens = 0
self._cache_creation_input_tokens = 0
self._event_provider = event_provider
+ self._streaming_response_processed = False
+ self._stream_error = None
def __enter__(self):
- self.__wrapped__.__enter__()
+ # Sarvam returns a generator from ``with httpx.stream``, which has 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
@@ -89,14 +91,44 @@ def chat_completions(
"""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 HTTP response.
+ """
+ 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`` 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,
@@ -108,8 +140,22 @@ def chat_completions(
version=version,
event_provider=self._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 = exc
raise
def wrapper(wrapped, instance, args, kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/sarvam/utils.py b/sdk/python/src/openlit/instrumentation/sarvam/utils.py
index c5531f70..297d8720 100644
--- a/sdk/python/src/openlit/instrumentation/sarvam/utils.py
+++ b/sdk/python/src/openlit/instrumentation/sarvam/utils.py
@@ -215,13 +215,14 @@ def process_chunk(scope, chunk):
chunk: Individual chunk from the streaming response.
"""
current_time = time.time()
-
- # Calculate TTFT for the first chunk
- if len(scope._timestamps) == 0:
- scope._ttft = calculate_ttft(scope._start_time, current_time)
-
scope._timestamps.append(current_time)
+ # Calculate TTFT for the first chunk (timestamps first, start time second,
+ # matching calculate_ttft's signature; every other instrumentor calls it
+ # in this order — the reversed call crashed on every chunk).
+ if len(scope._timestamps) == 1:
+ scope._ttft = calculate_ttft(scope._timestamps, scope._start_time)
+
# Extract and accumulate response data from chunk
if hasattr(chunk, "choices") and len(chunk.choices) > 0:
choice = chunk.choices[0]
@@ -503,7 +504,11 @@ def common_chat_logic(
**extra,
)
- 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))
# Span status and metrics
if not disable_metrics:
diff --git a/sdk/python/tests/test_sarvam_stream_span_lifecycle.py b/sdk/python/tests/test_sarvam_stream_span_lifecycle.py
new file mode 100644
index 00000000..4c1c0535
--- /dev/null
+++ b/sdk/python/tests/test_sarvam_stream_span_lifecycle.py
@@ -0,0 +1,468 @@
+"""Tests that an early-exited Sarvam chat stream still ends and exports its span.
+
+``ChatClient.completions(stream=True)`` returns the generator from
+``with httpx_client.stream``. The async method returns an async generator.
+Neither has ``__enter__`` / ``__aenter__``. A bare ``for`` does not call
+``close()`` on this wrapper.
+"""
+
+import asyncio
+import gc
+from types import SimpleNamespace
+
+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.sarvam.async_sarvam import async_chat_completions
+from openlit.instrumentation.sarvam.sarvam import chat_completions
+
+
+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-sarvam-stream-lifecycle"), exporter
+
+
+def _chat_kwargs():
+ return {
+ "model": "sarvam-m",
+ "messages": [{"role": "user", "content": "ping"}],
+ "stream": True,
+ }
+
+
+def _chunk(text, finish_reason=None):
+ return SimpleNamespace(
+ id="chatcmpl-sarvam",
+ model="sarvam-m",
+ choices=[
+ SimpleNamespace(
+ delta=SimpleNamespace(content=text),
+ finish_reason=finish_reason,
+ )
+ ],
+ usage=None,
+ )
+
+
+def _stop_chunk():
+ return _chunk("", finish_reason="stop")
+
+
+def _sync_wrapper(tracer, capture_message_content=False):
+ return chat_completions(
+ 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_chat_completions(
+ 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"])
+
+
+class _ClosingSyncSource:
+ """HTTP stand-in whose ``with`` exits when the generator is closed."""
+
+ def __init__(self, chunks, box, raise_after=None):
+ self._chunks = list(chunks)
+ self._box = box
+ self._raise_after = raise_after
+ self._index = 0
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self._box["closed"] = True
+
+ 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 _sync_generator(chunks, box, raise_after=None):
+ """Same shape as Sarvam's ``with httpx_client.stream: yield``."""
+
+ def _generate():
+ with _ClosingSyncSource(chunks, box, raise_after) as raw:
+ yield from raw
+
+ return _generate()
+
+
+class _ClosingAsyncSource:
+ """Async twin of ``_ClosingSyncSource``."""
+
+ def __init__(self, chunks, box, raise_after=None):
+ self._chunks = list(chunks)
+ self._box = box
+ self._raise_after = raise_after
+ self._index = 0
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc_value, traceback):
+ self._box["closed"] = True
+
+ 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
+
+
+def _async_generator(chunks, box, raise_after=None):
+ """Same shape as Sarvam's ``async with httpx_client.stream: yield``."""
+
+ async def _generate():
+ async with _ClosingAsyncSource(chunks, box, raise_after) as raw:
+ async for chunk in raw:
+ yield chunk
+
+ return _generate()
+
+
+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_stream_closed_via_context_manager_break_still_exports_span():
+ """An early break inside ``with`` must end the span and close the generator."""
+
+ tracer, exporter = _tracer_and_exporter()
+ wrapper = _sync_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator([_chunk("hi"), _chunk("there")], box)
+
+ traced_stream = wrapper(fake_create, object(), [], _chat_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["gen_ai.output.messages"]
+
+
+def test_sync_stream_explicit_close_still_exports_span():
+ """Calling ``.close()`` directly must finalize."""
+
+ tracer, exporter = _tracer_and_exporter()
+ wrapper = _sync_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator([_chunk("hi")], box)
+
+ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())
+ next(traced_stream)
+ traced_stream.close()
+
+ assert box["closed"]
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ 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)
+ box = {"closed": False}
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator([_chunk("hi"), _stop_chunk()], box)
+
+ traced_stream = wrapper(fake_create, object(), [], _chat_kwargs())
+
+ with traced_stream as stream:
+ for _item in stream:
+ pass
+
+ assert box["closed"]
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ assert _finish_reasons(spans[0]) == ("stop",)
+
+
+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 = _sync_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator([_chunk("hi"), _chunk("there")], box)
+
+ def run():
+ traced_stream = wrapper(fake_create, object(), [], _chat_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["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 = _sync_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+ chunks = [_chunk("one"), _chunk("two"), _chunk("three")]
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator(chunks, box)
+
+ response = wrapper(fake_create, object(), [], _chat_kwargs())
+ for item in response:
+ assert item == chunks[0]
+ break
+
+ assert len(exporter.get_finished_spans()) == 0
+ assert box["closed"] is False
+ assert next(response) == chunks[1]
+
+ 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["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 = _sync_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator([_chunk("hi"), _chunk("nope")], box, raise_after=1)
+
+ 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 box["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 a Sarvam failure. The cancel is incomplete."""
+
+ tracer, exporter = _tracer_and_exporter()
+ wrapper = _sync_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ def fake_create(*_args, **_kwargs):
+ return _sync_generator([_chunk("hi"), _chunk("more")], box)
+
+ 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 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_stream_explicit_aclose_still_exports_span():
+ """``aclose`` is the async generator cleanup method and must finalize the span."""
+
+ tracer, exporter = _tracer_and_exporter()
+ wrapper = _async_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ async def fake_create(*_args, **_kwargs):
+ return _async_generator([_chunk("hi")], box)
+
+ traced_stream = await wrapper(fake_create, object(), [], _chat_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",)
+
+
+async def test_async_stream_full_consumption_exports_exactly_one_span():
+ """Async full drain must export one span and keep the real stop reason."""
+
+ tracer, exporter = _tracer_and_exporter()
+ wrapper = _async_wrapper(tracer)
+ box = {"closed": False}
+
+ async def fake_create(*_args, **_kwargs):
+ return _async_generator([_chunk("hi"), _stop_chunk()], box)
+
+ traced_stream = await wrapper(fake_create, object(), [], _chat_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 _finish_reasons(spans[0]) == ("stop",)
+
+
+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 = _async_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ async def fake_create(*_args, **_kwargs):
+ return _async_generator([_chunk("hi"), _chunk("there")], box)
+
+ async def run():
+ traced_stream = await wrapper(fake_create, object(), [], _chat_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["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 = _async_wrapper(tracer, capture_message_content=True)
+ box = {"closed": False}
+
+ async def fake_create(*_args, **_kwargs):
+ return _async_generator([_chunk("hi"), _chunk("nope")], box, raise_after=1)
+
+ 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 box["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",)
+
+
+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)
+ box = {"closed": False}
+
+ async def fake_create(*_args, **_kwargs):
+ return _async_generator([_chunk("hi"), _chunk("more")], box)
+
+ 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 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",)