Skip to content

fix(vertex_ai): fix gemini-4-argon explicit context caching 400 crash (#46470) - #49860

Open
scriptsmay wants to merge 5 commits into
BerriAI:mainfrom
scriptsmay:fix/vertex-explicit-caching-unsupported-model-400
Open

scriptsmay wants to merge 5 commits into
BerriAI:mainfrom
scriptsmay:fix/vertex-explicit-caching-unsupported-model-400

Conversation

@scriptsmay

@scriptsmay scriptsmay commented Nov 3, 2026 •

Copy link
Copy Markdown

Handwritten 1-3 sentence description

<make-me-human-readable>
Prevent HTTP 400 crashes when clients send Anthropic prompt caching headers to Vertex AI models that do not support explicit context caching (e.g. Gemini-4-Argon). The proxy now drops unsupported caching instructions beforehand and safely falls back without dropping messages or tool parameters if Vertex AI rejects explicit cache creation.
</make-me-human-readable>

Details

TLDR

Problem this solves:

  • Clients sending Anthropic cache_control to Gemini-4 models (Argon/Carbon) trigger HTTP 400.
  • Gemini-4 supports implicit prompt caching only, but supports_prompt_caching: true is also used to gate explicit cache_control handling.
  • Explicit cache creation (cachedContents.create) is rejected with HTTP 400 and was not handled gracefully.

How it solves it:

  • Add a separate supports_explicit_prompt_caching field; set it to false on the gemini-4-* (Argon/Carbon) rows while keeping supports_prompt_caching: true for implicit caching and cost tracking.
  • Gate target_consumes_cache_control() and ContextCachingEndpoints / Vertex request body transformation on supports_explicit_prompt_caching.
  • Add defensive HTTP 400 fallback in sync/async check_and_create_cache preserving original messages and tools.

Relevant issues

Fixes #46470

User Flow

Before: a client requests Gemini-4-Argon with prompt caching headers and gets an HTTP 400 crash

  1. The client sends POST /v1/messages with model gemini-4-argon and Anthropic cache_control headers.
  2. The proxy attempts Vertex AI explicit context cache creation (cachedContents.create).
  3. Vertex AI responds with HTTP 400 stating explicit caching is not supported for this model.
  4. The proxy returns an HTTP 400 error to the client instead of completing the request.

After: the same request succeeds transparently without error

  1. The client sends POST /v1/messages with model gemini-4-argon and Anthropic cache_control headers.
  2. The proxy detects explicit caching is unsupported and skips cachedContents.create.
  3. If an upstream 400 occurs on cache creation, the proxy falls back preserving original messages and tools.
  4. The request completes successfully with a 200 response.

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v.
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Unit test execution:

$ uv run pytest tests/unit/llms/vertex_ai/context_caching/test_vertex_ai_explicit_caching_unsupported.py tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py tests/unit/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py tests/unit/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
============================= 553 passed in 10.66s =============================

Linting:

$ uv run ruff check litellm/llms/vertex_ai/context_caching/ litellm/llms/vertex_ai/gemini/ litellm/llms/anthropic/messages/adapters/ tests/unit/llms/vertex_ai/context_caching/
All checks passed!

Type

🐛 Bug Fix

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@CLAassistant

CLAassistant commented Nov 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rejasupotaro

Copy link
Copy Markdown

Thanks for working on this! Two notes from reproducing it with Claude Code → vertex_ai/gemini-4-carbon on LiteLLM 1.106.5 (HTTP 400 does not support explicit context caching):

  1. gemini-4-carbon is affected too. The Carbon rows added in feat(cost-map): add gemini/gemini-4-carbon from the Gemini API pricing page #50931 / feat(cost-map): add vertex_ai/gemini-4-carbon and gemini-4-carbon-cyber-permissive from the Vertex pricing page #50942 also have "supports_prompt_caching": true, so this PR's pre-check won't skip them. Every eligible request will still issue a failing POST /cachedContents before falling back.

  2. Flipping the flag to false may not stick. The catalog sync sets supports_prompt_caching: true for "implicit only" models on purpose (feat(cost-map): add gemini/gemini-4-carbon from the Gemini API pricing page #50931). The same convention is used for OpenAI's automatic caching. A future sync could revert a manual false.

Suggestion: keep supports_prompt_caching for implicit caching. Gate target_consumes_cache_control() / ContextCachingEndpoints on explicit-cache support instead, for example with a separate supports_explicit_prompt_caching: false set on the gemini-4-* rows. Happy to help test.

@scriptsmay
scriptsmay force-pushed the fix/vertex-explicit-caching-unsupported-model-400 branch from 2095cf4 to 11edcf9 Compare November 11, 2026 12:03
@veria-ai

veria-ai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Reviews

@scriptsmay

Copy link
Copy Markdown
Author

Thanks for the review! Adopted your suggestion — added a separate supports_explicit_prompt_caching: false on all gemini-4-* rows (argon + carbon, incl. cyber-permissive variants), keeping supports_prompt_caching: true for implicit caching; target_consumes_cache_control() and ContextCachingEndpoints now gate on the new field.

@tbartik

tbartik commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Hit this in production on v1.108.5 / v1.109.1 with Claude Code routing through auto_router/complexity_router to vertex_ai/gemini-4-carbon on Vertex's eu regional endpoint (400 INVALID_ARGUMENT: Model 'gemini-4-carbon' does not support explicit context caching.).

Read through the diff against current main — four concrete things worth addressing before merge:

  1. gemini-4-hydrogen rows landed after your Nov 11 update (feat(vertex_ai): add vertex_ai/gemini-4-hydrogen from the Vertex pricing page #51432): gemini/gemini-4-hydrogen and vertex_ai/gemini-4-hydrogen are now on main with "supports_prompt_caching": true. Google's Hydrogen model card also specifies implicit caching only (4,096-token minimum) and rejects explicit cachedContents with the same 400, so both rows (and the unit test list) need the explicit-caching opt-out too.

  2. Existing supports_prompt_cache_breakpoint flag & wildcard routing: main already has ProviderSpecificModelInfo.supports_prompt_cache_breakpoint (litellm/types/utils.py, litellm/utils.py, litellm/llms/bedrock/common_utils.py) for this exact split — models that price implicit cache reads (supports_prompt_caching: true) while rejecting explicit cache markers (e.g. moonshotai.kimi-k3*). Reusing supports_prompt_cache_breakpoint: false avoids a second boolean for the same concept. If you keep supports_explicit_prompt_caching separate, add it to _PATTERN_WIDE_CACHE_FLAGS in litellm/litellm_core_utils/pattern_match_deployments.py so wildcard deployments (vertex_ai/*, gemini/*) leave the flag to per-model catalog rows.

  3. Memoize the HTTP 400 fallback per (scope, target.model): In check_and_create_cache / async_check_and_create_cache, check_cache runs GET /cachedContents before POST /cachedContents, and Vertex meters cachedContents.list against a strict per-minute, per-location quota (known_cached_contents.py). Right now, when an unmapped alias or new implicit-only model hits the 400 fallback, nothing records that (scope, target.model) rejected explicit caching — so every turn in an agent loop pays two wasted round-trips (GET + failing POST) and burns list quota. Remembering unsupported models in KnownCachedContents stops repeat attempts after the first 400.

  4. model_info.base_model lookups: In litellm/llms/vertex_ai/gemini/transformation.py (and/or _context_caching_skip_reason), only deployment_model, cache_model, and direct model_info.get("supports_explicit_prompt_caching") are checked. Also checking model_info.get("base_model") via is_explicitly_disabled_factory lets custom/endpoint deployments configured with base_model: vertex_ai/gemini-4-carbon skip cachedContents up front without duplicating the flag.

@codspeed

codspeed Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing scriptsmay:fix/vertex-explicit-caching-unsupported-model-400 (11edcf9) with main (8b8ec4e)

Open in CodSpeed

@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.14286% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/llms/vertex_ai/gemini/transformation.py 87.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…ompt_caching

gemini-4 (argon/carbon) support implicit prompt caching only; explicit
cache_control markers return HTTP 400 "does not support explicit context
caching". Add a supports_explicit_prompt_caching model field and gate the
three explicit-caching code paths on it, keeping supports_prompt_caching
true for implicit caching and cost tracking.
…e_model

Address review feedback on BerriAI#49860 after rebasing onto latest main:
- add supports_explicit_prompt_caching: false to gemini-4-hydrogen rows
- register the flag in _PATTERN_WIDE_CACHE_FLAGS so wildcard deployments
  (vertex_ai/*, gemini/*) defer to per-model catalog rows
- check model_info.base_model via is_explicitly_disabled_factory so custom
  deployments with base_model: vertex_ai/gemini-4-* skip cachedContents
@scriptsmay
scriptsmay force-pushed the fix/vertex-explicit-caching-unsupported-model-400 branch from 11edcf9 to 350e856 Compare November 23, 2026 01:04
@scriptsmay

Copy link
Copy Markdown
Author

Thanks @tbartik — this is a real production reproduction, appreciate the detailed write-up. I've rebased the branch onto current main (was ~1100 commits behind) and addressed three of your four points:

  1. gemini-4-hydrogen opt-out: added supports_explicit_prompt_caching: false to both vertex_ai/gemini-4-hydrogen and gemini/gemini-4-hydrogen rows in both price-table copies, plus test coverage. Good catch — those rows landed in feat(vertex_ai): add vertex_ai/gemini-4-hydrogen from the Vertex pricing page #51432 after my original update.

  2. wildcard deployments: kept the separate supports_explicit_prompt_caching field (and added it to _PATTERN_WIDE_CACHE_FLAGS in pattern_match_deployments.py), so vertex_ai/* / gemini/* wildcard deployments leave the flag to per-model catalog rows. Adopting your supports_prompt_cache_breakpoint flag outright would touch its bedrock usages too, which I'd rather not bundle into this fix — happy to follow up separately if maintainers prefer the consolidation.

  3. base_model lookups: sync_transform_request_body / async_transform_request_body now also check model_info.base_model via is_explicitly_disabled_factory, so custom deployments with base_model: vertex_ai/gemini-4-* skip cachedContents up front.

On #3 (memoizing the 400 fallback): fully agree with the reasoning — the repeated GET + failing POST per turn does burn cachedContents.list quota. I'm deliberately leaving it out of this PR to keep the change surface focused on the 400 crash fix; I'll open a follow-up for it once this lands. Let me know if you'd rather see it here.

Tests: the dedicated unit test file passes (6/6), ruff clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: gemini-4-argon has supports_prompt_caching: true in model map and ContextCachingEndpoints ignores supports_prompt_caching: false

4 participants