Unicode, UTF-8, and text normalization
An optional technical deep dive into string representation
This page is a technical companion to the overview of the text-to-token path. It is not required to understand the general pipeline; it becomes useful when text looks identical on screen but produces different encodings or tokenization.
Three distinct layers
- Unicode defines code points and properties for representing text. A code point can be written, for example, as
U+00E9. - UTF-8 is an encoding that represents code points as bytes. A code point may require multiple bytes.
- Unicode normalization transforms some sequences into a canonical or compatibility form. It can bring equivalent strings into a common form, but must not be assumed: the policy depends on the preprocessing and tokenizer.
Unicode does not define token segmentation; UTF-8 does not choose the vocabulary; normalization is not universally applied in the same way.
Also distinguish a code point from a grapheme. A grapheme is roughly a unit perceived as one character by a reader; it can contain multiple code points (for example, a letter plus a combining accent, or an emoji sequence joined by a joiner character). In Python, len(string) counts code points, not necessarily the characters a person visually perceives.
Example: two encodings of “é”
The precomposed form contains one code point (U+00E9). The decomposed form contains e (U+0065) followed by a combining accent (U+0301). The visual rendering may be identical, but the code-point and byte sequences differ.
import unicodedata
composed = "é"
decomposed = "e\u0301"
print([f"U+{ord(char):04X}" for char in composed])
# ['U+00E9']
print(composed.encode("utf-8"))
# b'\xc3\xa9'
print([f"U+{ord(char):04X}" for char in decomposed])
# ['U+0065', 'U+0301']
print(decomposed.encode("utf-8"))
# b'e\xcc\x81'
print(unicodedata.normalize("NFC", composed) == unicodedata.normalize("NFC", decomposed))
# TrueNFC normalization makes these two strings equal in this example. This does not prove that a particular tokenizer applies NFC: check its documentation and test the exact version in use.
NFC/NFD concern canonical forms; NFKC/NFKD also apply certain compatibility equivalences. The latter can erase distinctions that matter in some domains (for example, typographic symbols or full-width forms). Do not choose a “stronger” normalization without checking the text requirements and the model’s contract.
What this implies—and what it does not
- Visually identical strings can have different representations before normalization.
- UTF-8 bytes are neither tokens nor vocabulary IDs.
- A tokenizer may normalize and pre-split text before applying segmentation rules; the exact steps depend on the implementation.
- The figure illustrates Unicode and UTF-8, not the output of a particular tokenizer. To inspect real tokens and IDs, run the exact tokenizer with its version and configuration.
Technical references
- UTF-8, RFC 3629 — specification of UTF-8 encoding.
- Unicode Standard Annex #15 — Unicode normalization forms.
- Official Unicode normalization charts — visual examples of normalized forms.
Return to the main route
The From text to tokens page returns to the conceptual level. The Tokenization strategies page compares algorithm families and their trade-offs.