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 Gemini labels field provider-aware filtering (#14563)

Tim Elfrink9d7942eb352d
diff --git a/litellm/llms/vertex_ai/gemini/transformation.py b/litellm/llms/vertex_ai/gemini/transformation.py
index 327b269d1d4..c59e3bb24e8 100644
--- a/litellm/llms/vertex_ai/gemini/transformation.py
+++ b/litellm/llms/vertex_ai/gemini/transformation.py
@@ -28,6 +28,7 @@ from litellm.types.files import (
get_file_type_from_extension,
is_gemini_1_5_accepted_file_type,
)
+from litellm.types.utils import LlmProviders
from litellm.types.llms.openai import (
AllMessageValues,
ChatCompletionAssistantMessage,
@@ -492,7 +493,8 @@ def _transform_request_body(
data["generationConfig"] = generation_config
if cached_content is not None:
data["cachedContent"] = cached_content
- if labels is not None:
+ # Only add labels for Vertex AI endpoints (not Google GenAI/AI Studio) and only if non-empty
+ if labels and custom_llm_provider != LlmProviders.GEMINI:
data["labels"] = labels
except Exception as e:
raise e
@@ -647,3 +649,5 @@ def _transform_system_message(
return SystemInstructions(parts=system_content_blocks), messages
return None, messages
+
+
diff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
index d6d33258576..4da2976e1f9 100644
--- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
+++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
@@ -1,4 +1,7 @@
-from litellm.llms.vertex_ai.gemini.transformation import check_if_part_exists_in_parts
+from litellm.llms.vertex_ai.gemini.transformation import (
+ check_if_part_exists_in_parts,
+ _transform_request_body,
+)
def test_check_if_part_exists_in_parts():
@@ -73,3 +76,82 @@ def test_check_if_part_exists_in_parts_camel_case_snake_case():
}
assert check_if_part_exists_in_parts(parts_mixed, part_mixed_casing)
+
+
+# Tests for issue #14556: Labels field provider-aware filtering
+def test_google_genai_excludes_labels():
+ """Test that Google GenAI/AI Studio endpoints exclude labels when custom_llm_provider='gemini'"""
+ messages = [{"role": "user", "content": "test"}]
+ optional_params = {"labels": {"project": "test", "team": "ai"}}
+ litellm_params = {}
+
+ result = _transform_request_body(
+ messages=messages,
+ model="gemini-2.5-pro",
+ optional_params=optional_params,
+ custom_llm_provider="gemini",
+ litellm_params=litellm_params,
+ cached_content=None,
+ )
+
+ # Google GenAI/AI Studio should NOT include labels
+ assert "labels" not in result
+ assert "contents" in result
+
+
+def test_vertex_ai_includes_labels():
+ """Test that Vertex AI endpoints include labels when custom_llm_provider='vertex_ai'"""
+ messages = [{"role": "user", "content": "test"}]
+ optional_params = {"labels": {"project": "test", "team": "ai"}}
+ litellm_params = {}
+
+ result = _transform_request_body(
+ messages=messages,
+ model="gemini-2.5-pro",
+ optional_params=optional_params,
+ custom_llm_provider="vertex_ai",
+ litellm_params=litellm_params,
+ cached_content=None,
+ )
+
+ # Vertex AI SHOULD include labels
+ assert "labels" in result
+ assert result["labels"] == {"project": "test", "team": "ai"}
+
+
+
+def test_metadata_to_labels_vertex_only():
+ """Test that metadata->labels conversion only happens for Vertex AI"""
+ messages = [{"role": "user", "content": "test"}]
+ optional_params = {}
+ litellm_params = {
+ "metadata": {
+ "requester_metadata": {
+ "user": "john_doe",
+ "project": "test-project"
+ }
+ }
+ }
+
+ # Google GenAI/AI Studio should not include labels from metadata
+ result = _transform_request_body(
+ messages=messages,
+ model="gemini-2.5-pro",
+ optional_params=optional_params.copy(),
+ custom_llm_provider="gemini",
+ litellm_params=litellm_params.copy(),
+ cached_content=None,
+ )
+ assert "labels" not in result
+
+ # Vertex AI should include labels from metadata
+ result = _transform_request_body(
+ messages=messages,
+ model="gemini-2.5-pro",
+ optional_params=optional_params.copy(),
+ custom_llm_provider="vertex_ai",
+ litellm_params=litellm_params.copy(),
+ cached_content=None,
+ )
+ assert "labels" in result
+ assert result["labels"] == {"user": "john_doe", "project": "test-project"}