Skip to content

fix: lowercase text for bge-small-zh-v1.5 (capitalized words became [UNK]) - #940

Open
chaitanya0305 wants to merge 1 commit into
qdrant:mainfrom
chaitanya0305:fix/bge-small-zh-lowercase
Open

chaitanya0305 wants to merge 1 commit into
qdrant:mainfrom
chaitanya0305:fix/bge-small-zh-lowercase

Conversation

@chaitanya0305

Copy link
Copy Markdown

What is wrong

BAAI/bge-small-zh-v1.5 turns every capitalized Latin word into [UNK]. Its WordPiece vocabulary has no uppercase Latin letters, but its tokenizer does not lowercase: do_lower_case is false in its tokenizer_config.json, unlike bge-base-zh-v1.5 and bge-large-zh-v1.5, and the BertNormalizer in tokenizer.json has lowercase: false. This holds for both the Qdrant/bge-small-zh-v1.5 export and the Xenova CoreML copy. sentence-transformers lowercases the text before tokenizing ("do_lower_case": true in the model's sentence_bert_config.json), so the reference embedding is computed on lowercased text.

text fastembed tokens cosine with sentence-transformers
Apple iPhone 售价 [CLS] [UNK] [UNK] 售 价 [SEP] 0.805
The quick brown fox jumps over the lazy dog. [CLS] [UNK] quick ... 0.991
apple iphone 售价 / 今天天气很好 identical 1.000

Any brand, product name or sentence-initial word in mixed Chinese/English text is affected, and distinct names collapse to the same vector (also reported upstream for the plain transformers path in FlagOpen/FlagEmbedding#1617). The canonical vector in the tests did not catch it because it is computed on lowercase text.

I checked every text, late-interaction and cross-encoder model fastembed lists: this is the only one whose sentence_bert_config.json lowercases while the tokenizer fastembed downloads does not. mxbai-edge-colbert-v0-17m is the other one, and it already lowercases in Python.

Fix

  • New field DenseModelDescription.lowercase (default False), set for BAAI/bge-small-zh-v1.5.
  • OnnxTextEmbedding.tokenize and _token_count lowercase the text in Python when the flag is set, like MxbaiEdgeColbert, and not through a tokenizer normalizer, for the reasons given there. Token counts and length-sorted batching therefore see the same tokens the model does.
  • Worker processes (parallel) and the CoreML path go through the same tokenize, so they are covered too. I checked parallel=2 and CoreMLExecutionProvider by hand: both now match the lowercase text.

Tests

test_lowercases_text_for_bge_small_zh embeds "Apple iPhone 售价" and checks it against the sentence-transformers values (SentenceTransformer("BAAI/bge-small-zh-v1.5").encode(..., normalize_embeddings=True)[:5]), against the lowercase text, and that the token counts match. It fails on main ([-0.0137, 0.0196, ...] vs [-0.0038, 0.0409, ...]) and passes with the fix. tests/test_text_onnx_embeddings.py and tests/test_custom_models.py pass (37 passed). ruff, ruff format and mypy are clean on the changed files.

Canonical values snippet:

from sentence_transformers import SentenceTransformer
SentenceTransformer("BAAI/bge-small-zh-v1.5").encode(["Apple iPhone 售价"], normalize_embeddings=True)[0][:5]
# [-0.00379998  0.04094673  0.0439145   0.03307618 -0.03430465]

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  • Does your submission pass the existing tests?
  • Have you added tests for your feature?
  • Have you installed pre-commit with pip3 install pre-commit and set up hooks with pre-commit install? (ran the same hooks, ruff check and ruff format, directly)

The vocabulary of BAAI/bge-small-zh-v1.5 has no uppercase latin letters,
but its tokenizer doesn't lowercase (do_lower_case false in its
tokenizer_config.json, unlike bge-base-zh and bge-large-zh), so every
capitalized latin word became [UNK]. sentence-transformers lowercases
the text first (do_lower_case in sentence_bert_config.json).
"Apple iPhone 售价" had cosine 0.80 with the sentence-transformers
embedding.

Add a lowercase flag to DenseModelDescription, set it for
bge-small-zh-v1.5 and lowercase the text before tokenization and token
counting, as mxbai-edge-colbert does.
@chaitanya0305
chaitanya0305 requested a review from joein as a code owner December 4, 2026 01:08
@coderabbitai

coderabbitai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

DenseModelDescription adds a lowercase option that defaults to False. BGE small Chinese v1.5 enables the option. OnnxTextEmbedding lowercases input before tokenization and token counting when the option is enabled. A test checks mixed-case embedding output against a canonical vector and lowercase input, and compares token counts.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: anush008

Merge Risk: Minimal · up to 8549e

This change lowercases input text for BAAI/bge-small-zh-v1.5 so that capitalized Latin words are no longer turned into unknown tokens. Other models are unaffected. No merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: lowercasing text for bge-small-zh-v1.5 to fix capitalized Latin words becoming [UNK]. The raw summary and objectives both describe this lowercase preprocessing change …
Description check Passed The description explains the bug (the tokenizer does not lowercase, so capitalized Latin words become [UNK]), the fix (a lowercase flag, applied in tokenize and _token_count), and the test that covers…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: lowercasing text for bge-small-zh-v1.5 to fix capitalized Latin words becoming [UNK]. The raw summary and objectives both describe this lowercase preprocessing change in OnnxTextEmbedding.tokenize and the new DenseModelDescription.lowercase flag.

Full details: Description check

Explanation

The description explains the bug (the tokenizer does not lowercase, so capitalized Latin words become [UNK]), the fix (a lowercase flag, applied in tokenize and _token_count), and the test that covers it. It matches the changeset closely.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4ec31d1b-4fa2-4de5-9be9-f9eab942d716
Commits

Reviewing files that changed from the base of the PR and between ac9ab5c and 8549e61.

Files selected for processing: 3

  • fastembed/common/model_description.py
  • fastembed/text/onnx_embedding.py
  • tests/test_text_onnx_embeddings.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant