fix(genai): resolve single aggregated embedding bug for gemini-embedding models#1817
Open
Sai Teja Bandaru (saitejabandaru-in) wants to merge 6 commits into
Conversation
…ing models Unlike traditional text embeddings (e.g. text-embedding-004), multimodal gemini-embedding models (such as gemini-embedding-2) treat list inputs in embed_content as parts of a single aggregated multimodal document, returning exactly one vector regardless of how many strings are passed. This change checks if the target model is a gemini-embedding model, and if so, runs individual embeds in parallel using a ThreadPoolExecutor (sync path) and asyncio.gather (async path) to correctly return a distinct embedding for each document in the input list, aligning with the LangChain Embeddings interface spec.
Changes standard unit test MODEL_NAME to text-embedding-004 to maintain coverage for standard list batching. Adds dedicated sync and async tests targeting gemini-embedding-2-preview to verify the new parallel ThreadPoolExecutor and asyncio.gather execution paths and ensure regression safety.
…ing models Unlike traditional text embeddings (e.g. text-embedding-004), multimodal gemini-embedding models (such as gemini-embedding-2) treat list inputs in embed_content as parts of a single aggregated multimodal document, returning exactly one vector regardless of how many strings are passed. This change checks if the target model is a gemini-embedding model, and if so, runs individual embeds in parallel using a ThreadPoolExecutor (sync path) and asyncio.gather (async path) to correctly return a distinct embedding for each document in the input list, aligning with the LangChain Embeddings interface spec.
Changes standard unit test MODEL_NAME to text-embedding-004 to maintain coverage for standard list batching. Adds dedicated sync and async tests targeting gemini-embedding-2-preview to verify the new parallel ThreadPoolExecutor and asyncio.gather execution paths and ensure regression safety.
…ing models Unlike traditional text embeddings (e.g. text-embedding-004), multimodal gemini-embedding models (such as gemini-embedding-2) treat list inputs in embed_content as parts of a single aggregated multimodal document, returning exactly one vector regardless of how many strings are passed. This change checks if the target model is a gemini-embedding model, and if so, runs individual embeds in parallel using a ThreadPoolExecutor (sync path) and asyncio.gather (async path) to correctly return a distinct embedding for each document in the input list, aligning with the LangChain Embeddings interface spec.
Changes standard unit test MODEL_NAME to text-embedding-004 to maintain coverage for standard list batching. Adds dedicated sync and async tests targeting gemini-embedding-2-preview to verify the new parallel ThreadPoolExecutor and asyncio.gather execution paths and ensure regression safety.
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.
Description
This Pull Request resolves a critical integration issue (#37728) where calling
GoogleGenerativeAIEmbeddings.embed_documents(oraembed_documents) withgemini-embedding-2always returns a list of exactly 1 vector, regardless of how many documents are passed.Root Cause
Unlike traditional text embeddings (e.g.,
text-embedding-004), multimodal gemini-embedding models (such asgemini-embedding-2) treat list inputs in standardembed_contentcalls as parts of a single aggregated multimodal document (designed for cross-modal retrieval, combining text/images/video/etc.). Consequently, they return exactly one merged vector for the entire batch.Solution
gemini-embeddingmodel. If so, it embeds each document individually in parallel to prevent aggregation:ThreadPoolExecutorfor concurrent network requests.asyncio.gatherfor non-blocking concurrent awaits.text-embedding-004) to maximize network efficiency for those models.text-embedding-004to preserve test coverage of the traditional batching logic.test_embed_documents_gemini_embedding_2andtest_aembed_documents_gemini_embedding_2unit tests targetinggemini-embedding-2-previewto verify correct multi-call dispatching and output reconstruction.