Skip to content

fix(litellm): make the span current while streaming calls run - #4042

Merged
caroger merged 3 commits into
Arize-ai:mainfrom
PabloPardoGarcia:fix/litellm-stream-span-context
Oct 20, 2026
Merged

caroger merged 3 commits into
Arize-ai:mainfrom
PabloPardoGarcia:fix/litellm-stream-span-context

Conversation

@PabloPardoGarcia

@PabloPardoGarcia PabloPardoGarcia commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

fixes #4044

Problem

On streaming calls (stream=True), the completion, acompletion, responses and aresponses wrappers create their span with start_span and call the wrapped litellm function without making that span current. The non-streaming branches use start_as_current_span, and the anthropic messages paths already wrap the call in trace_api.use_span(...).

LiteLLM calls the provider SDK inside that function. So when another instrumentor traces the provider call (for example openinference-instrumentation-openai on the OpenAI client LiteLLM uses), its span sees no current span and starts a new trace, instead of nesting under the litellm span as it does for non-streaming calls.

Observed with openinference-instrumentation-litellm 0.1.50 and openinference-instrumentation-openai 0.1.65 on litellm.completion(..., stream=True): the ChatCompletion span is a root span in a separate trace, while the same call without stream=True nests under completion.

Fix

In the four stream branches, run the wrapped call under trace_api.use_span(span, end_on_exit=False, record_exception=False, set_status_on_exception=False), the same form the anthropic messages paths use. The span is still ended by the stream finalizers when the stream is consumed, and it is not left current after the call returns.

Tests

tests/test_stream_span_context.py: the wrapped function is replaced by one that opens a span of its own (standing in for a provider instrumentor) and then calls the original. For each of completion, acompletion, responses and aresponses with stream=True, that span must be a child of the litellm span, in the same trace. A fifth test checks the litellm span does not stay current in the caller's context after the call.

Without the fix, the four nesting tests fail. The full package suite passes (134 tests), as do ruff format --diff, ruff check and mypy at the versions tox pins.

PabloPardoGarcia and others added 2 commits October 19, 2026 22:05
The stream branches of completion, acompletion, responses and aresponses created their span with start_span and called the wrapped litellm function without making it current. Spans created by other instrumentors for the provider call (the OpenAI client, for example) then started a new trace instead of nesting under the litellm span. The wrapped call now runs under trace_api.use_span(span, end_on_exit=False), as the anthropic messages paths already do; the span still ends when the stream is consumed.
…an error

The Responses stream finalizers caught only Exception, so a task cancelled or a
KeyboardInterrupt raised while reading a stream ended the span UNSET with no
exception event. They now catch BaseException and record it, except
GeneratorExit, which means the caller closed the stream early and stays UNSET.
@caroger
caroger force-pushed the fix/litellm-stream-span-context branch from c3b3bb5 to 143abb4 Compare October 20, 2026 05:15
@caroger

caroger commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Maintainer's note

I pushed these changes to this branch.

  • Rebased onto main (force-push from c3b3bb54). fix(litellm): end spans on failed calls, unread streams, and non-completed responses #3948 merged and wrapped the same four self.original_litellm_funcs[...] stream calls in a try/except that ends the span with ERROR when the call raises, so this PR conflicted with main. Each call site now runs your trace_api.use_span(...) block inside fix(litellm): end spans on failed calls, unread streams, and non-completed responses #3948's try/except BaseException. The call runs with the litellm span current, and a failing call still ends the span with ERROR. The resolution is part of your rebased commit, now 730e1f25.
  • 143abb46 fix(litellm): record a Responses stream cancelled partway through as an error. The Responses stream finalizers caught only Exception, so a cancelled task or a KeyboardInterrupt while reading a Responses stream ended the span UNSET with no exception event. They now record any BaseException as ERROR, except GeneratorExit, which means the caller closed the stream early and stays UNSET. Three tests cover a cancelled async stream, an interrupted sync stream, and an early close.

Please pull the rebased branch before pushing again.

…event on failed calls

Two mutations survived the suite: dropping the GeneratorExit carve-out from the
sync Responses finalizer, and dropping use_span's record_exception=False and
set_status_on_exception=False, which records the exception twice. A sync
early-close test and an exception-event count on the failed-call test now
catch both.
@caroger

caroger commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Maintainer's note

I pushed one more commit to this branch, as a fast-forward with no rebase.

  • 8a7b8a03 test(litellm): pin sync Responses early close and a single exception event on failed calls. A review found two changes the suite would not catch. One was removing the GeneratorExit carve-out from the sync Responses finalizer, which would mark every early close as ERROR. The other was dropping record_exception=False and set_status_on_exception=False from your use_span calls, which would record a failed call's exception twice. A sync early-close test and an exception-event count on the failed-call test now fail on both.

@caroger
caroger enabled auto-merge (squash) October 20, 2026 05:39
@caroger
caroger merged commit a5dd653 into Arize-ai:main Oct 20, 2026
21 checks passed
@mikeldking mikeldking mentioned this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[bug] LiteLLM instrumentation: streaming calls don't make the span current, so child spans start a new trace

2 participants