#7805·chroma

[BUG] TypeScript BM25 hasher hashes UTF-16 code units, so non-ASCII term ids differ from Python and Rust

Author: feiiiiii5Created Sep 25, 2026Updated Sep 25, 2026

Description

The TypeScript BM25 embedding function computes different term ids from the Python and Rust clients for any token containing a character above U+00FF, so a sparse index written by one client silently misses non-ASCII terms when queried from another.

Murmur3AbsHasher.murmur3 in clients/new-js/packages/ai-embeddings/chroma-bm25/src/index.ts reads key.charCodeAt(i) & 0xff and folds key.length into the final mix. Both operate on UTF-16 code units. MurmurHash3 is defined over bytes, so for a token like café this hashes a different byte string than the reference implementations do.

The other two clients hash UTF-8:

  • chromadb/utils/embedding_functions/schemas/bm25_tokenizer.py:268 uses mmh3.hash, which UTF-8-encodes a str.
  • rust/chroma/src/embed/murmur3_abs_hasher.rs:22 passes token.as_bytes(), and its doc comment states the intent: "This matches Python's abs(mmh3.hash(token)) behavior".

The package's own test file declares this compatibility as its contract. src/index.test.ts:38 says the second test "mirrors rust test test_bm25_stopwords_and_punctuation to guarantee compatibility", and its expectedIndices are copied from the Rust test. Every fixture in the file is ASCII, which is why the mismatch has not shown up.

Impact

A term's id is that term's identity in a shared sparse index. Index a collection with chroma_bm25 from Python or Rust, then query it from the TypeScript client, and every non-ASCII query term hashes to an id that is not in the index. The query returns fewer or no results with no error. The reverse direction fails the same way.

The affected range is not exotic. A Latin-1 character such as é is one UTF-16 code unit but two UTF-8 bytes, so accented Latin (café, naïve, Müller, Español) diverges, as do CJK, Cyrillic, Greek, Hebrew, Arabic, Devanagari and emoji.

@chroma-core/chroma-bm25 is published and not private, so this reaches users on npm.

Reproduction

Hashing one token each way, offline, with no network or credentials. The TypeScript column is the current implementation; the other is abs(mmh3.hash(token, seed=0)) for the token as the tokenizer produces it, lowercased and stemmed:

text TypeScript today Python / Rust
café 1721337931 605818632 mismatch
naïve 756638508 756638508 matches by luck, see below
Müller 1069828929 1613901631 mismatch
Español 1566715335 589134877 mismatch
日本語 450827494 1515949417 mismatch
Привет 124712451 993413998 mismatch

An end-to-end check through the public API, one document, before the fix:

document: 'Le café est naïve 日本語'
  python / rust indices: [342821761, 605818632, 744779191, 992511445, 1515949417]
  typescript  indices:   [342821761, 450827494, 744779191, 1268694621, 1721337931]

naïve deserves a note. It is listed as matching by coincidence: the stemmer reduces it to naïv, and for that particular string the UTF-16-code-unit and UTF-8 byte sequences happen to produce the same hash. That is luck, not agreement, and it does not hold for the other five.

Environment

  • @chroma-core/chroma-bm25 0.1.9
  • Node v23.11.0, pnpm 10.15.0
  • main at e122af79fd42216d6319ff248914a22142c4ab49

Fix

Encode the token to UTF-8 bytes first and hash those, leaving the rest of the function unchanged. new TextEncoder().encode(key) gives a Uint8Array, and indexing it yields bytes rather than code units, so the same arithmetic produces the reference hash. key.length becomes len, the byte length, which is what the h1 ^= len final mix needs.

No existing test changes: every token in the current fixtures is ASCII, so its UTF-8 encoding is byte-identical to its code units.