Skip to content

fix(core): L2-normalize OpenAI-compatible embedding vectors - #1869

Closed
MbappeWU wants to merge 1 commit into
basicmachines-co:mainfrom
MbappeWU:fix/1862-openai-embed-l2-normalize
Closed

MbappeWU wants to merge 1 commit into
basicmachines-co:mainfrom
MbappeWU:fix/1862-openai-embed-l2-normalize

Conversation

@MbappeWU

Copy link
Copy Markdown
Contributor

Summary

OpenAIEmbeddingProvider.embed_documents returned API vectors as-is. Against api.openai.com that 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

  • L2-normalize each vector in OpenAIEmbeddingProvider.embed_documents after batches are assembled and before the dimension check (mirror LiteLLMEmbeddingProvider).
  • Zero vectors stay zero; positive norms become unit vectors.
  • Regression tests: unnormalized [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 passed
  • uv run ruff check / ruff format --check on touched files → clean
  • CI

Notes

  • Red→green: the new L2 test failed before the production edit (provider returned raw [3,4,0]), then the full file suite passed.

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>
@MbappeWU

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@MbappeWU

Copy link
Copy Markdown
Contributor Author

@CLAassistant check

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-25T19:20:10.478616Z
We couldn’t complete this review. Comment @codex review to try again.
3ee05e8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@phernandez phernandez added the cla needed The contributor needs to sign the CLA label Oct 27, 2026
@phernandez

Copy link
Copy Markdown
Member

@MbappeWU we'd like to merge your contribution. can you sign the CLA please?

@phernandez

Copy link
Copy Markdown
Member

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.

@phernandez phernandez closed this Oct 29, 2026
phernandez added a commit that referenced this pull request Oct 29, 2026
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla needed The contributor needs to sign the CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI-compatible embedding provider: unnormalized vectors from non-OpenAI endpoints (Ark/doubao) silently break semantic search

3 participants