Skip to content

fix: validate embedding dimensions before pgvector writes#1670

Open
ai-ag2026 wants to merge 2 commits into
vectorize-io:mainfrom
ai-ag2026:fix/validate-retain-embedding-dimensions
Open

fix: validate embedding dimensions before pgvector writes#1670
ai-ag2026 wants to merge 2 commits into
vectorize-io:mainfrom
ai-ag2026:fix/validate-retain-embedding-dimensions

Conversation

@ai-ag2026
Copy link
Copy Markdown

@ai-ag2026 ai-ag2026 commented May 20, 2026

Summary

  • validate embedding vectors before they can reach pgvector writes
  • reject empty vectors and vectors whose length does not match the backend's declared dimension
  • cover both retain and consolidation observation creation paths with regression tests

Root cause

Retain embedding generation already guaranteed that the number of returned vectors matched the number of input texts, but it did not validate per-vector dimensions. If an embedding backend returned [] or another wrong-sized vector, the invalid value could flow downstream until PostgreSQL/pgvector raised different vector dimensions 384 and 0.

The same shared embedding utility is also used by consolidation when creating observations, so the validation boundary protects both retain writes and consolidation-created observations.

Related reconnaissance

Test plan

  • RED on current origin/main: uv run pytest tests/test_consolidation_embedding_validation.py::test_create_observation_rejects_zero_length_embedding_before_insert -q -o addopts= failed because the zero-length vector reached the DB insert path.
  • uv run pytest tests/test_consolidation_embedding_validation.py::test_create_observation_rejects_zero_length_embedding_before_insert tests/test_retain_orchestrator_mapping.py -q -o addopts= → 11 passed
  • uv run ruff check tests/test_consolidation_embedding_validation.py hindsight_api/engine/retain/embedding_utils.py tests/test_retain_orchestrator_mapping.py → passed
  • Added-line secret/private marker scan → 0 findings

@ai-ag2026 ai-ag2026 changed the title fix: validate retain embedding dimensions fix: validate embedding dimensions before pgvector writes May 20, 2026
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.

batch_retain can pass zero-length embeddings to pgvector (vector(384) vs vector(0))

1 participant