Repository navigation
Conversation
OpenAIEmbeddingProvider now unit-normalizes embeddings so non-OpenAI OpenAI-compatible endpoints (e.g. Ark/doubao) satisfy the sqlite distance-to-similarity contract (same as FastEmbed/LiteLLM). Fixes basicmachines-co#1862 Signed-off-by: MbappeWU <MbappeWU@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
|
I have read the CLA Document and I hereby sign the CLA |
|
@CLAassistant check |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
|
|
@MbappeWU we'd like to merge your contribution. can you sign the CLA please? |
|
Thank you @MbappeWU for diagnosing #1862 and for this fix. We're shipping 1.0 tomorrow and couldn't merge without a signed CLA, so we re-implemented it in #1948, with credit to you in the commit and the PR. The CLA bot doesn't read comments; signing happens through the link in its message, so for future PRs please sign there. |
## Why The SQLite vector index converts L2 distance to cosine similarity with `1 - L²/2`, which holds only for unit vectors. OpenAI returns unit vectors. Other OpenAI-compatible endpoints, such as Volcengine Ark in #1862, don't, so every semantic match fell below the similarity floor and vector and hybrid search silently returned nothing. Fixes #1862. Credit: @MbappeWU diagnosed this and proposed the fix in #1869. That PR can't be merged without a signed CLA, and 1.0 ships tomorrow, so this re-implements it. ## What changed - A `unit_normalized` helper next to the embedding provider protocol. - The OpenAI provider normalizes every vector it receives. - The FastEmbed and LiteLLM providers already normalized with their own copies; both now use the helper. ## Verification - New `test_openai_provider_returns_unit_vectors`: the stub returns `[n, n+1, n+2]`, and the provider must return unit-length vectors in the same direction. It fails without the fix. - The concurrent-ordering test now compares normalized vectors. - Provider tests (OpenAI, FastEmbed, LiteLLM, prefixing): 95 passed. `just fast-check` is clean. Stored vectors: the provider identity is unchanged for api.openai.com, so those users re-embed nothing. With a custom `api_base` the identity gains a `:unit-normalized` marker, so the next index pass re-embeds the vectors that may be unnormalized. No manual reindex is needed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01CMYVL1x91B8kJxy7nn2GU8 --------- Signed-off-by: phernandez <paul@basicmachines.co> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Summary
OpenAIEmbeddingProvider.embed_documentsreturned API vectors as-is. Againstapi.openai.comthat is invisible (OpenAI returns unit vectors), but OpenAI-compatible endpoints such as Volcengine Ark / doubao can return unnormalized vectors. The sqlite distance→similarity formula (1 - L²/2) then scores every neighbor near zero, so semantic/hybrid search silently returns no hits.This is the same contract FastEmbed (#1023) and LiteLLM already honor.
Fixes #1862
Changes
OpenAIEmbeddingProvider.embed_documentsafter batches are assembled and before the dimension check (mirrorLiteLLMEmbeddingProvider).[3,4,0]→[0.6,0.8,0]; already-unit and zero vectors unchanged; concurrent batch order still preserved.Test plan
uv run pytest tests/repository/test_openai_provider.py -q→ 35 passeduv run ruff check/ruff format --checkon touched files → cleanNotes
[3,4,0]), then the full file suite passed.