From 2ef0d360d47fb6358cc401774df0417354a7ef21 Mon Sep 17 00:00:00 2001 From: paulo037 Date: Mon, 27 Jul 2026 22:24:47 -0300 Subject: [PATCH 1/2] fix(gemini): use collection endpoint for custom context cache --- .../context_caching/vertex_ai_context_caching.py | 3 +++ .../test_vertex_ai_context_caching.py | 13 +++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py b/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py index 0bf3715f798..46a270aabcc 100644 --- a/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py +++ b/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py @@ -75,6 +75,9 @@ class ContextCachingEndpoints(VertexBase): base_url = get_vertex_base_url(vertex_location) url = f"{base_url}/v1beta1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}" + if custom_llm_provider == "gemini" and api_base: + return auth_header, f"{api_base.rstrip('/')}/{endpoint}" + return self._check_custom_proxy( api_base=api_base, custom_llm_provider=custom_llm_provider, diff --git a/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py b/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py index cf75964ddb7..d1ffa6dbcf5 100644 --- a/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py +++ b/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py @@ -1881,12 +1881,9 @@ class TestVertexAIGlobalLocation: "global-aiplatform" not in url ), "URL should not contain 'global-aiplatform' prefix" - def test_gemini_context_caching_with_custom_api_base_passes_model(self): - """Gemini context caching with custom api_base must pass model to _check_custom_proxy. - - Regression test for https://github.com/BerriAI/litellm/issues/23846 - Previously model was hardcoded to None, causing ValueError when api_base was set. - """ + def test_gemini_context_caching_with_custom_api_base_uses_collection_endpoint( + self, + ): caching = ContextCachingEndpoints() auth_header, url = caching._get_token_and_url_context_caching( @@ -1899,8 +1896,8 @@ class TestVertexAIGlobalLocation: model="gemini-1.5-pro", ) - assert "models/gemini-1.5-pro" in url - assert url.startswith("https://my-proxy.example.com/") + assert auth_header == {"x-goog-api-key": "test-key"} + assert url == "https://my-proxy.example.com/cachedContents" def test_gemini_context_caching_without_api_base_ignores_model(self): """Without custom api_base, model param is not needed (default URL is used).""" From a57055325d49a6b726a360ba43e43180548968cd Mon Sep 17 00:00:00 2001 From: paulo037 Date: Mon, 27 Jul 2026 22:36:05 -0300 Subject: [PATCH 2/2] test(gemini): preserve custom cache regression coverage --- .../context_caching/test_vertex_ai_context_caching.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py b/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py index d1ffa6dbcf5..2de471219c3 100644 --- a/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py +++ b/tests/test_litellm/llms/vertex_ai/context_caching/test_vertex_ai_context_caching.py @@ -1881,9 +1881,7 @@ class TestVertexAIGlobalLocation: "global-aiplatform" not in url ), "URL should not contain 'global-aiplatform' prefix" - def test_gemini_context_caching_with_custom_api_base_uses_collection_endpoint( - self, - ): + def test_gemini_custom_cache_uses_collection_endpoint_without_model(self): caching = ContextCachingEndpoints() auth_header, url = caching._get_token_and_url_context_caching( @@ -1893,7 +1891,6 @@ class TestVertexAIGlobalLocation: vertex_project=None, vertex_location=None, vertex_auth_header=None, - model="gemini-1.5-pro", ) assert auth_header == {"x-goog-api-key": "test-key"}