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): fan array embeddings input out into one vertex row per element

milan627d2755da56
diff --git a/litellm/llms/vertex_ai/files/transformation.py b/litellm/llms/vertex_ai/files/transformation.py
index 1a5d40dabb2..78a16b002e7 100644
--- a/litellm/llms/vertex_ai/files/transformation.py
+++ b/litellm/llms/vertex_ai/files/transformation.py
@@ -66,7 +66,7 @@ from litellm.types.llms.openai import (
PathLike,
)
from litellm.types.files import StreamingMediaUploadConfig
-from litellm.types.llms.vertex_ai import GcsBucketResponse
+from litellm.types.llms.vertex_ai import GcsBucketResponse, GeminiEmbeddingInput
from litellm.types.utils import (
Embedding,
EmbeddingResponse,
@@ -87,6 +87,7 @@ _EMBED_REQUEST_FIELD_BY_GEMINI_PARAM = {
"taskType": "task_type",
"title": "title",
}
+_VERTEX_BATCH_FANNED_OUT_KEY_PATTERN = re.compile(r"(?P<custom_id>.*)#(?P<index>\d+)/(?P<total>\d+)")
def _sanitize_gcp_label_value(value: str) -> str:
@@ -222,46 +223,93 @@ def _openai_batch_output_row(
}
-def _transform_vertex_embeddings_batch_output_row_to_openai(
- vertex_output_row: Mapping[str, Any],
+def _split_vertex_batch_key(vertex_output_row: Mapping[str, Any]) -> tuple[str, int]:
+ """
+ Resolve `(custom_id, index within that custom_id)` for a Vertex batch output row.
+
+ A `/v1/embeddings` entry whose `input` is an array fans out into one Vertex row per
+ element, tagged `<custom_id>#<index>/<total>` (see `_vertex_batch_embeddings_key`),
+ so the rows can be reassembled into a single OpenAI response.
+ """
+ key = _get_litellm_batch_custom_id(vertex_output_row)
+ match = _VERTEX_BATCH_FANNED_OUT_KEY_PATTERN.fullmatch(key)
+ if match is None or int(match["total"]) < 2:
+ return key, 0
+ return match["custom_id"], int(match["index"])
+
+
+def _vertex_embeddings_rows_to_openai_batch_output_row(
+ custom_id: str,
+ vertex_output_rows: tuple[Mapping[str, Any], ...],
model: str | None,
) -> Mapping[str, Any]:
"""
- Transforms one Vertex Gemini Embedding batch output row into an OpenAI batch
- output row holding an `/v1/embeddings` response body.
+ Transforms the Vertex Gemini Embedding batch output rows belonging to one OpenAI
+ batch entry into an OpenAI batch output row holding an `/v1/embeddings` response.
Example Vertex jsonl
{"key": "id_1", "request": {...}, "response": {"embedding": {"values": [-0.015, 0.024]}, "usageMetadata": {"promptTokenCount": 2}}}
- 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.
+ An entry that asked for several embeddings at once maps to several rows here, which
+ become the indexed elements of a single `data` array. One failed element fails the
+ whole entry, since an OpenAI batch row is either a response or an error. Live rows
+ report usage under `usageMetadata`; the documented `tokenCount` is kept as a
+ fallback. Rows carry no `modelVersion`, so the model comes from the batch they
+ belong to.
"""
- custom_id = _get_litellm_batch_custom_id(vertex_output_row)
- status = vertex_output_row.get("status", "")
+ status = next((row["status"] for row in vertex_output_rows if row.get("status")), "")
if status:
return _openai_batch_output_row(
custom_id=custom_id,
error={"code": "vertex_ai_error", "message": status},
)
- vertex_response = vertex_output_row.get("response") or {}
- usage_metadata = vertex_response.get("usageMetadata") or {}
- token_count = int(usage_metadata.get("promptTokenCount") or vertex_response.get("tokenCount") or 0)
+ responses = tuple(row.get("response") or {} for row in vertex_output_rows)
+ token_count = sum(
+ int((response.get("usageMetadata") or {}).get("promptTokenCount") or response.get("tokenCount") or 0)
+ for response in responses
+ )
body = EmbeddingResponse(
model=model or "",
data=[
Embedding(
- embedding=vertex_response["embedding"]["values"],
- index=0,
+ embedding=response["embedding"]["values"],
+ index=index,
object="embedding",
)
+ for index, response in enumerate(responses)
],
usage=Usage(prompt_tokens=token_count, total_tokens=token_count),
).model_dump()
return _openai_batch_output_row(custom_id=custom_id, body=body)
+def _transform_vertex_embeddings_batch_output_to_openai(
+ vertex_output_rows: Iterable[Mapping[str, Any]],
+ model: str | None,
+) -> tuple[Mapping[str, Any], ...]:
+ """
+ Transforms a whole Vertex Gemini Embedding batch output into OpenAI batch output
+ rows, one per OpenAI batch entry, in the order the entries first appear.
+
+ Rows are grouped rather than mapped one to one because a single entry can fan out
+ into several Vertex rows, and Vertex returns them in arbitrary order.
+ """
+ keyed_rows = tuple((_split_vertex_batch_key(row), row) for row in vertex_output_rows)
+ grouped_rows = {
+ custom_id: tuple(row for _, row in group)
+ for custom_id, group in itertools.groupby(sorted(keyed_rows, key=lambda kr: kr[0]), key=lambda kr: kr[0][0])
+ }
+ return tuple(
+ _vertex_embeddings_rows_to_openai_batch_output_row(
+ custom_id=custom_id,
+ vertex_output_rows=grouped_rows[custom_id],
+ model=model,
+ )
+ for custom_id in dict.fromkeys(custom_id for (custom_id, _), _ in keyed_rows)
+ )
+
+
def _model_from_managed_gcs_url(url: str) -> str | None:
"""
Extracts the model from a LiteLLM-managed Vertex batch GCS url.
@@ -290,21 +338,49 @@ def _is_embeddings_batch_entry(openai_entry: Mapping[str, Any]) -> bool:
return path == "embeddings" or path.endswith("/embeddings")
-def _openai_batch_jsonl_entry_to_vertex_embeddings_row(
+def _openai_embedding_input_elements(
+ embedding_input: GeminiEmbeddingInput,
+) -> tuple[Union[str, List[str]], ...]:
+ """
+ Split an OpenAI `input` into the elements that each get their own embedding.
+
+ A string is one embedding, a flat array is one embedding per element, and a nested
+ array is one combined embedding per inner array, matching the online
+ `batchEmbedContents` path.
+ """
+ if isinstance(embedding_input, list):
+ return tuple(embedding_input)
+ return (embedding_input,)
+
+
+def _vertex_batch_embeddings_key(custom_id: str, index: int, total: int) -> str:
+ """
+ The top-level `key` Vertex echoes back on an embeddings row.
+
+ An entry asking for several embeddings needs several Vertex rows, so its key also
+ carries the element index and the group size; `_split_vertex_batch_key` reads them
+ back out. Entries asking for a single embedding keep their bare `custom_id`.
+ """
+ return custom_id if total < 2 else f"{custom_id}#{index}/{total}"
+
+
+def _openai_batch_jsonl_entry_to_vertex_embeddings_rows(
openai_entry: Mapping[str, Any],
-) -> Mapping[str, Any]:
+) -> tuple[Mapping[str, Any], ...]:
"""
- Transforms a single OpenAI `/v1/embeddings` batch entry into a Vertex Gemini
- Embedding batch row.
+ Transforms a single OpenAI `/v1/embeddings` batch entry into Vertex Gemini Embedding
+ batch rows, one per requested embedding.
Example Vertex jsonl
{"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`) 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.
+ `key`. An `EmbedContentRequest` returns exactly one vector, so an entry whose `input`
+ is an array fans out into one row per element and is reassembled on the way back.
+ 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
"""
@@ -313,37 +389,58 @@ def _openai_batch_jsonl_entry_to_vertex_embeddings_row(
if embedding_input is None:
raise ValueError("`input` is required on /v1/embeddings batch requests, but was not provided")
- embed_content_request = transform_openai_input_gemini_embed_content(
- input=embedding_input,
- model=openai_request_body.get("model", ""),
- optional_params=openai_request_body,
- )
- 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
- }
+ elements = _openai_embedding_input_elements(embedding_input)
+ if not elements:
+ raise ValueError("`input` on /v1/embeddings batch requests must not be empty")
+ embed_content_requests = tuple(
+ transform_openai_input_gemini_embed_content(
+ input=element,
+ model=openai_request_body.get("model", ""),
+ optional_params=openai_request_body,
+ )
+ for element in elements
+ )
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_request_fields},
- }
+ return tuple(
+ {
+ **(
+ {}
+ if custom_id is None
+ else {
+ _VERTEX_BATCH_KEY_FIELD: _vertex_batch_embeddings_key(
+ custom_id=str(custom_id),
+ index=index,
+ total=len(embed_content_requests),
+ )
+ }
+ ),
+ "request": {
+ "content": embed_content_request["content"],
+ **{
+ 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
+ },
+ },
+ }
+ for index, embed_content_request in enumerate(embed_content_requests)
+ )
-def _openai_batch_jsonl_entry_to_vertex_wrapped_request(
+def _openai_batch_jsonl_entry_to_vertex_rows(
openai_entry: Dict[str, Any],
map_openai_to_vertex_params: Callable[[Dict[str, Any]], Dict[str, Any]],
-) -> Mapping[str, Any]:
+) -> tuple[Mapping[str, Any], ...]:
"""
- Transforms a single OpenAI JSONL batch entry into its Vertex wrapped request.
+ Transforms a single OpenAI JSONL batch entry into the Vertex rows it maps to.
jsonl body for vertex is {"request": <request_body>}
Example Vertex jsonl
{"request":{"contents": [{"role": "user", "parts": [{"text": "What is the relation between the following video and image samples?"}, {"fileData": {"fileUri": "gs://cloud-samples-data/generative-ai/video/animals.mp4", "mimeType": "video/mp4"}}, {"fileData": {"fileUri": "gs://cloud-samples-data/generative-ai/image/cricket.jpeg", "mimeType": "image/jpeg"}}]}]}}
"""
if _is_embeddings_batch_entry(openai_entry):
- return _openai_batch_jsonl_entry_to_vertex_embeddings_row(openai_entry)
+ return _openai_batch_jsonl_entry_to_vertex_embeddings_rows(openai_entry)
openai_request_body = openai_entry.get("body") or {}
vertex_request_body = _transform_request_body(
@@ -361,7 +458,7 @@ def _openai_batch_jsonl_entry_to_vertex_wrapped_request(
vertex_request_body["labels"] = {}
_set_litellm_batch_custom_id_labels(vertex_request_body["labels"], custom_id)
- return {"request": vertex_request_body}
+ return ({"request": vertex_request_body},)
def _iter_stripped_lines(raw_lines: Iterable[Union[str, bytes]]) -> Iterator[str]:
@@ -459,10 +556,10 @@ class _OpenAIToVertexBatchUploadStream(BaseFileUploadStream):
def _iter_vertex_jsonl_chunks(self) -> Iterator[bytes]:
first = True
for entry in _iter_openai_jsonl_entries(self._openai_file_content):
- wrapped = _openai_batch_jsonl_entry_to_vertex_wrapped_request(entry, self._map_openai_to_vertex_params)
- prefix = b"" if first else b"\n"
- first = False
- yield prefix + json.dumps(wrapped).encode("utf-8")
+ for wrapped in _openai_batch_jsonl_entry_to_vertex_rows(entry, self._map_openai_to_vertex_params):
+ prefix = b"" if first else b"\n"
+ first = False
+ yield prefix + json.dumps(wrapped).encode("utf-8")
def iter_bytes(self) -> Iterator[bytes]:
return self._iter_vertex_jsonl_chunks()
@@ -914,25 +1011,29 @@ class VertexAIFilesConfig(VertexBase, BaseFilesConfig):
request=httpx.Request(method="POST", url="https://example.com"),
)
+ all_lines = itertools.chain([first_line], lines)
+
+ # Embedding rows are grouped by `custom_id` rather than transformed one at a
+ # time, since an entry that asked for several embeddings comes back as
+ # several rows, in arbitrary order.
+ if _is_vertex_embeddings_batch_output_row(first_row):
+ openai_outputs = _transform_vertex_embeddings_batch_output_to_openai(
+ vertex_output_rows=(json.loads(line) for line in all_lines),
+ model=model,
+ )
+ return b"\n".join(json.dumps(openai_output).encode("utf-8") for openai_output in openai_outputs)
+
# Transform each row straight into the output buffer, so peak memory
# stays at ~one row plus the output. If any row fails, return the
# original content unchanged.
output = bytearray()
- for line in itertools.chain([first_line], lines):
+ for line in all_lines:
try:
- vertex_output_row = json.loads(line)
- openai_output = (
- _transform_vertex_embeddings_batch_output_row_to_openai(
- vertex_output_row=vertex_output_row,
- model=model,
- )
- if _is_vertex_embeddings_batch_output_row(vertex_output_row)
- else self._transform_single_vertex_batch_output_to_openai(
- vertex_output=vertex_output_row,
- vertex_gemini_config=vertex_gemini_config,
- logging_obj=batch_transform_logging_obj,
- mock_httpx_response=mock_httpx_response,
- )
+ openai_output = self._transform_single_vertex_batch_output_to_openai(
+ vertex_output=json.loads(line),
+ vertex_gemini_config=vertex_gemini_config,
+ logging_obj=batch_transform_logging_obj,
+ mock_httpx_response=mock_httpx_response,
)
except Exception:
return content
diff --git a/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_streaming.py b/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_streaming.py
index 2e3280c0ed1..957fc7dbcf4 100644
--- a/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_streaming.py
+++ b/tests/test_litellm/llms/vertex_ai/files/test_vertex_ai_files_streaming.py
@@ -37,7 +37,7 @@ from litellm.llms.vertex_ai.files.transformation import (
_get_litellm_batch_custom_id_from_labels,
_iter_openai_jsonl_entries,
_iter_openai_jsonl_lines,
- _openai_batch_jsonl_entry_to_vertex_wrapped_request,
+ _openai_batch_jsonl_entry_to_vertex_rows,
)
from litellm.types.llms.openai import CreateFileRequest
@@ -84,8 +84,9 @@ def _reference_vertex_jsonl_string(cfg: VertexAIFilesConfig, content: str) -> st
transform, so the streaming path can be checked against it for parity."""
entries = [json.loads(line) for line in content.splitlines() if line.strip()]
return "\n".join(
- json.dumps(_openai_batch_jsonl_entry_to_vertex_wrapped_request(entry, cfg._map_openai_to_vertex_params))
+ json.dumps(row)
for entry in entries
+ for row in _openai_batch_jsonl_entry_to_vertex_rows(entry, cfg._map_openai_to_vertex_params)
)
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 3eff3083220..73d1d6eeb5a 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
@@ -15,7 +15,7 @@ from unittest.mock import MagicMock
from litellm.llms.vertex_ai.files.transformation import (
VertexAIFilesConfig,
_get_litellm_batch_custom_id_from_labels,
- _openai_batch_jsonl_entry_to_vertex_wrapped_request,
+ _openai_batch_jsonl_entry_to_vertex_rows,
_sanitize_gcp_label_value,
)
from litellm.types.llms.openai import OpenAIFileObject, HttpxBinaryResponseContent
@@ -1054,14 +1054,15 @@ class TestTryTransformDoesNotMutateCallerLoggingObj:
def _wrap_entries(openai_jsonl_content):
- """Vertex-wrapped requests for a list of OpenAI batch entries, built via the
- live single-entry transform that the streaming upload path uses."""
+ """Vertex rows for a list of OpenAI batch entries, built via the live
+ single-entry transform that the streaming upload path uses."""
cfg = VertexAIFilesConfig()
return [
- _openai_batch_jsonl_entry_to_vertex_wrapped_request(
+ row
+ for entry in openai_jsonl_content
+ for row in _openai_batch_jsonl_entry_to_vertex_rows(
entry, cfg._map_openai_to_vertex_params
)
- for entry in openai_jsonl_content
]
@@ -1424,6 +1425,86 @@ class TestVertexEmbeddingsBatchInputTranslation:
with pytest.raises(ValueError, match="`input` is required"):
_wrap_entries([_embeddings_entry(body={"model": "gemini-embedding-2"})])
+ def test_should_raise_when_input_empty(self):
+ with pytest.raises(ValueError, match="must not be empty"):
+ _wrap_entries(
+ [_embeddings_entry(body={"model": "gemini-embedding-2", "input": []})]
+ )
+
+ def test_should_fan_an_input_array_out_into_one_row_per_element(self):
+ """
+ An `EmbedContentRequest` returns exactly one vector, so an OpenAI entry asking
+ for several embeddings needs several Vertex rows.
+ """
+ rows = _wrap_entries(
+ [
+ _embeddings_entry(
+ body={
+ "model": "gemini-embedding-001",
+ "input": ["first", "second"],
+ "dimensions": 768,
+ }
+ )
+ ]
+ )
+
+ assert rows == [
+ {
+ "key": "request-1#0/2",
+ "request": {
+ "content": {"parts": [{"text": "first"}]},
+ "output_dimensionality": 768,
+ },
+ },
+ {
+ "key": "request-1#1/2",
+ "request": {
+ "content": {"parts": [{"text": "second"}]},
+ "output_dimensionality": 768,
+ },
+ },
+ ]
+
+ def test_should_keep_the_bare_custom_id_for_single_element_arrays(self):
+ (row,) = _wrap_entries(
+ [
+ _embeddings_entry(
+ body={"model": "gemini-embedding-2", "input": ["only one"]}
+ )
+ ]
+ )
+
+ assert row["key"] == "request-1"
+
+ def test_should_combine_a_nested_input_into_one_multipart_row(self):
+ """Nested arrays are the combined-embedding shape, as on the online path."""
+ (row,) = _wrap_entries(
+ [
+ _embeddings_entry(
+ body={
+ "model": "gemini-embedding-2",
+ "input": [
+ [
+ "a caption",
+ "gs://cloud-samples-data/generative-ai/image/benchmark.jpeg",
+ ]
+ ],
+ }
+ )
+ ]
+ )
+
+ assert row["key"] == "request-1"
+ assert row["request"]["content"]["parts"] == [
+ {"text": "a caption"},
+ {
+ "file_data": {
+ "mime_type": "image/jpeg",
+ "file_uri": "gs://cloud-samples-data/generative-ai/image/benchmark.jpeg",
+ }
+ },
+ ]
+
def test_should_keep_chat_completions_lines_on_generate_content_path(self):
(row,) = _wrap_entries(
[
@@ -1569,6 +1650,95 @@ class TestVertexEmbeddingsBatchOutputTranslation:
"request-2",
]
+ def test_should_reassemble_a_fanned_out_input_array_into_one_row(self, config):
+ """Vertex returns the rows of one entry in arbitrary order."""
+ (result,) = self._transform(
+ config,
+ [
+ self._vertex_embeddings_output_row(
+ key="request-1#1/2",
+ response={
+ "embedding": {"values": [0.3, 0.4]},
+ "usageMetadata": {"promptTokenCount": 5},
+ },
+ ),
+ self._vertex_embeddings_output_row(
+ key="request-1#0/2",
+ response={
+ "embedding": {"values": [0.1, 0.2]},
+ "usageMetadata": {"promptTokenCount": 3},
+ },
+ ),
+ ],
+ )
+
+ assert result["custom_id"] == "request-1"
+ assert result["response"]["body"]["data"] == [
+ {"embedding": [0.1, 0.2], "index": 0, "object": "embedding"},
+ {"embedding": [0.3, 0.4], "index": 1, "object": "embedding"},
+ ]
+ assert result["response"]["body"]["usage"]["prompt_tokens"] == 8
+
+ def test_should_keep_fanned_out_entries_apart_and_in_file_order(self, config):
+ results = self._transform(
+ config,
+ [
+ self._vertex_embeddings_output_row(key="request-2#0/2"),
+ self._vertex_embeddings_output_row(key="request-1"),
+ self._vertex_embeddings_output_row(key="request-2#1/2"),
+ ],
+ )
+
+ assert [result["custom_id"] for result in results] == ["request-2", "request-1"]
+ assert len(results[0]["response"]["body"]["data"]) == 2
+ assert len(results[1]["response"]["body"]["data"]) == 1
+
+ def test_should_fail_the_whole_entry_when_one_of_its_rows_failed(self, config):
+ """An OpenAI batch row is either a response or an error, never both."""
+ (result,) = self._transform(
+ config,
+ [
+ self._vertex_embeddings_output_row(key="request-1#0/2"),
+ self._vertex_embeddings_output_row(
+ key="request-1#1/2", status="Quota exceeded", response={}
+ ),
+ ],
+ )
+
+ assert result["custom_id"] == "request-1"
+ assert result["response"] is None
+ assert result["error"]["message"] == "Quota exceeded"
+
+ def test_should_end_to_end_round_trip_a_fanned_out_embeddings_batch(self, config):
+ first_row, second_row = _wrap_entries(
+ [
+ _embeddings_entry(
+ custom_id="MyRequest-1",
+ body={
+ "model": "gemini-embedding-2",
+ "input": ["hello world", "goodbye world"],
+ },
+ )
+ ]
+ )
+
+ (result,) = self._transform(
+ config,
+ [
+ {
+ **row,
+ "status": "",
+ "response": {"embedding": {"values": values}},
+ }
+ for row, values in ((second_row, [0.3]), (first_row, [0.1]))
+ ],
+ )
+
+ assert result["custom_id"] == "MyRequest-1"
+ assert [
+ embedding["embedding"] for embedding in result["response"]["body"]["data"]
+ ] == [[0.1], [0.3]]
+
def test_should_end_to_end_round_trip_openai_embeddings_batch(self, config):
(vertex_row,) = _wrap_entries(
[