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): put embed config inside the request and read live usage

milane96614a39f45
diff --git a/litellm/llms/vertex_ai/files/transformation.py b/litellm/llms/vertex_ai/files/transformation.py
index 516a3ca7184..1a5d40dabb2 100644
--- a/litellm/llms/vertex_ai/files/transformation.py
+++ b/litellm/llms/vertex_ai/files/transformation.py
@@ -82,7 +82,7 @@ _GCP_LABEL_VALUE_MAX_LEN = 63
_CUSTOM_ID_RAW_LABEL_PREFIX = "b32_"
_VERTEX_BATCH_KEY_FIELD = "key"
_MANAGED_GCS_MODEL_PATH_PATTERN = re.compile(r"publishers/[^/]+/models/([^/?]+)")
-_EMBED_CONTENT_CONFIG_FIELD_BY_GEMINI_PARAM = {
+_EMBED_REQUEST_FIELD_BY_GEMINI_PARAM = {
"outputDimensionality": "output_dimensionality",
"taskType": "task_type",
"title": "title",
@@ -231,10 +231,11 @@ def _transform_vertex_embeddings_batch_output_row_to_openai(
output row holding an `/v1/embeddings` response body.
Example Vertex jsonl
- {"key": "id_1", "request": {...}, "response": {"tokenCount": "2", "embedding": {"values": [-0.015, 0.024]}}}
+ {"key": "id_1", "request": {...}, "response": {"embedding": {"values": [-0.015, 0.024]}, "usageMetadata": {"promptTokenCount": 2}}}
- `tokenCount` is serialized as a string by Vertex (int64 proto field), and the row
- carries no `modelVersion`, so the model comes from the batch the row belongs to.
+ Live rows report usage under `usageMetadata`; the documented `tokenCount` is kept as
+ a fallback. The row carries no `modelVersion`, so the model comes from the batch it
+ belongs to.
"""
custom_id = _get_litellm_batch_custom_id(vertex_output_row)
status = vertex_output_row.get("status", "")
@@ -245,7 +246,8 @@ def _transform_vertex_embeddings_batch_output_row_to_openai(
)
vertex_response = vertex_output_row.get("response") or {}
- token_count = int(vertex_response.get("tokenCount") or 0)
+ usage_metadata = vertex_response.get("usageMetadata") or {}
+ token_count = int(usage_metadata.get("promptTokenCount") or vertex_response.get("tokenCount") or 0)
body = EmbeddingResponse(
model=model or "",
data=[
@@ -296,11 +298,13 @@ def _openai_batch_jsonl_entry_to_vertex_embeddings_row(
Embedding batch row.
Example Vertex jsonl
- {"key": "id_1", "request": {"content": {"parts": [{"text": "Hello World"}]}}, "embed_content_config": {"output_dimensionality": 768, "task_type": "RETRIEVAL_DOCUMENT"}}
+ {"key": "id_1", "request": {"content": {"parts": [{"text": "Hello World"}]}, "output_dimensionality": 768, "task_type": "RETRIEVAL_DOCUMENT"}}
Note that `content` is singular (an `EmbedContentRequest`, not a
- `GenerateContentRequest`), the per-row config is a sibling of `request` rather than
- part of it, and the `custom_id` round-trips through the top-level `key`.
+ `GenerateContentRequest`) and that the `custom_id` round-trips through the top-level
+ `key`. The docs put the per-row config in an `embed_content_config` sibling of
+ `request`, but the API rejects that key outright and fails the whole batch job, so
+ the config fields go inside the `EmbedContentRequest` itself.
API Ref: https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/batch-prediction-genai-embeddings
"""
@@ -314,17 +318,16 @@ def _openai_batch_jsonl_entry_to_vertex_embeddings_row(
model=openai_request_body.get("model", ""),
optional_params=openai_request_body,
)
- embed_content_config = {
- config_field: embed_content_request[gemini_param]
- for gemini_param, config_field in _EMBED_CONTENT_CONFIG_FIELD_BY_GEMINI_PARAM.items()
+ embed_request_fields = {
+ request_field: embed_content_request[gemini_param]
+ for gemini_param, request_field in _EMBED_REQUEST_FIELD_BY_GEMINI_PARAM.items()
if gemini_param in embed_content_request
}
custom_id = openai_entry.get("custom_id")
return {
**({_VERTEX_BATCH_KEY_FIELD: str(custom_id)} if custom_id is not None else {}),
- "request": {"content": embed_content_request["content"]},
- **({"embed_content_config": embed_content_config} if embed_content_config else {}),
+ "request": {"content": embed_content_request["content"], **embed_request_fields},
}
diff --git a/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_transformation.py b/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_transformation.py
index 636a3106617..3eff3083220 100644
--- a/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_transformation.py
+++ b/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_transformation.py
@@ -1359,7 +1359,11 @@ class TestVertexEmbeddingsBatchInputTranslation:
assert "key" not in row
- def test_should_map_openai_params_to_embed_content_config_sibling(self):
+ def test_should_map_openai_params_into_the_embed_content_request(self):
+ """
+ The docs put these in an `embed_content_config` sibling of `request`, but Vertex
+ rejects that key and fails the whole job, so they belong inside the request.
+ """
(row,) = _wrap_entries(
[
_embeddings_entry(
@@ -1374,17 +1378,20 @@ class TestVertexEmbeddingsBatchInputTranslation:
]
)
- assert row["embed_content_config"] == {
- "output_dimensionality": 768,
- "task_type": "RETRIEVAL_DOCUMENT",
- "title": "some_title",
+ assert row == {
+ "key": "request-1",
+ "request": {
+ "content": {"parts": [{"text": "hello world"}]},
+ "output_dimensionality": 768,
+ "task_type": "RETRIEVAL_DOCUMENT",
+ "title": "some_title",
+ },
}
- assert "output_dimensionality" not in row["request"]
- def test_should_omit_embed_content_config_when_no_params_given(self):
+ def test_should_omit_config_fields_when_no_params_given(self):
(row,) = _wrap_entries([_embeddings_entry()])
- assert "embed_content_config" not in row
+ assert set(row["request"]) == {"content"}
def test_should_translate_multimodal_gcs_input(self):
(row,) = _wrap_entries(
@@ -1465,8 +1472,8 @@ class TestVertexEmbeddingsBatchOutputTranslation:
"key": "request-1",
"request": {"content": {"parts": [{"text": "hello world"}]}},
"response": {
- "tokenCount": "2",
"embedding": {"values": [-0.015, 0.024]},
+ "usageMetadata": {"promptTokenCount": 2},
},
}
row.update(overrides)
@@ -1503,6 +1510,21 @@ class TestVertexEmbeddingsBatchOutputTranslation:
assert body["usage"]["prompt_tokens"] == 2
assert body["usage"]["total_tokens"] == 2
+ def test_should_fall_back_to_documented_token_count_field(self, config):
+ (result,) = self._transform(
+ config,
+ [
+ self._vertex_embeddings_output_row(
+ response={
+ "embedding": {"values": [-0.015, 0.024]},
+ "tokenCount": "2",
+ }
+ )
+ ],
+ )
+
+ assert result["response"]["body"]["usage"]["prompt_tokens"] == 2
+
def test_should_resolve_model_from_managed_gcs_object_path(self, config):
object_path = urllib.parse.quote(
"litellm-vertex-files/publishers/google/models/gemini-embedding-2/"
@@ -1569,8 +1591,8 @@ class TestVertexEmbeddingsBatchOutputTranslation:
"status": "",
"processed_time": "2026-07-29T05:55:52.379528Z",
"response": {
- "tokenCount": "2",
"embedding": {"values": [-0.015, 0.024]},
+ "usageMetadata": {"promptTokenCount": 2},
},
}
],