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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(vertex_ai): build full request path when custom api_base has no path (#32367)
Mateo Wangb8248a21d2ac
diff --git a/litellm/llms/vertex_ai/vertex_llm_base.py b/litellm/llms/vertex_ai/vertex_llm_base.pyindex d57d7bf17df..e177d06bb01 100644--- a/litellm/llms/vertex_ai/vertex_llm_base.py+++ b/litellm/llms/vertex_ai/vertex_llm_base.py@@ -9,6 +9,7 @@ import jsonimport osimport threadingfrom typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Tuple+from urllib.parse import urlparseimport litellmfrom litellm._logging import verbose_logger@@ -615,7 +616,8 @@ class VertexBase:Handles custom api_base for:1. Gemini (Google AI Studio) - constructs /models/{model}:{endpoint}- 2. Vertex AI with standard proxies - constructs {api_base}:{endpoint}+ 2. Vertex AI with standard proxies - constructs {api_base}:{endpoint};+ if api_base has no path (bare host), grafts the default vertex URL path onto it3. Vertex AI with PSC endpoints - constructs full path structure{api_base}/v1/projects/{project}/locations/{location}/endpoints/{model}:{endpoint}(only when use_psc_endpoint_format=True)@@ -660,8 +662,9 @@ class VertexBase:model_for_url,endpoint,)+ elif urlparse(api_base).path in ("", "/"):+ url = api_base.rstrip("/") + urlparse(url).pathelse:- # Fallback to simple format if we don't have all parametersurl = "{}:{}".format(api_base, endpoint)if stream is True:url = url + "?alt=sse"diff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_psc_endpoint_support.py b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_psc_endpoint_support.pyindex 5a6bc871a03..499bbf6ccd4 100644--- a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_psc_endpoint_support.py+++ b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_psc_endpoint_support.py@@ -152,9 +152,9 @@ class TestVertexAIPSCEndpointSupport:assert url == expected_url, f"Expected {expected_url}, but got {url}"def test_standard_proxy_with_googleapis(self):- """Test that standard proxies with googleapis.com in URL use simple format"""+ """Test that standard proxies with a path in the URL use simple format"""vertex_base = VertexBase()- proxy_api_base = "https://my-proxy.googleapis.com"+ proxy_api_base = "https://my-proxy.googleapis.com/vertex-proxy"endpoint_id = "gemini-pro" # Not numericproject_id = "test-project"location = "us-central1"diff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py b/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.pyindex 2cf97081806..ffe935a52af 100644--- a/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py+++ b/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py@@ -774,7 +774,7 @@ class TestVertexBase:"https://aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-pro:generateContent","gemini-pro","Bearer token123",- "https://custom-vertex-api.com:generateContent",+ "https://custom-vertex-api.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-pro:generateContent",),# Test case 4: No API base provided (should return original values)(@@ -930,6 +930,101 @@ class TestVertexBase:result_url_no_streaming == expected_no_streaming_url), f"Expected {expected_no_streaming_url}, got {result_url_no_streaming}"+ def test_check_custom_proxy_vertex_bare_host_api_base_grafts_default_path(self):+ vertex_base = VertexBase()++ result_auth_header, result_url = vertex_base._check_custom_proxy(+ api_base="https://aiplatform.googleapis.com",+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint="embedContent",+ stream=None,+ auth_header="Bearer token123",+ url="https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-embedding-2:embedContent",+ model="gemini-embedding-2",+ )++ assert result_auth_header == "Bearer token123"+ assert (+ result_url+ == "https://aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-embedding-2:embedContent"+ )++ def test_check_custom_proxy_vertex_bare_host_api_base_with_trailing_slash(self):+ vertex_base = VertexBase()++ _, result_url = vertex_base._check_custom_proxy(+ api_base="https://internal-gateway.example.com/",+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint="embedContent",+ stream=None,+ auth_header="Bearer token123",+ url="https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-embedding-2:embedContent",+ model="gemini-embedding-2",+ )++ assert (+ result_url+ == "https://internal-gateway.example.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-embedding-2:embedContent"+ )++ def test_check_custom_proxy_vertex_api_base_with_path_keeps_endpoint_append(self):+ vertex_base = VertexBase()+ gateway_api_base = "https://gateway.ai.cloudflare.com/v1/account-id/my-gateway/google-vertex-ai/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-embedding-2"++ _, result_url = vertex_base._check_custom_proxy(+ api_base=gateway_api_base,+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint="embedContent",+ stream=None,+ auth_header="Bearer token123",+ url="https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-embedding-2:embedContent",+ model="gemini-embedding-2",+ )++ assert result_url == f"{gateway_api_base}:embedContent"++ def test_check_custom_proxy_vertex_bare_host_streaming_keeps_single_alt_sse(self):+ vertex_base = VertexBase()++ _, result_url = vertex_base._check_custom_proxy(+ api_base="https://internal-gateway.example.com",+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint="streamGenerateContent",+ stream=True,+ auth_header="Bearer token123",+ url="https://us-central1-aiplatform.googleapis.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-2.5-pro:streamGenerateContent?alt=sse",+ model="gemini-2.5-pro",+ )++ assert (+ result_url+ == "https://internal-gateway.example.com/v1/projects/test-project/locations/us-central1/publishers/google/models/gemini-2.5-pro:streamGenerateContent?alt=sse"+ )++ def test_check_custom_proxy_psc_endpoint_format_unaffected_by_bare_host(self):+ vertex_base = VertexBase()++ _, result_url = vertex_base._check_custom_proxy(+ api_base="https://10.96.32.8",+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint="predict",+ stream=None,+ auth_header="Bearer token123",+ url="",+ model="1234567890",+ vertex_project="test-project",+ vertex_location="us-central1",+ vertex_api_version="v1",+ use_psc_endpoint_format=True,+ )++ assert result_url == "https://10.96.32.8/v1/projects/test-project/locations/us-central1/endpoints/1234567890:predict"+@pytest.mark.parametrize("api_base, custom_llm_provider, gemini_api_key, endpoint, stream, auth_header, url, model, expected_auth_header, expected_url",[