Repository navigation
fix(caching): keep reasoning, citations, and audio on streamed cache hits - #32096
mateo-berri merged 8 commits into
Conversation
[Medium impact] Refactors how cached streaming responses replay reasoning and metadata. Core streaming behavior changes for all cache hits, but the new sequencing matches live stream order and is heavily tested with both unit and integration coverage SummaryFixes streamed response cache hits to preserve reasoning, citations, audio, and tool calls that were previously dropped or reordered. Refactors replay logic to emit deltas in live-stream order: reasoning first, then word-sliced content, then tool calls, with a final finish frame carrying usage. Major changes:
Reviews (6) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
df9e379 to
33f60b6
Compare
Merging this PR will not alter performance1 warning
Comparing |
Signed-off-by: jesco-absolut <team@srswti.com>
33f60b6 to
0811d06
Compare
Resolve the conflict with main's _first_replay_delta by validating the whole cached message into the first replayed Delta, keeping main's guard that drops a cached provider_specific_fields that is not an object. The replay now carries reasoning_content, thinking_blocks, and annotations like the sync replay does. Extend the regression test to cover annotations on the first replayed chunk.
… stream order A cached answer replayed as a stream now ends with its own empty finish chunk, so a chunk that carries only audio or images keeps its payload and role. Reasoning fields lead on a chunk with no text, and annotations arrive once on the last text slice, matching a live stream's order.
…tream Only the role, reasoning fields and provider fields lead a replayed answer. Tool calls now arrive on their own chunk after the last text slice, and logprobs ride the first text chunk, so a cached answer with reasoning and a tool call keeps a live stream's reasoning, text, tool call order.
… no text A cached answer that thinks and then only calls a tool now replays its reasoning on its own chunk before the tool call, the order a live stream sends, instead of one chunk carrying both.
…h reasoning A cached answer that thinks, sends only whitespace as text, and then calls a tool now replays that whitespace between the reasoning and the tool call, instead of dropping it.
|
bugbot run |
…_cache_reasoning # Conflicts: # litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
|
bugbot run |
| "provider_specific_fields": _cached_provider_specific_fields( | ||
| _CACHED_MESSAGE_PROVIDER_FIELDS.validate_python(message) | ||
| ), | ||
| } |
There was a problem hiding this comment.
Function calls dropped on cache replay
Medium Severity
_first_replay_delta and _replay_deltas now build frames with Delta.model_validate, which skips Delta.__init__. Legacy function_call therefore stays a raw dict. CustomStreamWrapper then reads .arguments on that value, catches the error, and replaces the delta with an empty one, so a streamed cache hit loses the call.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit d6fdf73. Configure here.
There was a problem hiding this comment.
Checked at d6fdf73. Delta.model_validate coerces the dict into FunctionCall, and a cached function_call streams through CustomStreamWrapper intact with finish_reason function_call
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
1 issue from previous review remains unresolved.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit d6fdf73. Configure here.
mateo-berri
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution!


A streamed cache hit drops reasoning, citations, and audio, and sends tool calls before the text. This replays the cached message in a live stream's order
Details
TLDR
Problem this solves:
How it solves it:
User Flow
Before: a cached stream loses fields and order the first stream had
reasoning_effortand gets reasoning and signeddelta.thinking_blocksid, but only the answer textdelta.annotationsanddelta.audioAfter: the cached stream keeps everything the first stream had, reasoning first
reasoning_effortand gets reasoning and signeddelta.thinking_blocksid, with the reasoning and signed thinking blocks before the textdelta.annotations, on the last text chunk, anddelta.audioRelevant issues
Fixes #32068, closed though main still has the bug
Pre-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 moreDelays 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
Live curl runs, the client from #32068, against real Anthropic and OpenAI, one
--num_workers 2proxy per leg (base on 25917, tip on 26917) on one Redis. Each repeat is the cache hit. This gist has every stream, andshapelists a stream's chunksConfig
Before (77064d6)
Chat, Claude reasoning
chat-reasoning-2.sseis a cache hit with the same id and no reasoningChat, web search
Steps
chat-annotations-2.sse. It answers from the cache with one of the first call's ids, and theurl_citationis goneChat, audio answer
Steps
chat-audio-2.ssecomes back from the cache with the same id and no audio at allResponses, Claude reasoning
Steps
-D responses-reasoning-3.headers. Both repeats cost nothing and come back in milliseconds, so they came from the cache, and neither carries a reasoning summaryChat, reasoning and tool call
Steps
chat-tool-2.sseis a cache hit with the same id, no reasoning, and the tool call on the first text chunkResponses, reasoning and tool call
Steps
responses-tool-2.sseandresponses-tool-3.sse. Both come back in milliseconds from the cache, without the reasoning itemChat, tool only
Steps
chat-toolonly-2.sseis a cache hit with the same id, with no reasoning at allSDK sync stream
Steps
litellm.completion(stream=True)with an in-memory cache serves hits through the sync path.sdk.pystreams one prompt twice, and the second call is the hit. With text, the hit sends the tool call on its first chunk. Without text, it sends the reasoning and the tool call togetherChat, blank text and tool
Steps
chat-blank-2.sseis a cache hit with the same id, with the blank text but no reasoningAfter (d6fdf73)
Chat, Claude reasoning
chat-reasoning-2.sseis a cache hit with the same id, with the reasoning and signed blocks firstChat, web search
Steps
chat-annotations-2.sse. It answers from the cache, and its last text chunk carries theurl_citationChat, audio answer
Steps
chat-audio-2.ssecomes back from the cache with the same id, and one chunk carries all of the first call's audio with its transcriptResponses, Claude reasoning
Steps
-D responses-reasoning-3.headers. Both repeats cost nothing and come back in milliseconds, so they came from the cache, and each carries the whole reasoning summary in one deltaChat, reasoning and tool call
Steps
chat-tool-2.sseis a cache hit with the same id, sending the reasoning, then the text, then the tool callResponses, reasoning and tool call
Steps
responses-tool-2.sseandresponses-tool-3.sse. Both come back in milliseconds from the cache, with the reasoning item first like the live answerChat, tool only
Steps
chat-toolonly-2.sseis a cache hit with the same id, sending the reasoning, then the tool callSDK sync stream
Steps
The same
sdk.py. Both hits send the reasoning first and the tool call last, like the first callChat, blank text and tool
Steps
chat-blank-2.sseis a cache hit with the same id, with the reasoning, the blank text, then the callObservations from the run
Type
Bug Fix
Test
Caveats (if any)
Low
Final Attestation
Bugbot summary