Repository navigation
fix(openai): stop stripping cache_control from the caller's own messages and tools - #35602
Conversation
[Medium impact] The PR appears safe to merge with both previous findings addressed SummaryThe PR keeps the caller's cache hints when preparing requests for providers that cannot read them. Claude requests keep the newest four message hints under The latest changes address both previous findings. No new actionable issue was found
Reviews (8) · Last reviewed commit: "fix(vertex_ai): put every system message..." · Reviewed by Greptile |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing |
|
the Responses API sibling still has this exact bug: |
4c01e47 to
ff6350a
Compare
|
Pushed a fixup for the |
|
Second lint fixup. The type-discipline budget flagged Checked against the unmodified file rather than in isolation: |
ff6350a to
f4614c1
Compare
|
Good catch, and you are right. Done in 77a00a9. I reproduced it before touching anything rather than going off the docstring. Same fix shape as the chat path, and the docstring line that stated the in-place behaviour as a fact is corrected rather than left to mislead the next reader. I had flagged this one as a deliberate follow-up in the PR body because it is a second API surface and litellm's own checklist asks for isolated scope. Since you would rather have it here, here it is; the two are one defect with one cause, so keeping them together is easy to argue. Regression test added to the mapped Responses test file, next to the existing On the three lint gates, checked against the unmodified file rather than in isolation: |
14e4811 to
5215469
Compare
5215469 to
6b50e7d
Compare
|
Rebased onto Copying every message and tool unconditionally broke two assertions that were added upstream in the meantime, So the strip now goes through
|
d37c498 to
b832e17
Compare
b832e17 to
965b59e
Compare
965b59e to
2750687
Compare
|
Rebased onto main as a single commit. The diff had picked up unrelated staging commits after the retarget; it now contains only this fix. |
6487f55 to
1c84934
Compare
# Conflicts: # tests/unit/llms/mistral/test_mistral_chat_transformation.py
…on OpenAI-compatible Claude routes under drop_params
# Conflicts: # litellm/llms/openai/common_utils.py # tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py # tests/unit/llms/mistral/test_mistral_chat_transformation.py
…compatible request bodies
…model, and Converse tool messages The 4-hint cap now reads drop_params through drops_unsupported_params on chat, Converse, and Anthropic routes, names Claude by base_model on OpenAI-compatible routes, and counts a Converse tool message's hints as the one cache point it sends.
|
bugbot run |
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
# Conflicts: # tests/unit/llms/openrouter/chat/test_openrouter_chat_transformation.py
…rve a top-level cache_control on OpenAI-compatible routes Dropping the oldest hint one at a time re-walked the whole request per hint, which took seconds on long tool histories. The cap now drops each message's oldest hints in the order the provider counts them, so a string assistant turn loses its text hint before its tool call hints. OpenAI-compatible Claude routes also keep room for a request-level or extra_body cache_control under drop_params.
|
bugbot run |
…so generateContent never sends it beside cachedContent
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit fbc2685. Configure here.
mateo-berri
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution!
…#49192 and #35602 in their integration tests (#49468) * fix(caching): stop counting an unrun server tool call mark, and follow #49192 and #35602 in their integration tests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(caching): return the server tool call id instead of a TypeGuard Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(integration): assert the Gemini cache write carries the system prompt Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
When an app marks its request with
cache_controland an OpenAI-compatible model goes first, LiteLLM removed the hints from the app's request, so a Claude fallback sent none and paid full price. This PR removes them on a copy, and since Claude takes at most 4, it keeps the newest 4 underdrop_paramsand otherwise returns Claude's error. A Gemini fallback now gets them too, so its context caching moves the system prompt into the cache, as Gemini requiresDetails
TLDR
Problem this solves:
cache_controlfor OpenAI-compatible providers edited the caller's request, so Claude fallbacks lost cachingHow it solves it:
drop_params, Claude on Anthropic, Vertex AI, Bedrock, Azure AI, OpenRouter, and Databricks keeps the newest 4, and /v1/messages passes them unchangedLogs now keep the caller's hints, opt-in
prompt_cachingrouting pins hinted OpenAI traffic, and a Gemini fallback creates a context cache. Copying 80 messages with 390 KB images takes 0.20 msUser Flow
Before: an app that marks a long document with
cache_controlloses prompt caching whenever its OpenAI-compatible model falls back to Claudegpt-5.5(OpenAI) to fall back to Claude Haiku"model": "gpt-5.5"and a long document marked"cache_control": {"type": "ephemeral"}x-litellm-attempted-fallbacks: 1After: the same fallback writes the cache once and reads it on every repeat
gpt-5.5(OpenAI) to fall back to Claude Haiku"model": "gpt-5.5"and a long document marked"cache_control": {"type": "ephemeral"}x-litellm-attempted-fallbacks: 1Five hints in one message, with a Claude fallback that has drop_params
Before: an app that marks five parts of one message with
cache_controlgets no caching from a Claude fallback that hasdrop_params: truegpt-5.5to fall back to a Claude deployment on Anthropic, OpenRouter, Vertex AI, Bedrock, Azure AI, or Databricks withdrop_params: true"model": "gpt-5.5"and five text parts, each markedcache_controlx-litellm-attempted-fallbacks: 1After: Claude keeps the newest 4 hints, so the cache is written once and read on every repeat
gpt-5.5to fall back to a Claude deployment on Anthropic, OpenRouter, Vertex AI, Bedrock, Azure AI, or Databricks withdrop_params: true"model": "gpt-5.5"and five text parts, each markedcache_controlx-litellm-attempted-fallbacks: 1Without
drop_paramson the Claude deployment, step 4 is instead Claude's "A maximum of 4 blocks with cache_control" error under the first model's status (401 here), where Before gave a 200 with no cachingPre-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
Last updated: fbc2685. QA ran on 092d3bc. Since then only the 4-hint cap and Gemini caching changed, and the other cases reach neither. N1, N2, N4, and R1 to R7 ran again at 929a93c, and G1 and G2 at fbc2685
Live calls, no mocks. Each leg runs
litellm --config config.yaml --port $PORT --num_workers 2, and each first model has a bad key or dead endpointconfig.yaml
Bodies hold 17k to 65k marked tokens. "cached" means written, then read. All raw output
Controls and G1, plus R1 to R7 (five hints through other Claude routes) and P1 to P6 (other first providers)
openai/Before (d18efdf)
O1, /v1/responses
After (092d3bc)
O1 again
Type
🐛 Bug Fix
Caveats (if any)
Low
drop_params, a Claude fallback given over 4 hints now returns Claude's 4-block error instead of a 200 without cachingcache_controlis still dropped, as on the basebase_modelnames Claude, so an inference profile ARN needsbase_modeltool_configcache point is not countedFinal Attestation