Repository navigation
fix(embeddings): send one Content per text to Gemini embed_content - #511
Open
pcbeingused33 wants to merge 1 commit into
Open
pcbeingused33 wants to merge 1 commit into
pcbeingused33 wants to merge 1 commit into
Conversation
gemini-embedding-2 aggregates a plain list of strings into a single embedding, so batches came back with one vector for N inputs. Wrap each text in its own Content and check the returned count.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
With
--embedding-mode geminiandgemini-embedding-2,compute_embeddings_geminireturns one vector per batch instead of one per text. Each batch is sent ascontents=[str, ...].gemini-embedding-001returns one embedding per string in that list, butgemini-embedding-2aggregates all the entries of a request into a single embedding. Google's embeddings docs (https://ai.google.dev/gemini-api/docs/embeddings) describe this, and the same behavior was measured against the API in redis/redis-vl-python#886. The result is an embeddings array whose row count doesn't match the passages, which nothing caught.This PR:
types.Content(parts=[types.Part(text=...)]). That gives one embedding per input on bothgemini-embedding-001andgemini-embedding-2, so nothing changes for 001 users.RuntimeErrorotherwise, instead of building a misaligned array.The
task_typeconfig from #488 is unchanged.Related Issues
None: no tracking issue. Same root cause as redis/redis-vl-python#886.
Tests
In
tests/test_gemini_embeddings.py(mockedgoogle.genaiclient, no network):test_gemini_batch_sends_one_content_per_text: the fake client acts likegemini-embedding-2(one vector for a plain list of strings, one perContentotherwise). The test asserts three texts give three distinct rows.test_gemini_batch_rejects_embedding_count_mismatch: one embedding returned for two inputs raises.Both fail on main and pass with this change. The existing task_type tests still pass.
ruff check/ruff format --checkare clean on the touched files.Checklist
uv run pytest tests/test_gemini_embeddings.py)ruff formatandruff check)pre-commit run --all-files)This change was prepared with the help of an AI coding assistant.