Skip to content

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

Merged
phernandez merged 4 commits into
mainfrom
fix/1862-normalize-openai-embeddings
Oct 29, 2026
Merged

phernandez merged 4 commits into
mainfrom
fix/1862-normalize-openai-embeddings

Conversation

@phernandez

@phernandez phernandez commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

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.ai/code/session_01CMYVL1x91B8kJxy7nn2GU8

The SQLite vector index turns L2 distance into cosine similarity with
1 - L^2/2, which holds only for unit vectors. OpenAI returns unit
vectors, but other OpenAI-compatible endpoints (Volcengine Ark in #1862)
do not, so every semantic match fell below the similarity floor and
vector and hybrid search returned nothing.

Normalize in the OpenAI provider, through one unit_normalized helper
that the FastEmbed and LiteLLM providers now share instead of their own
copies.

Fixes #1862. The normalization fix was first proposed by @MbappeWU in
#1869; this re-implements it so it can ship in 1.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMYVL1x91B8kJxy7nn2GU8
Signed-off-by: phernandez <paul@basicmachines.co>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 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 ✅ Completed 2026-10-29T05:22:09.396079Z b19327f Manual request
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fcf562bbdb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…oints

Normalizing vectors did not change the stored-vector identity, so vectors
already stored unnormalized from a custom endpoint stayed as they were
and semantic search kept matching nothing until a full reindex.

The OpenAI provider now has an explicit identity: unchanged for
api.openai.com, which always returned unit vectors, and marked as
unit-normalized for a custom api_base. The configured identity uses the
same function, so the next index pass re-embeds only the affected
setups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMYVL1x91B8kJxy7nn2GU8
Signed-off-by: phernandez <paul@basicmachines.co>
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9d2880a95

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…r identity

The OpenAI SDK falls back to OPENAI_BASE_URL when no api_base is
configured, so a custom endpoint chosen that way kept the old identity
and its unnormalized vectors were never re-embedded. Both the runtime
and configured identities now use the endpoint the SDK will call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMYVL1x91B8kJxy7nn2GU8
Signed-off-by: phernandez <paul@basicmachines.co>
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 99ca316c14

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

An api_base or OPENAI_BASE_URL pointing at https://api.openai.com/v1 is
still the official service, whose vectors were always unit length.
Recognize its host so those users do not re-embed their whole corpus on
upgrade.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMYVL1x91B8kJxy7nn2GU8
Signed-off-by: phernandez <paul@basicmachines.co>
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: b19327fa74

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@phernandez
phernandez merged commit c45da4e into main Oct 29, 2026
88 of 89 checks passed
@phernandez
phernandez deleted the fix/1862-normalize-openai-embeddings branch October 29, 2026 06:07
phernandez added a commit that referenced this pull request Oct 29, 2026
## Why

`just release v1.0.0` pre-flight-checks for a `## v1.0.0` heading on
`main`, so the changelog has to land first.

## What changed

`CHANGELOG.md`: the Unreleased section becomes `## v1.0.0 (2026-10-29)`.
Its existing items are kept, and the 60 or so commits since #1871 are
added:

- **Breaking:**
- the MCP surface is only `bm_*`, with the old tools deleted (#1944,
#1947, #1959, #1962–#1964)
  - `project` is required (#1934)
  - the ChatGPT adapters moved to Cloud
  - `bm tool` mirrors the tools (#1943)
  - the POSIX verbs are rebuilt and `bm tail` is removed (#1960)
  - cloud sync/bisync are removed (#1953)
  - `bm schema` project resolution (#1959)
  - LiteLLM is an optional extra
- **Features:** text-file line ranges in `bm_read`; chat imports return
their file paths.
- **Bug fixes:** including the three found in the 1.0 live test (#1971,
#1972, #1974), with contributor credits for @Flynn-Kawolsky (#1957),
@SWSAmor (#1951) and @MbappeWU (#1948).
- **Security:** the CodeRabbit deep-scan hardening batch.

I checked the entries against PR bodies rather than titles, and
corrected #1954, #1948 and #1967 to describe what they actually fixed.

The date is today; change it if the tag lands tomorrow.

🤖 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

None yet

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

1 participant