Repository navigation
fix(litellm): make the span current while streaming calls run - #4042
Merged
caroger merged 3 commits intoOct 20, 2026
Merged
Conversation
2 tasks done
caroger
approved these changes
Oct 20, 2026
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
force-pushed
the
fix/litellm-stream-span-context
branch
from
October 20, 2026 05:15
c3b3bb5 to
143abb4
Compare
Contributor
Maintainer's noteI pushed these changes to this branch.
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.
Contributor
Maintainer's noteI pushed one more commit to this branch, as a fast-forward with no rebase.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #4044
Problem
On streaming calls (
stream=True), thecompletion,acompletion,responsesandaresponseswrappers create their span withstart_spanand call the wrapped litellm function without making that span current. The non-streaming branches usestart_as_current_span, and the anthropic messages paths already wrap the call intrace_api.use_span(...).LiteLLM calls the provider SDK inside that function. So when another instrumentor traces the provider call (for example
openinference-instrumentation-openaion 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-litellm0.1.50 andopeninference-instrumentation-openai0.1.65 onlitellm.completion(..., stream=True): theChatCompletionspan is a root span in a separate trace, while the same call withoutstream=Truenests undercompletion.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 ofcompletion,acompletion,responsesandaresponseswithstream=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 checkandmypyat the versions tox pins.