LiteLLM stream abandoned early leaves its span open
What component(s) are affected?
- Opik Python SDK
Opik version
- Opik version: 2.2.87, SDK source at
ff20285
Describe the problem
A LiteLLM stream never ends its span when the caller stops reading early. A break, a caller timeout, or an early return leaves a span with no end_time, which the UI shows as a call still running. litellm/stream_patchers.py patches CustomStreamWrapper.__next__, and only its StopIteration branch ends the span.
#8518 ends non-exhausted @track generators, but LiteLLM never goes through SyncTrackedGenerator, so this path stays open after it. The OpenAI Stream.__iter__ wrapper ends its span on close through finally.
Reproduction steps and code snippets
Offline, with mock_response and the SDK's fake_backend fixture, LiteLLM 1.103.1. Saved as tests/library_integration/litellm/test_probe_break.py:
import gc
import litellm
import opik
from opik.integrations.litellm import track_completion
def test_probe_consumer_breaks_early(fake_backend):
stream = track_completion()(litellm.completion)(
model="gpt-4o-mini", messages=[{"role": "user", "content": "hi"}],
mock_response="hello world, this is a longer answer", stream=True,
)
for _ in stream:
break
del stream
gc.collect()
opik.flush_tracker()
spans = [s for t in fake_backend.trace_trees for s in t.spans]
print(f"\nPROBE traces={len(fake_backend.trace_trees)} spans={len(spans)} "
f"end_time={[s.end_time for s in spans]}")$ python -m pytest tests/library_integration/litellm/test_probe_break.py -q -s -p no:warnings
PROBE traces=1 spans=1 end_time=[None]
1 passed in 3.48s
# same file with `pass` in place of `break`
PROBE traces=1 spans=1 end_time=[datetime.datetime(2026, 9, 30, 23, 8, 32, 985306, tzinfo=TzInfo(0))]
1 passed in 3.97s#8647 fixes the mid-stream exception path and leaves this one: the probe gives end_time=[None] on its head too.
Error logs or stack trace
None. Nothing raises.
A fix probably should not copy the OpenAI finally as is: #8138 shows its aggregator failing on partial events with an error-level traceback.
@aadereiko, you are reviewing the close path in #8518. Should LiteLLM follow the same decision, partial output recorded and the span ended on close or GC?
Source: comet-ml/opik