Skip to content

fix(embeddings): one request per text for the Vertex single-content models - #4204

Merged
nicoloboschi merged 1 commit into
mainfrom
fix/gemini-vertex-single-content
Sep 8, 2026
Merged

fix(embeddings): one request per text for the Vertex single-content models#4204
nicoloboschi merged 1 commit into
mainfrom
fix/gemini-vertex-single-content

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

The bug

test-api (1/3) is red on main and on every open PR, on the live Vertex test added by #4001:

FAILED tests/test_gemini_embeddings.py::test_gemini_embedding_2_vertexai_one_vector_per_input
  ValueError: The embedContent API for this model only supports one content at a time.
  google/genai/models.py:6460

It is not a test problem. GeminiEmbeddings.encode sends each text as its own Content — that is #4001's fix, and it is what stops a multimodal model fusing a whole batch into one vector. The SDK now refuses more than one Content for most of the Vertex family: embed_content routes every model whose name contains gemini, except gemini-embedding-001, and every maas model to the single-content embedContent endpoint, and raises client-side before anything reaches the network.

So an install configured with gemini-embedding-2-preview on Vertex fails every retain and every recall, not just the test.

The fix

Batching is the only free variable — each text still has to be its own Content to come back as its own vector — so for those models encode issues one request per text. gemini-embedding-001, the non-gemini Vertex models (text-embedding-005, …) and the entire Gemini API (non-Vertex) path keep the configured batch size.

The model predicate is mirrored from google.genai._transformers.t_is_vertex_embed_content_model rather than imported: it is private, and a copy that drifts fails loudly here — the SDK raises — instead of silently sending batches that never worked.

Tests

Six parametrized cases counting the requests encode actually issues for three texts, against a mocked client:

model Vertex requests
gemini-embedding-2-preview yes 3
google/gemini-embedding-2-preview yes 3
text-multilingual-maas-002 yes 3
gemini-embedding-001 yes 1
text-embedding-005 yes 1
gemini-embedding-2-preview no 1

They assert the per-request contents lengths, so a regression shows up as three texts back in one call rather than as an exception — the fix is the request shape. The live test from #4001 is unchanged and should go green in the CI jobs that carry GCP credentials.

ruff and ty clean; pytest tests/test_gemini_embeddings.py — 46 passed, 1 skipped (the live test, no local credentials).

…odels

`GeminiEmbeddings.encode` sends each text as its own Content, which is what
stops a multimodal model fusing a batch into one vector (#4001). On Vertex the
SDK now refuses that outright for most of the family: `embed_content` routes
every model whose name contains `gemini` — bar `gemini-embedding-001` — and
every `maas` model to the single-content `embedContent` endpoint, and raises

    ValueError: The embedContent API for this model only supports one content
    at a time.

client-side for anything longer. So an install configured with
`gemini-embedding-2-preview` on Vertex fails every retain and every recall
before a request leaves the process, and `test_api` has been red on the live
test that covers it.

Batching is the only free variable — each text still has to be its own Content
to come back as its own vector — so these models get one text per request.
`gemini-embedding-001`, the non-gemini Vertex models, and the whole Gemini API
path keep the configured batch size.

The predicate is mirrored from `google.genai._transformers`, not imported: it
is private, and a copy that drifts fails loudly here (the SDK raises) rather
than silently sending batches that never worked.
@nicoloboschi
nicoloboschi merged commit b2a7257 into main Sep 8, 2026
111 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant