fix: PGVector.add_embeddings silently truncates on length mismatch#310
Open
Humphrey (HumphreySun98) wants to merge 1 commit into
Conversation
`PGVector.add_embeddings` and `aadd_embeddings` built the SQL insert payload via `zip(texts, metadatas, embeddings, ids_)`, which silently truncates to the shortest argument. They then returned the full `ids_` list regardless of how many rows were actually written, so an upstream embedder bug that returned fewer embeddings than texts saw N IDs returned to the caller but only M rows persisted, with no exception raised. As called out in langchain-ai#300 (and observed downstream of langchain-google-genai's `gemini-embedding-2` regression in langchain-ai/langchain-google#1704). This change validates lengths up-front via `_validate_lengths_match` — raising a clear `ValueError` before any DB session is opened — and adds `strict=True` to the trailing `zip` calls as a backstop. Adds unit-test coverage that bypasses `PGVector.__init__` (no Postgres needed) for both sync and async paths. Fixes langchain-ai#300
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
`PGVector.add_embeddings` (and `aadd_embeddings`) build the SQL insert payload via:
```python
data = [
{...}
for text, metadata, embedding, id in zip(texts, metadatas, embeddings, ids_)
]
return ids_
```
Plain `zip` truncates to the shortest argument, then the method returns the full `ids_` list regardless. So when an upstream embedder bug returns fewer embeddings than texts — e.g. langchain-ai/langchain-google#1704's `gemini-embedding-2` regression — the caller sees N IDs but only M rows actually land in Postgres, with no exception. This silent data loss is exactly the symptom reported in #300.
This PR validates the input lengths up-front via a new `_validate_lengths_match` helper that raises a clear `ValueError` before any DB session is opened, and adds `strict=True` to the trailing `zip` calls as a backstop. Both sync and async paths are covered.
Relevant issues
Fixes #300
Changes
Testing
```
$ python -m pytest tests/unit_tests/v1/test_pgvector_length_validation.py -v
5 passed in 0.64s
```
Reverting only the `_validate_lengths_match` call and `strict=True` flag while keeping the new tests makes the mismatch tests fail back to silent truncation, confirming the regression coverage pins the buggy behavior. `ruff check` and `ruff format --check` pass.
Note
The same anti-pattern exists in `langchain_postgres/v2/async_vectorstore.py:297` (`for id, content, embedding, metadata in zip(ids, texts, embeddings, metadatas):`). v1 is the path explicitly called out in #300 and the path users hit through the deprecated `PGVector` import; happy to fix the v2 analog in a follow-up if you'd like it bundled differently.
Disclaimer: this PR was prepared with the assistance of an AI agent (Claude Code). All code and test changes were reviewed by the author before submission.