Repository navigation
fix(proxy): keep extra chunk fields on the streaming fast path - #44867
Aasif-Simpplr wants to merge 2 commits into
Conversation
_fast_serialize_simple_model_response_stream writes a fixed set of fields, so a field set on the chunk outside its schema (model_extra), for example by an async_post_call_streaming_iterator_hook, was silently dropped from plain text chunks. Fall back to the canonical serializer when the chunk carries an extra the fast payload does not write; service_tier is still served on the fast path.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
VANDRANKI
left a comment
There was a problem hiding this comment.
Community review. This does not clear the merge gate. I ran this against the PR head with a local checkout (python 3.13, litellm main plus this PR).
The bug is real. On main, setting an extra attribute on a ModelResponseStream (chunk.gw = {"a": 1}) makes _fast_serialize_simple_model_response_stream return bytes without gw, while model_dump_json(exclude_none=True, exclude_unset=True) keeps it. With the PR the fast path returns None for that chunk and _serialize_streaming_chunk emits "gw":{"a":1}. A service_tier extra still takes the fast path and is written. Choice-level and delta-level attribute assignments are dropped by both serializers, so those do not diverge.
Problem: the new guard bails on far more than hook-added fields. model_extra contains {'citations': None} on chunks coming out of CustomStreamWrapper for OpenAI-style streams, because streaming_handler.py (~line 985) always does setattr(model_response, "citations", getattr(original_chunk, "citations", None)). I ran litellm.completion(model="gpt-4o", mock_response="hello there world", stream=True) and called the fast serializer on each chunk:
- main: every chunk returned bytes (fast path used)
- PR head: every chunk except the last returned
None(the last chunk has an emptymodel_extra)
So the fast path is effectively disabled for normal OpenAI-compatible streaming, which is the case it exists for. Output is still correct, since the slow path drops None values, but this is a throughput regression on the hot path. I did not benchmark it, I only counted which chunks take the fast path.
Suggested change, ignoring None-valued extras because exclude_none=True drops them anyway:
if model_extra and any(k != "service_tier" and v is not None for k, v in model_extra.items()):
return NoneI applied exactly that locally: the mock stream chunks take the fast path again, and the gw chunk still returns None.
The test test_fast_serialize_simple_model_response_stream_with_extra_field_returns_none passes in my repro, but there is no test with citations=None (or another None extra) asserting the fast path is still used. I think that test is what would catch this. I did not run the repo's pytest suite, because its proxy conftest needs prisma, which I did not install. I ran the equivalent calls in a script instead.
The stream wrapper sets citations=None on every OpenAI-style chunk, so the previous guard sent almost every chunk to the slow path. exclude_none drops None extras on the slow path too, so only non-None extras need it.
|
@VANDRANKI thanks, good catch. Confirmed and fixed in 7d33d5b with the change you suggested: the guard now ignores None-valued extras, since Your repro (
Added I'll refresh the live Baseten proof in the description for the new head. |
|
Another real-world case for this fix, from llama.cpp. llama.cpp's server adds a non-standard
Adding Environment: LiteLLM proxy 1.90.0, Open WebUI -> LiteLLM -> llama-swap -> llama.cpp b11819 / Strata. |
|
Closing as superseded by #49398, which removed the streaming fast path so every chunk keeps its extra fields. Thanks for the fix |
TLDR
Problem this solves:
How it solves it:
service_tieris still served on the fast path, as todayUser Flow
Before: a team that tags every streamed chunk with routing metadata from an
async_post_call_streaming_iterator_hooksees the tag vanish on some chunksgateway_metadata) on each chunk it yields"stream": truereasoning_contentinclude the field, but the plain text chunk and thefinish_reason: "stop"chunk come back without itAfter: every chunk carries the field the callback set
"stream": truePre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Real Baseten calls (
baseten/zai-org/GLM-5.3-Flash) through a local proxy. The proxy loads a callback that tags every chunk it yields:Request used on both sides:
Before (10444df)
PYTHONPATH=<checkout>:. python -m litellm.proxy.proxy_cli --config config.yaml --port 4011gateway_metadata, the ones withreasoning_content. The plain text and stop chunks lose it:After (7d33d5b)
PYTHONPATH=<checkout>:. python -m litellm.proxy.proxy_cli --config config.yaml --port 4011gateway_metadata, including the plain text and stop chunks:Chunks whose only extra is a None value (the
citations=Nonethe stream wrapper sets on every OpenAI-style chunk) still take the fast path. Withlitellm.completion(model="gpt-4o", mock_response="hello there world", stream=True)and the fast serializer called on each chunk, cccfe19 used the fast path on 1 of 7 chunks and 7d33d5b uses it on all 7, while a chunk with a real extra still falls backType
🐛 Bug Fix
Caveats (if any)
Low
model_dump_json, slower than the fast path; None-valued extras keep the fast pathFinal Attestation