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): read the served location from optional_params when pricing proxy calls

mateo-berri8494a4deee8b
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index e97abcf0af6..91a312b4f45 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -375,18 +375,29 @@ def _published_pricing(deployment_model: str | None) -> ModelInfo | None:
def _resolve_vertex_location_for_cost(
custom_llm_provider: str | None,
litellm_params: Mapping[str, object] | None,
+ optional_params: Mapping[str, object] | None,
model: str,
) -> str | None:
"""
The Vertex AI location a request was served from, resolved the same way
dispatch resolves it, so regional deployments price with the
regional-endpoint uplift. None for non-Vertex providers.
+
+ Chat dispatch reads the location from request kwargs, which reach this
+ logging object through optional_params: on the proxy the logging object is
+ created before the router picks a deployment, so the deployment's location
+ never lands in litellm_params.
"""
if custom_llm_provider is None or not custom_llm_provider.startswith("vertex_ai"):
return None
from litellm.llms.vertex_ai.vertex_llm_base import VertexBase
- configured_location: Final = VertexBase.safe_get_vertex_ai_location(litellm_params or MappingProxyType({}))
+ empty: Final[Mapping[str, object]] = MappingProxyType({})
+ configured_location: Final = (
+ VertexBase.explicit_vertex_ai_location(optional_params or empty)
+ or VertexBase.explicit_vertex_ai_location(litellm_params or empty)
+ or VertexBase.safe_get_vertex_ai_location(empty)
+ )
return VertexBase.get_vertex_region(configured_location, model)
@@ -1598,6 +1609,7 @@ class Logging(LiteLLMLoggingBaseClass):
"vertex_location": _resolve_vertex_location_for_cost(
custom_llm_provider=self.model_call_details.get("custom_llm_provider", None),
litellm_params=(self.litellm_params if hasattr(self, "litellm_params") else None),
+ optional_params=self.optional_params,
model=litellm_model_name or self.model,
),
}
diff --git a/litellm/llms/vertex_ai/vertex_llm_base.py b/litellm/llms/vertex_ai/vertex_llm_base.py
index 0b3c003a60a..75098515deb 100644
--- a/litellm/llms/vertex_ai/vertex_llm_base.py
+++ b/litellm/llms/vertex_ai/vertex_llm_base.py
@@ -1192,6 +1192,17 @@ class VertexBase:
or get_secret_str("VERTEXAI_CREDENTIALS")
)
+ @staticmethod
+ def explicit_vertex_ai_location(params: Mapping[str, object]) -> str | None:
+ """
+ The location explicitly configured in the given params, without any
+ module-level or environment fallback. None when not configured.
+ """
+ for configured in (params.get("vertex_location"), params.get("vertex_ai_location")):
+ if isinstance(configured, str) and configured:
+ return configured
+ return None
+
@staticmethod
def safe_get_vertex_ai_location(litellm_params: Mapping[str, object]) -> str | None:
"""
@@ -1206,7 +1217,9 @@ class VertexBase:
Returns:
Vertex AI location/region or None
"""
- for configured in (litellm_params.get("vertex_location"), litellm_params.get("vertex_ai_location")):
- if isinstance(configured, str) and configured:
- return configured
- return litellm.vertex_location or get_secret_str("VERTEXAI_LOCATION") or get_secret_str("VERTEX_LOCATION")
+ return (
+ VertexBase.explicit_vertex_ai_location(litellm_params)
+ or litellm.vertex_location
+ or get_secret_str("VERTEXAI_LOCATION")
+ or get_secret_str("VERTEX_LOCATION")
+ )
diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
index 01e5ec26b1e..c2d73ea467d 100644
--- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
+++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
@@ -4960,41 +4960,111 @@ def test_pre_call_redacts_and_masks_raw_request(logging_obj):
assert "key=*****" in raw_api_base
-def test_resolve_vertex_location_for_cost():
- """Vertex requests resolve the serving location the way dispatch does; other providers get None."""
+def _resolve(custom_llm_provider, litellm_params, optional_params, model):
from litellm.litellm_core_utils.litellm_logging import (
_resolve_vertex_location_for_cost,
)
- assert _resolve_vertex_location_for_cost("openai", {"vertex_location": "us-east5"}, "gpt-4o") is None
- assert _resolve_vertex_location_for_cost(None, {}, "gemini-3.5-flash") is None
- assert (
- _resolve_vertex_location_for_cost("vertex_ai", {"vertex_location": "us-east5"}, "gemini-3.5-flash")
- == "us-east5"
+ return _resolve_vertex_location_for_cost(
+ custom_llm_provider=custom_llm_provider,
+ litellm_params=litellm_params,
+ optional_params=optional_params,
+ model=model,
)
+
+
+def test_resolve_vertex_location_for_cost():
+ """Vertex requests resolve the serving location the way dispatch does; other providers get None."""
+ assert _resolve("openai", {"vertex_location": "us-east5"}, None, "gpt-4o") is None
+ assert _resolve(None, {}, None, "gemini-3.5-flash") is None
+ assert _resolve("vertex_ai", {"vertex_location": "us-east5"}, None, "gemini-3.5-flash") == "us-east5"
+ assert _resolve("vertex_ai", {"vertex_location": "global"}, None, "gemini-3.5-flash") == "global"
assert (
- _resolve_vertex_location_for_cost("vertex_ai", {"vertex_location": "global"}, "gemini-3.5-flash") == "global"
+ _resolve("vertex_ai_beta", {"vertex_ai_location": "europe-west1"}, None, "claude-haiku-4-5@20251001")
+ == "europe-west1"
)
+
+
+def test_resolve_vertex_location_for_cost_reads_optional_params(monkeypatch):
+ """
+ On the proxy the logging object predates deployment selection, so the deployment's
+ configured location only reaches it through optional_params. A configured global
+ location must beat the environment fallback, or every proxy call gets the regional uplift.
+ """
+ monkeypatch.setenv("VERTEXAI_LOCATION", "us-east5")
+ monkeypatch.setattr(litellm, "vertex_location", None)
+
+ assert _resolve("vertex_ai", {}, {"vertex_location": "global"}, "gemini-3.5-flash") == "global"
+ assert _resolve("vertex_ai", None, {"vertex_location": "europe-west1"}, "gemini-3.5-flash") == "europe-west1"
assert (
- _resolve_vertex_location_for_cost(
- "vertex_ai_beta", {"vertex_ai_location": "europe-west1"}, "claude-haiku-4-5@20251001"
+ _resolve(
+ "vertex_ai",
+ {"vertex_location": "us-east5"},
+ {"vertex_location": "global"},
+ "gemini-3.5-flash",
)
- == "europe-west1"
+ == "global"
)
+ assert _resolve("vertex_ai", {"vertex_location": "global"}, {}, "gemini-3.5-flash") == "global"
+ assert _resolve("vertex_ai", {}, {}, "gemini-3.5-flash") == "us-east5"
def test_resolve_vertex_location_for_cost_default_region(monkeypatch):
"""With no location configured anywhere, resolution lands on the dispatch default us-central1."""
- from litellm.litellm_core_utils.litellm_logging import (
- _resolve_vertex_location_for_cost,
- )
-
monkeypatch.delenv("VERTEXAI_LOCATION", raising=False)
monkeypatch.delenv("VERTEX_LOCATION", raising=False)
monkeypatch.setattr(litellm, "vertex_location", None)
- assert _resolve_vertex_location_for_cost("vertex_ai", {}, "gemini-3.5-flash") == "us-central1"
- assert _resolve_vertex_location_for_cost("vertex_ai", None, "gemini-3.5-flash") == "us-central1"
+ assert _resolve("vertex_ai", {}, None, "gemini-3.5-flash") == "us-central1"
+ assert _resolve("vertex_ai", None, None, "gemini-3.5-flash") == "us-central1"
+
+
+def test_response_cost_calculator_prices_proxy_vertex_calls_on_the_configured_location(monkeypatch):
+ """
+ Proxy-shaped logging objects (created before the router picks a deployment) carry the
+ deployment's vertex_location only in optional_params. A global deployment must price at
+ base rates even when the environment points at a regional location, and a regional one
+ must price with the uplift.
+ """
+ from datetime import datetime
+
+ from litellm.litellm_core_utils.get_model_cost_map import get_model_cost_map
+
+ monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")
+ monkeypatch.setattr(litellm, "model_cost", get_model_cost_map(url=""))
+ monkeypatch.setenv("VERTEXAI_LOCATION", "us-east5")
+ monkeypatch.setattr(litellm, "vertex_location", None)
+
+ def cost_at(location):
+ logging_obj = LitellmLogging(
+ model="gemini-3.5-flash",
+ messages=[{"role": "user", "content": "hi"}],
+ stream=False,
+ call_type="completion",
+ start_time=datetime.now(),
+ litellm_call_id=f"vertex-loc-{location}",
+ function_id="f",
+ )
+ logging_obj.update_environment_variables(
+ model="gemini-3.5-flash",
+ user="",
+ optional_params={"vertex_location": location},
+ litellm_params={"api_base": ""},
+ custom_llm_provider="vertex_ai",
+ )
+ response = ModelResponse(
+ id="resp-1",
+ model="gemini-3.5-flash",
+ choices=[{"message": {"role": "assistant", "content": "hello"}, "index": 0, "finish_reason": "stop"}],
+ usage={"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
+ )
+ return logging_obj._response_cost_calculator(result=response)
+
+ info = litellm.model_cost["vertex_ai/gemini-3.5-flash"]
+ expected_global = 10 * info["input_cost_per_token"] + 5 * info["output_cost_per_token"]
+
+ assert cost_at("global") == pytest.approx(expected_global)
+ assert cost_at("us-east5") == pytest.approx(info["regional_endpoint_uplift_multiplier"] * expected_global)
def test_set_cost_breakdown_stores_vertex_location():