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(gemini)!: honor supports_sampling_params on the Gemini and Vertex path and flag Gemini 3.6 Flash, 3.5 Flash-Lite and later (#47645)

ryan-crabbe-berric29a4f56a300
diff --git a/litellm/llms/gemini/chat/transformation.py b/litellm/llms/gemini/chat/transformation.py
index 143652ee77c..936919af83d 100644
--- a/litellm/llms/gemini/chat/transformation.py
+++ b/litellm/llms/gemini/chat/transformation.py
@@ -82,9 +82,9 @@ class GoogleAIStudioGeminiConfig(VertexGeminiConfig):
return "tts" in model
def get_supported_openai_params(self, model: str) -> list[str]:
+ sampling_params: Final = ("temperature", "top_p") if self.supports_sampling_params(model) else ()
supported_params: Final = [
- "temperature",
- "top_p",
+ *sampling_params,
"max_tokens",
"max_completion_tokens",
"stream",
diff --git a/litellm/llms/vertex_ai/files/transformation.py b/litellm/llms/vertex_ai/files/transformation.py
index ea71e5fef50..176569f5d8d 100644
--- a/litellm/llms/vertex_ai/files/transformation.py
+++ b/litellm/llms/vertex_ai/files/transformation.py
@@ -1107,14 +1107,18 @@ class VertexAIFilesConfig(VertexBase, BaseFilesConfig):
"""
wrapper to call VertexGeminiConfig.map_openai_params
"""
- from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (
- VertexGeminiConfig,
- )
-
config: Final = VertexGeminiConfig()
- _model: Final = openai_request_body.get("model", "")
+ raw_model: Final = openai_request_body.get("model", "")
+ model: Final = raw_model if isinstance(raw_model, str) else ""
+ sampling_params: Final = tuple(
+ p for p in ("temperature", "top_p", "top_k") if openai_request_body.get(p) is not None
+ )
+ if sampling_params and not VertexGeminiConfig.supports_sampling_params(model):
+ VertexGeminiConfig.reject_unsupported_sampling_param(
+ model=model, param=", ".join(sampling_params), drop_params=False
+ )
vertex_params: Final = config.map_openai_params(
- model=_model,
+ model=model,
non_default_params=openai_request_body,
optional_params={},
drop_params=False,
diff --git a/litellm/llms/vertex_ai/gemini/transformation.py b/litellm/llms/vertex_ai/gemini/transformation.py
index b6517a90250..e5ce395031a 100644
--- a/litellm/llms/vertex_ai/gemini/transformation.py
+++ b/litellm/llms/vertex_ai/gemini/transformation.py
@@ -16,6 +16,7 @@ from pydantic import BaseModel, TypeAdapter
import litellm
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.asyncify import asyncify
+from litellm.litellm_core_utils.core_helpers import drops_unsupported_params
from litellm.litellm_core_utils.prompt_templates.common_utils import (
get_image_mime_type_from_url,
)
@@ -1189,7 +1190,18 @@ def transform_request_body(
# labels: optional explicit param and/or metadata.requester_metadata (OpenAI metadata)
labels: Final = pop_vertex_request_labels(optional_params, litellm_params)
- filtered_params = {k: v for k, v in optional_params.items() if _get_equivalent_key(k, set(config_fields))}
+ from .vertex_and_google_ai_studio_gemini import VertexGeminiConfig
+
+ drops_top_k: Final = "top_k" in optional_params and not VertexGeminiConfig.supports_sampling_params(model)
+ if drops_top_k:
+ request_params: Final = cast(Mapping[str, object], litellm_params) # cast-ok: untyped shared signature
+ VertexGeminiConfig.reject_unsupported_sampling_param(
+ model=model,
+ param="top_k",
+ drop_params=drops_unsupported_params(request_params.get("drop_params")),
+ )
+ config_keys: Final = set(config_fields) - {"top_k"} if drops_top_k else set(config_fields)
+ filtered_params = {k: v for k, v in optional_params.items() if _get_equivalent_key(k, config_keys)}
generation_config: Final[GenerationConfig | None] = GenerationConfig(**filtered_params)
diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
index a65ae9da3e9..241b2d2a3a0 100644
--- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
+++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
@@ -429,11 +429,43 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
return False
return True
+ @staticmethod
+ def supports_sampling_params(model: str) -> bool:
+ """Whether Google still honors ``temperature`` / ``top_p`` / ``top_k`` for ``model``.
+
+ Driven by ``supports_sampling_params`` in the model map: Gemini 3.6 Flash, 3.5 Flash-Lite
+ and later accept the fields but ignore them, and a later generation will 400 on them.
+ A model the map says nothing about keeps forwarding them. ``temperature`` and ``top_p``
+ leave ``get_supported_openai_params`` when the flag is off, so the shared unsupported-param
+ check raises or drops them and ``allowed_openai_params`` still forwards them verbatim;
+ ``top_k`` is a provider-specific kwarg that bypasses that check and is gated while the
+ request body is built."""
+ rows: Final = cast(Mapping[str, Mapping[str, object]], litellm.model_cost) # cast-ok: JSON rows by model id
+ bare: Final = model.split("/")[-1]
+ for candidate in (model, bare, f"vertex_ai/{bare}", f"gemini/{bare}"):
+ if isinstance(flag := rows.get(candidate, {}).get("supports_sampling_params"), bool):
+ return flag
+ return True
+
+ @staticmethod
+ def reject_unsupported_sampling_param(model: str, param: str, drop_params: bool) -> None:
+ """Return under ``drop_params`` so the caller drops ``param``, else raise the client-side 400
+ that names the escape hatch."""
+ if litellm.drop_params or drop_params:
+ return
+ raise UnsupportedParamsError(
+ message=(
+ f"{model} does not support {param}: the model map flags it `supports_sampling_params: false` "
+ "because Google ignores sampling params on this model. "
+ "To drop unsupported params, set `litellm.drop_params = True`."
+ ),
+ status_code=400,
+ )
+
def get_supported_openai_params(self, model: str) -> list[str]:
+ sampling_params: Final = ("temperature", "top_p", "top_k") if self.supports_sampling_params(model) else ()
supported_params: Final = [
- "temperature",
- "top_p",
- "top_k",
+ *sampling_params,
"max_tokens",
"max_completion_tokens",
"stream",
@@ -1291,7 +1323,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
allow_integer_enums: bool = True,
) -> dict:
self._apply_include_server_side_tool_invocations(non_default_params, optional_params)
+ supports_sampling_params: Final = self.supports_sampling_params(model)
for param, value in non_default_params.items():
+ if param in ("temperature", "top_p", "top_k") and not supports_sampling_params:
+ continue
if param == "temperature":
if VertexGeminiConfig._is_gemini_3_or_newer(model):
if value is not None and value < 1.0:
diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json
index 04a3f9225ce..98fee42de3c 100644
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@ -21921,6 +21921,7 @@
],
"supports_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -21945,6 +21946,7 @@
],
"supports_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -21976,6 +21978,7 @@
"supports_function_calling": true,
"supports_prompt_caching": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -22039,6 +22042,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -29435,6 +29439,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30157,6 +30162,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30218,6 +30224,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30286,6 +30293,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30335,6 +30343,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": false,
"supports_url_context": false,
@@ -31761,6 +31770,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -31945,6 +31955,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32007,6 +32018,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32069,6 +32081,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32438,6 +32451,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32499,6 +32513,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32567,6 +32582,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32616,6 +32632,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": false,
"supports_url_context": false,
@@ -33210,6 +33227,7 @@
"supports_function_calling": true,
"supports_parallel_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_vision": true
},
"github_copilot/gemini-3.8-flash": {
@@ -33228,6 +33246,7 @@
"supports_function_calling": true,
"supports_parallel_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_vision": true
},
"github_copilot/gpt-3.5-turbo": {
@@ -52623,6 +52642,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -65841,6 +65861,7 @@
"supports_tool_choice": true,
"supports_response_schema": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_vision": true,
"source": "https://deepinfra.com/pricing",
"supports_audio_input": true
@@ -69282,6 +69303,7 @@
"perplexity/google/gemini-3.5-flash-lite": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"supports_web_search": true,
"supports_function_calling": true,
"input_cost_per_token": 3e-07,
@@ -69292,6 +69314,7 @@
"perplexity/google/gemini-3.6-flash": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"supports_web_search": true,
"supports_function_calling": true,
"input_cost_per_token": 1.5e-06,
@@ -69302,6 +69325,7 @@
"perplexity/google/gemini-3.7-flash": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"supports_web_search": true,
"supports_function_calling": true,
"input_cost_per_token": 7.5e-07,
@@ -71393,6 +71417,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_vision": true,
"supports_web_search": true
},
@@ -71410,6 +71435,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_vision": true,
"supports_web_search": true
},
@@ -74340,6 +74366,7 @@
"perplexity/google/gemini-3.8-flash": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"input_cost_per_token": 7.5e-07,
"output_cost_per_token": 3.75e-06,
"cache_read_input_token_cost": 7.5e-08,
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index f86c6cfd4b6..5939a350970 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -212,6 +212,7 @@ class ProviderSpecificModelInfo(TypedDict, total=False):
supports_parallel_function_calling: bool | None
supports_web_search: bool | None
supports_reasoning: bool | None
+ supports_sampling_params: ReadOnly[bool | None]
supports_adaptive_thinking: bool | None
supports_legacy_thinking: ReadOnly[bool | None]
thinking_always_on: ReadOnly[bool | None]
diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json
index 04a3f9225ce..98fee42de3c 100644
--- a/model_prices_and_context_window.json
+++ b/model_prices_and_context_window.json
@@ -21921,6 +21921,7 @@
],
"supports_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -21945,6 +21946,7 @@
],
"supports_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -21976,6 +21978,7 @@
"supports_function_calling": true,
"supports_prompt_caching": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -22039,6 +22042,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_tool_choice": true,
"supports_vision": true
},
@@ -29435,6 +29439,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30157,6 +30162,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30218,6 +30224,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30286,6 +30293,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -30335,6 +30343,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": false,
"supports_url_context": false,
@@ -31761,6 +31770,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -31945,6 +31955,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32007,6 +32018,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32069,6 +32081,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32438,6 +32451,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32499,6 +32513,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32567,6 +32582,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -32616,6 +32632,7 @@
"supports_reasoning": true,
"supports_minimal_reasoning_effort": false,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": false,
"supports_url_context": false,
@@ -33210,6 +33227,7 @@
"supports_function_calling": true,
"supports_parallel_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_vision": true
},
"github_copilot/gemini-3.8-flash": {
@@ -33228,6 +33246,7 @@
"supports_function_calling": true,
"supports_parallel_function_calling": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_vision": true
},
"github_copilot/gpt-3.5-turbo": {
@@ -52623,6 +52642,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_system_messages": true,
"supports_tool_choice": true,
"supports_url_context": true,
@@ -65841,6 +65861,7 @@
"supports_tool_choice": true,
"supports_response_schema": true,
"supports_reasoning": true,
+ "supports_sampling_params": false,
"supports_vision": true,
"source": "https://deepinfra.com/pricing",
"supports_audio_input": true
@@ -69282,6 +69303,7 @@
"perplexity/google/gemini-3.5-flash-lite": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"supports_web_search": true,
"supports_function_calling": true,
"input_cost_per_token": 3e-07,
@@ -69292,6 +69314,7 @@
"perplexity/google/gemini-3.6-flash": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"supports_web_search": true,
"supports_function_calling": true,
"input_cost_per_token": 1.5e-06,
@@ -69302,6 +69325,7 @@
"perplexity/google/gemini-3.7-flash": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"supports_web_search": true,
"supports_function_calling": true,
"input_cost_per_token": 7.5e-07,
@@ -71393,6 +71417,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_vision": true,
"supports_web_search": true
},
@@ -71410,6 +71435,7 @@
"supports_prompt_caching": true,
"supports_reasoning": true,
"supports_response_schema": true,
+ "supports_sampling_params": false,
"supports_vision": true,
"supports_web_search": true
},
@@ -74340,6 +74366,7 @@
"perplexity/google/gemini-3.8-flash": {
"litellm_provider": "perplexity",
"mode": "responses",
+ "supports_sampling_params": false,
"input_cost_per_token": 7.5e-07,
"output_cost_per_token": 3.75e-06,
"cache_read_input_token_cost": 7.5e-08,
diff --git a/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py b/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py
index dc1e762e752..cf4a7c4cbc8 100644
--- a/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py
+++ b/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py
@@ -12,8 +12,10 @@ from urllib.parse import parse_qs, urlparse
import httpx
import pytest
+from pydantic import TypeAdapter
from unittest.mock import MagicMock
+import litellm
from litellm.llms.vertex_ai.files.transformation import (
VertexAIFilesConfig,
_get_litellm_batch_custom_id_from_labels,
@@ -1988,3 +1990,58 @@ class TestVertexEmbeddingsBatchOutputTranslation:
content = json.dumps(legacy_row).encode("utf-8")
assert config._try_transform_vertex_batch_output_to_openai(content) == content
+
+
+_SAMPLING_KNOBS: Final = MappingProxyType({"temperature": 0, "top_p": 0.5, "top_k": 3})
+
+
+def _chat_entry(model: str, knobs: Mapping[str, object]) -> Mapping[str, object]:
+ return MappingProxyType(
+ {
+ "custom_id": "request-1",
+ "method": "POST",
+ "url": "/v1/chat/completions",
+ "body": {"model": model, "messages": [{"role": "user", "content": "hi"}], "max_tokens": 5, **knobs},
+ }
+ )
+
+
+def _generation_config(row: Mapping[str, object]) -> object:
+ request: Final = TypeAdapter(dict[str, object]).validate_python(row["request"])
+ return request["generationConfig"]
+
+
+def _flag_model_as_no_sampling(monkeypatch: pytest.MonkeyPatch, model: str) -> None:
+ monkeypatch.setitem(litellm.model_cost, model, {"supports_sampling_params": False})
+
+
+def test_batch_row_for_a_flagged_gemini_model_raises_a_400_naming_drop_params(monkeypatch: pytest.MonkeyPatch) -> None:
+ """A batch file row gets the same contract as a real-time call: the knobs Google ignores are refused
+ with a 400 that names drop_params instead of silently vanishing from the uploaded row"""
+ _flag_model_as_no_sampling(monkeypatch, "gemini-zeta-9")
+ monkeypatch.setattr(litellm, "drop_params", False)
+ with pytest.raises(litellm.UnsupportedParamsError, match=r"temperature, top_p, top_k.*drop_params"):
+ _wrap_entries([_chat_entry("gemini-zeta-9", _SAMPLING_KNOBS)])
+
+
+def test_batch_row_with_null_knobs_for_a_flagged_gemini_model_is_not_refused(monkeypatch: pytest.MonkeyPatch) -> None:
+ """A null knob means no override, the same as on a real-time call, so it must not fail the upload"""
+ _flag_model_as_no_sampling(monkeypatch, "gemini-zeta-9")
+ monkeypatch.setattr(litellm, "drop_params", False)
+ (row,) = _wrap_entries([_chat_entry("gemini-zeta-9", {"temperature": None, "top_p": None, "top_k": None})])
+ assert _generation_config(row) == {"max_output_tokens": 5}
+
+
+def test_batch_row_for_a_flagged_gemini_model_drops_the_knobs_under_global_drop_params(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _flag_model_as_no_sampling(monkeypatch, "gemini-zeta-9")
+ monkeypatch.setattr(litellm, "drop_params", True)
+ (row,) = _wrap_entries([_chat_entry("gemini-zeta-9", _SAMPLING_KNOBS)])
+ assert _generation_config(row) == {"max_output_tokens": 5}
+
+
+def test_batch_row_for_an_unflagged_gemini_model_keeps_the_knobs(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(litellm, "drop_params", False)
+ (row,) = _wrap_entries([_chat_entry("gemini-2.5-flash", _SAMPLING_KNOBS)])
+ assert _generation_config(row) == {"max_output_tokens": 5, **_SAMPLING_KNOBS}
diff --git a/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py b/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
index 0796e125817..bb624add673 100644
--- a/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
+++ b/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
@@ -27,7 +27,7 @@ from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (
VertexGeminiConfig,
_log_gemini_sampling_params_deprecated,
)
-from litellm.types.llms.vertex_ai import GeminiFinishReason, UsageMetadata
+from litellm.types.llms.vertex_ai import GeminiFinishReason, RequestBody, UsageMetadata
from litellm.types.utils import ChoiceLogprobs, Usage
from litellm.utils import _invalidate_model_cost_lowercase_map, CustomStreamWrapper
from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
@@ -3739,6 +3739,169 @@ def test_get_optional_params_gemini_3_has_no_default_temperature(
assert optional_params["thinkingConfig"]["thinkingLevel"] == "medium"
+_SAMPLING_PARAMS: Final = {"temperature": 0.3, "top_p": 0.9, "top_k": 20}
+
+
+def _gemini_config(config_cls: Literal["vertex", "studio"]) -> VertexGeminiConfig:
+ return VertexGeminiConfig() if config_cls == "vertex" else GoogleAIStudioGeminiConfig()
+
+
+def _gemini_request_body(
+ custom_llm_provider: Literal["vertex_ai", "gemini"],
+ optional_params: dict[str, object],
+ litellm_params: dict[str, object],
+) -> RequestBody:
+ from litellm.llms.vertex_ai.gemini.transformation import transform_request_body
+
+ return transform_request_body(
+ messages=[{"role": "user", "content": "hi"}],
+ model="gemini-zeta-9",
+ optional_params=optional_params,
+ custom_llm_provider=custom_llm_provider,
+ litellm_params=litellm_params,
+ cached_content=None,
+ )
+
+
[email protected]("custom_llm_provider", ["vertex_ai", "gemini"])
[email protected]("param", ["temperature", "top_p"])
+def test_get_optional_params_raises_a_400_naming_drop_params_for_a_flagged_gemini_model(
+ custom_llm_provider: Literal["vertex_ai", "gemini"], param: str, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Google ignores sampling params on models flagged ``supports_sampling_params: false``, so without
+ drop_params the caller gets the 400 that names the escape hatch instead of a 200 that lied (#47015)"""
+ from litellm.utils import get_optional_params
+
+ monkeypatch.setattr(litellm, "drop_params", False)
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", {"supports_sampling_params": False})
+
+ with pytest.raises(litellm.utils.UnsupportedParamsError, match="drop_params") as excinfo:
+ get_optional_params(
+ model="gemini-zeta-9", custom_llm_provider=custom_llm_provider, **{param: _SAMPLING_PARAMS[param]}
+ )
+
+ assert excinfo.value.status_code == 400
+ assert param in str(excinfo.value)
+
+
[email protected]("custom_llm_provider", ["vertex_ai", "gemini"])
+def test_allowed_openai_params_still_forwards_a_sampling_param_the_map_flags_off(
+ custom_llm_provider: Literal["vertex_ai", "gemini"], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The 400 tells the caller to send allowed_openai_params, so that escape hatch has to work"""
+ from litellm.utils import get_optional_params
+
+ monkeypatch.setattr(litellm, "drop_params", False)
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", {"supports_sampling_params": False})
+
+ optional_params: Final = get_optional_params(
+ model="gemini-zeta-9",
+ custom_llm_provider=custom_llm_provider,
+ temperature=0.3,
+ allowed_openai_params=["temperature"],
+ )
+
+ assert optional_params["temperature"] == 0.3
+
+
[email protected]("custom_llm_provider", ["vertex_ai", "gemini"])
+def test_top_k_is_stripped_from_the_request_body_of_a_flagged_gemini_model_under_drop_params(
+ custom_llm_provider: Literal["vertex_ai", "gemini"], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """top_k is a provider-specific kwarg that skips the shared unsupported-param check, so it is gated
+ where the request body is built"""
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", {"supports_sampling_params": False})
+
+ body: Final = _gemini_request_body(
+ custom_llm_provider, {"top_k": 20, "max_output_tokens": 10}, {"drop_params": True}
+ )
+
+ assert body["generationConfig"] == {"max_output_tokens": 10}
+
+
[email protected]("custom_llm_provider", ["vertex_ai", "gemini"])
+def test_top_k_raises_a_400_naming_drop_params_for_a_flagged_gemini_model(
+ custom_llm_provider: Literal["vertex_ai", "gemini"], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(litellm, "drop_params", False)
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", {"supports_sampling_params": False})
+
+ with pytest.raises(litellm.utils.UnsupportedParamsError, match="drop_params") as excinfo:
+ _gemini_request_body(custom_llm_provider, {"top_k": 20}, {})
+
+ assert excinfo.value.status_code == 400
+ assert "top_k" in str(excinfo.value)
+
+
[email protected]("custom_llm_provider", ["vertex_ai", "gemini"])
[email protected]("row", [{"supports_sampling_params": True}, {}], ids=["flag_true", "flag_absent"])
+def test_gemini_keeps_forwarding_sampling_params_unless_the_model_map_flags_them_off(
+ custom_llm_provider: Literal["vertex_ai", "gemini"], row: dict[str, bool], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """An explicit true and a model the map says nothing about both keep today's pass-through, top_k included"""
+ from litellm.utils import get_optional_params
+
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", row)
+
+ optional_params: Final = get_optional_params(
+ model="gemini-zeta-9", custom_llm_provider=custom_llm_provider, drop_params=True, **_SAMPLING_PARAMS
+ )
+ body: Final = _gemini_request_body(custom_llm_provider, dict(optional_params), {"drop_params": True})
+
+ assert body["generationConfig"] == _SAMPLING_PARAMS
+
+
[email protected]("model_key", ["gemini-zeta-9", "vertex_ai/gemini-zeta-9", "gemini/gemini-zeta-9"])
+def test_gemini_sampling_flag_is_read_from_whichever_provider_row_carries_it(
+ model_key: str, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setitem(litellm.model_cost, model_key, {"supports_sampling_params": False})
+
+ result: Final = VertexGeminiConfig().map_openai_params(
+ non_default_params={"temperature": 0.3},
+ optional_params={},
+ model="gemini-zeta-9",
+ drop_params=True,
+ )
+
+ assert "temperature" not in result
+
+
[email protected]("config_cls", ["vertex", "studio"])
+def test_gemini_supported_openai_params_omit_sampling_params_when_flagged_off(
+ config_cls: Literal["vertex", "studio"], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", {"supports_sampling_params": False})
+
+ supported: Final = set(_gemini_config(config_cls).get_supported_openai_params("gemini-zeta-9"))
+
+ assert not supported & set(_SAMPLING_PARAMS)
+ assert "max_tokens" in supported
+
+
[email protected]("custom_llm_provider", ["vertex_ai", "gemini"])
+def test_get_optional_params_drops_sampling_params_for_a_flagged_gemini_model_under_drop_params(
+ custom_llm_provider: Literal["vertex_ai", "gemini"], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The path every SDK and proxy call takes, on both the Vertex and the AI Studio route"""
+ from litellm.utils import get_optional_params
+
+ monkeypatch.setitem(litellm.model_cost, "gemini-zeta-9", {"supports_sampling_params": False})
+
+ optional_params: Final = get_optional_params(
+ model="gemini-zeta-9",
+ custom_llm_provider=custom_llm_provider,
+ temperature=0.3,
+ top_p=0.9,
+ max_tokens=10,
+ drop_params=True,
+ )
+
+ assert "temperature" not in optional_params
+ assert "top_p" not in optional_params
+ assert optional_params["max_output_tokens"] == 10
+
+
def test_media_resolution_from_detail_parameter():
"""Test that OpenAI's detail parameter is correctly mapped to media_resolution"""
from litellm.llms.vertex_ai.gemini.transformation import (
diff --git a/tests/unit/test_model_prices_and_context_window.py b/tests/unit/test_model_prices_and_context_window.py
index 628870259a4..701b79d0ba9 100644
--- a/tests/unit/test_model_prices_and_context_window.py
+++ b/tests/unit/test_model_prices_and_context_window.py
@@ -272,6 +272,27 @@ def test_vertex_gemini_flash_rows_carry_the_documented_retirement_date(prices: d
assert prices[f"{prefix}{model}"]["deprecation_date"] == VERTEX_GEMINI_FLASH_RETIREMENT_DATES[model]
+def _sampling_flags_across_provider_twins(prices: Mapping[str, Mapping[str, object]], key: str) -> frozenset[object]:
+ bare: Final = key.split("/")[-1]
+ twins: Final = tuple(twin for twin in (bare, f"vertex_ai/{bare}", f"gemini/{bare}") if twin in prices)
+ return frozenset(prices[twin].get("supports_sampling_params") for twin in (key, *twins))
+
+
+def test_gemini_rows_agree_on_supports_sampling_params_across_their_provider_twins(
+ prices: Mapping[str, Mapping[str, object]],
+) -> None:
+ """The bare, vertex_ai/ and gemini/ rows of one Gemini model gate sampling params identically, so a request
+ takes the same drop-or-400 path on the Vertex route and the AI Studio route"""
+ flagged: Final = tuple(
+ key
+ for key, row in prices.items()
+ if "supports_sampling_params" in row and key.split("/")[-1].startswith("gemini-")
+ )
+ assert flagged
+ for key in flagged:
+ assert len(_sampling_flags_across_provider_twins(prices, key)) == 1, key
+
+
OPENAI_REASONING_FAMILY_MARKERS = ("codex", "deep-research", "chat-latest")
diff --git a/tests/unit/test_router_model_cost_isolation.py b/tests/unit/test_router_model_cost_isolation.py
index 426481d2719..f001008ee2c 100644
--- a/tests/unit/test_router_model_cost_isolation.py
+++ b/tests/unit/test_router_model_cost_isolation.py
@@ -585,7 +585,7 @@ def test_should_not_pollute_shared_key_with_custom_nonzero_pricing():
)
[email protected]("flag", ["supports_regex_lookaround", "supports_assistant_prefill"])
[email protected]("flag", ["supports_regex_lookaround", "supports_assistant_prefill", "supports_sampling_params"])
def test_capability_flag_stays_on_the_deployment_that_set_it(flag: str) -> None:
"""A deployment's capability override must not land on the shared
``{provider}/{model}`` key, or every sibling deployment of that model would inherit it."""