I searched my own notes for "naïve Bayes" and my RAG pipeline returned 3 documents. grep -ril on the same folder found 11.
Same corpus, same word, same machine. The retriever was not dumb and the embeddings were not bad. The problem was that half of my "ï" characters were one code point and the other half were two. That is Unicode normalization, and it had been quietly corrupting my search index for months.
This is the autopsy: what I measured, what I broke while fixing it, and the 12 lines that fixed it.
TL;DR
- The same visible text can be stored as different bytes. "é" is either one code point (NFC,
U+00E9) or two (NFD,e+U+0301). Python says they are not equal. - In my 41,200-chunk RAG index, 3,914 chunks (9.5%) were not in NFC, mostly from PDF extraction and filenames off an old HFS+ backup.
- The damage: exact-hash dedup missed 612 duplicate chunks, and my BM25 tokenizer split NFD words in half (
naïvebecamenai+ve). - Fix: run
unicodedata.normalize("NFC", text)on every document and every query before hashing, chunking, tokenizing or embedding. Map PDF ligatures likefiexplicitly. - Don't blanket-apply NFKC. It turns
x²intox2andH₂OintoH2O. Use it only for the search key, never for stored text.
What is Unicode normalization and why does it break search?
Unicode normalization is the process of converting text into one canonical byte sequence, because Unicode allows several sequences to render as the same character. If you skip it, two strings that look identical on screen will fail ==, produce different hashes, and tokenize differently.
Here is the whole bug in five lines of a Python REPL:
>>> a = "café" # NFC: é is one code point >>> b = "café" # NFD: e + combining acute accent >>> print(a, b) café café >>> a == b False >>> len(a), len(b) (4, 5) >>> a.encode(), b.encode() (b'caf\xc3\xa9', b'cafe\xcc\x81') Your terminal, your editor, and your LLM's chat UI all render these the same. Your SHA-256, your dict keys, your SQL WHERE, and your tokenizer do not.
Where did the NFD text in my corpus come from?
Two sources, neither of which I'd have guessed.
1. PDF text extraction. About 400 of my 2,870 documents are papers and vendor docs saved as PDFs. Depending on how the PDF was produced, the extractor hands back accented letters as base letter plus combining mark. It also hands back typographic ligatures: "configuration" arrives as configuration, where fi is a single code point, U+FB01. That word will never match a query typed on a keyboard.
2. Filenames from an old Mac backup. HFS+, the macOS filesystem before APFS, stored filenames in a decomposed form. A chunk of my notes came off an HFS+-era backup drive. My ingestion prepends the filename to each chunk as a title line, so every chunk from a file named "Café notes.md" carried an NFD "é" in its header, while the body (typed later on a newer machine) used NFC.
That second one is nasty because it gives you mixed normalization inside a single chunk.
How did I measure the damage?
I wrote a 20-line audit and ran it over every chunk in the index:
import unicodedata LIGATURES = range(0xFB00, 0xFB07) # ff fi fl ffi ffl long-st st def audit(text: str) -> dict: return { "not_nfc": not unicodedata.is_normalized("NFC", text), "ligature": any(ord(ch) in LIGATURES for ch in text), } unicodedata.is_normalized has been in the standard library since Python 3.8 and is much faster than normalizing and comparing.
Results on my index:
| Check | Chunks | Share |
|---|---|---|
| Total chunks | 41,200 | 100% |
| Not in NFC | 3,914 | 9.5% |
| Contains a Latin ligature | 1,106 | 2.7% |
| Byte-different but equal after NFC | 612 | 1.5% |
That last row is the dedup failure. My pipeline deduplicates chunks by hashing their text. 612 chunks were the same paragraph ingested twice (once from a PDF, once from my own notes quoting it), but the hashes differed, so both went into the vector store. On queries that hit those paragraphs, the duplicates took 2 of my top 5 retrieval slots, which means the LLM saw the same context twice and lost a slot for something useful.
Why did BM25 miss words like "naïve"?
My BM25 tokenizer split NFD words in half. I used the classic re.findall(r"\w+", text). In Python, \w matches characters where str.isalnum() is true, and a combining mark like U+0308 is not alphanumeric. So:
>>> import re >>> re.findall(r"\w+", "naïve") # NFC ['naïve'] >>> re.findall(r"\w+", "naïve") # NFD ['nai', 've'] The NFD document never indexed the token naïve. It indexed nai and ve. A query for "naïve Bayes", typed on a keyboard in NFC, could only match through "Bayes", and it lost to every document that matched both terms. Hybrid search didn't save me either, because the dense side ranked the duplicate NFC chunks above the NFD originals.
That's how 11 relevant files became 3 results.
What is the fix for Unicode normalization in a RAG pipeline?
Normalize to NFC at the boundary, on both sides: every document at ingestion and every query at search time. Then handle ligatures explicitly, because NFC leaves them alone.
import unicodedata _LIGATURE_MAP = str.maketrans({ "ff": "ff", "fi": "fi", "fl": "fl", "ffi": "ffi", "ffl": "ffl", "ſt": "st", "st": "st", }) def clean(text: str) -> str: return unicodedata.normalize("NFC", text).translate(_LIGATURE_MAP) def search_key(text: str) -> str: # only for BM25 tokens and dedup hashes, never stored or shown return unicodedata.normalize("NFKC", clean(text)).casefold() clean() runs before anything touches the text: chunking, hashing, embedding, storage. search_key() runs only to build BM25 tokens and dedup hashes. The query goes through the same two functions. Forgetting the query side is the classic half-fix: your index is pristine and users still type into a different normal form.
I re-ran ingestion only on the 3,914 affected chunks plus the 612 duplicates, which re-embedded about 4,500 chunks instead of the full 41,200.
Why not just use NFKC everywhere?
Because NFKC changes meaning, not just encoding. This is the part I got wrong first.
My first fix was one line: normalize("NFKC", text) on everything. It killed the ligatures for free, so I shipped it. Two days later I noticed an answer that said a formula used "x2" where my notes said "x²". NFKC folds "compatibility" characters into plain equivalents:
-
x²becomesx2 -
H₂ObecomesH2O -
½becomes1⁄2 -
™becomesTM - full-width
AbecomesA
For search keys that's exactly what you want: someone typing "H2O" should find "H₂O". For the text you store and feed to the model, it is data corruption. Hence the split: NFC for storage and context, NFKC plus casefold only for matching.
What broke after the fix?
Highlighting. My UI shows the matched passage with the query term highlighted, using character offsets saved at ingestion. Normalization changes string length (NFD "é" is 2 code points, NFC is 1), so every offset after an accented character in a re-ingested chunk drifted left by one per accent.
Before, a highlight on "Bayes" in an NFD chunk landed on "ayes " instead. I stopped storing offsets entirely and now find highlight spans at render time by re-matching the term against the stored NFC text. Offsets computed on one normal form are worthless on another. If you store spans anywhere (citations, highlights, NER annotations), normalize before you compute them.
Results after the fix
Same corpus, same retriever, same embedding model:
- "naïve Bayes": 3 results became 11, matching
grep. - Duplicate chunks in the store: 612 removed.
- My 60-query test set of queries with at least one non-ASCII term: relevant document in top 10 went from 35 of 60 to 56 of 60.
The 4 remaining misses are a different problem: transliteration. Someone writes "Muller" without the umlaut and my notes say "Müller". Normalization doesn't touch that, and I'm not convinced stripping diacritics globally is safe, so it's still open.
Total time: one evening of debugging, one afternoon re-ingesting, and two days running a quietly wrong NFKC build before I caught it.
So, can Unicode normalization break a RAG pipeline?
Yes. Unicode normalization differences make visually identical text fail equality, so exact-hash deduplication keeps duplicates, regex tokenizers split decomposed (NFD) words like naïve into fragments, and keyword search misses documents that contain the exact query term. In my 41,200-chunk index, 9.5% of chunks were not NFC, which cost 612 duplicate chunks and dropped non-ASCII query recall to 35 of 60. The fix is to apply unicodedata.normalize("NFC", ...) to every document and every query before chunking, hashing, tokenizing or embedding, map PDF ligatures explicitly, and reserve NFKC with casefolding for search keys only, never for the text you store or send to the model.
Written by the developer behind Preterview, an interview prep platform.
