Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(vertex_ai): handle global location in context caching (#16997)

prawaan7cc92d1ced7d
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 70b068b5a4d..26be4d3c2b8 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
@@ -64,11 +64,17 @@ class ContextCachingEndpoints(VertexBase):
elif custom_llm_provider == "vertex_ai":
auth_header = vertex_auth_header
endpoint = "cachedContents"
- url = f"https://{vertex_location}-aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}"
+ if vertex_location == "global":
+ url = f"https://aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}"
+ else:
+ url = f"https://{vertex_location}-aiplatform.googleapis.com/v1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}"
else:
auth_header = vertex_auth_header
endpoint = "cachedContents"
- url = f"https://{vertex_location}-aiplatform.googleapis.com/v1beta1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}"
+ if vertex_location == "global":
+ url = f"https://aiplatform.googleapis.com/v1beta1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}"
+ else:
+ url = f"https://{vertex_location}-aiplatform.googleapis.com/v1beta1/projects/{vertex_project}/locations/{vertex_location}/{endpoint}"
return self._check_custom_proxy(
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 0320092c7a3..88d1b59c5b5 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
@@ -785,3 +785,64 @@ class TestContextCachingEndpoints:
# But original tools should still be available for comparison
assert original_tools == self.sample_tools
+
+
+class TestVertexAIGlobalLocation:
+ """Test global location handling in context caching."""
+
+ def test_global_location_url_construction_v1(self):
+ """Test that global location uses correct URL (no location prefix) for v1 API."""
+ caching = ContextCachingEndpoints()
+
+ # Mock the _check_custom_proxy to return the URL unchanged
+ with patch.object(caching, '_check_custom_proxy', side_effect=lambda **kwargs: (kwargs.get('auth_header'), kwargs.get('url'))):
+ auth_header, url = caching._get_token_and_url_context_caching(
+ gemini_api_key=None,
+ custom_llm_provider="vertex_ai",
+ api_base=None,
+ vertex_project="test-project",
+ vertex_location="global",
+ vertex_auth_header="Bearer test-token",
+ )
+
+ # Assert correct URL format for global
+ expected_url = "https://aiplatform.googleapis.com/v1/projects/test-project/locations/global/cachedContents"
+ assert url == expected_url, f"Expected {expected_url}, got {url}"
+ assert "global-aiplatform" not in url, "URL should not contain 'global-aiplatform' prefix"
+
+ def test_regional_location_url_construction_v1(self):
+ """Test that regional location uses correct URL (with location prefix) for v1 API."""
+ caching = ContextCachingEndpoints()
+
+ with patch.object(caching, '_check_custom_proxy', side_effect=lambda **kwargs: (kwargs.get('auth_header'), kwargs.get('url'))):
+ auth_header, url = caching._get_token_and_url_context_caching(
+ gemini_api_key=None,
+ custom_llm_provider="vertex_ai",
+ api_base=None,
+ vertex_project="test-project",
+ vertex_location="us-central1",
+ vertex_auth_header="Bearer test-token",
+ )
+
+ # Assert correct URL format for regional
+ expected_url = "https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/cachedContents"
+ assert url == expected_url, f"Expected {expected_url}, got {url}"
+
+ def test_global_location_url_construction_v1beta1(self):
+ """Test that global location uses correct URL for v1beta1 API."""
+ caching = ContextCachingEndpoints()
+
+ with patch.object(caching, '_check_custom_proxy', side_effect=lambda **kwargs: (kwargs.get('auth_header'), kwargs.get('url'))):
+ auth_header, url = caching._get_token_and_url_context_caching(
+ gemini_api_key=None,
+ custom_llm_provider="vertex_ai_beta",
+ api_base=None,
+ vertex_project="test-project",
+ vertex_location="global",
+ vertex_auth_header="Bearer test-token",
+ )
+
+ # Assert correct URL format for global with beta API
+ expected_url = "https://aiplatform.googleapis.com/v1beta1/projects/test-project/locations/global/cachedContents"
+ assert url == expected_url, f"Expected {expected_url}, got {url}"
+ assert "global-aiplatform" not in url, "URL should not contain 'global-aiplatform' prefix"
\ No newline at end of file