UTF-8 Plumbing: Byte-level Tokenizers Unavoidably Enable LLMs to Generate Ill-formed UTF-8

Preston Firestone, Shubham Ugare, Gagandeep Singh, Sasa Misailovic

TL;DR

Large language models (LLMs) often use byte-level tokenizers to handle diverse text, but this can lead to "ill-formed UTF-8" sequences that break downstream code. This paper formally proves that byte-level tokenizers can always produce such ill-formed sequences, regardless of the model or vocabulary. This fundamental issue causes real-world bugs in LLM applications, and the paper explores mitigations and their limitations.

What Problem Does This Paper Address?

Modern LLMs rely on tokenizers to convert raw text into numerical representations that the models can process. While character-based tokenizers ensure valid text, they require massive vocabularies. Byte-level tokenizers, on the other hand, use a small, fixed vocabulary (256 bytes), avoiding out-of-vocabulary issues. However, this approach means that the sequences of bytes produced by the tokenizer, and subsequently by the LLM, are not guaranteed to form valid UTF-8 text. This "ill-formed UTF-8" can cause unexpected errors and incorrect behavior in applications that assume valid UTF-8 input, such as web servers, text editors, and even code generation systems. The paper aims to formally understand why this problem occurs and its implications.

Methods

The authors introduce a formal framework using monoid theory to describe tokenization. A monoid is a mathematical structure with a set of elements and an associative binary operation. In this context, sequences of bytes or tokens form monoids. They define tokenization as a pair of stochastic maps between these monoids: one map for cutting text into tokens (tokenizer, denoted by $\tau$) and another for joining tokens back into text (detokenizer, denoted by $\kappa$).

A key concept is homomorphism, a property where an operation on combined elements yields the same result as applying the operation separately and then combining. The paper proves that:

  • The detokenization map ($\kappa$) is a homomorphism, meaning joining tokens and then decoding is equivalent to decoding and then joining.
  • The tokenization map ($\tau$) is not a homomorphism. This is the core of the problem: cutting text into tokens and then decoding the tokens individually does not necessarily yield the same result as decoding the entire original text.

They also introduce the concept of "glitch tokens" and "visual spoofs" as related issues where tokenization can lead to unexpected model behavior or security vulnerabilities.

Datasets

The paper does not introduce new datasets for training or evaluation. Instead, it uses existing examples and concepts from the literature to illustrate its points. For instance, it references the Rigveda and Old Church Slavonic text examples from Figure 1 and Table 6 to demonstrate character and byte representations. The evaluation of SynCode's Unicode handling (Section E) uses a subset of the TweetEval emoji dataset (Barbieri et al., 2020).

Key Insights and Ideas

  • Leaky Abstraction: Byte-level tokenization exposes the underlying byte representation, which is usually hidden by the UTF-8 abstraction. This "leakage" is the root cause of the problem.
  • Non-Homomorphic Tokenization: The fundamental insight is that the process of tokenizing text (cutting it into pieces) is not a "homomorphism." This means that how you break down and reassemble text matters, and doing it incrementally (token by token) can lead to different results than processing the whole string at once.
  • UTF-8 is Not a Monoid Homomorphism: Interpreting byte sequences as UTF-8 is not a homomorphism. This means that even if you have a sequence of valid UTF-8 tokens, combining them might not result in a valid UTF-8 string if the original byte sequence was ill-formed.
  • Byte-Level Tokenizers are Inherently Problematic: The paper proves that any vocabulary containing tokens that are ill-formed UTF-8 will always be able to produce ill-formed UTF-8 sequences. This is a strong theoretical guarantee that the problem is unavoidable with such tokenizers.
  • Incremental Decoding Issues: A significant practical implication is that processing tokens incrementally (e.g., streaming output) can lead to different results than processing the entire sequence at once. This is because ill-formed byte sequences might be handled differently at each step.

Key Results

  • Formal Proof: The paper provides a formal proof using monoid theory that byte-level tokenizers can always generate ill-formed UTF-8 sequences.
  • Real-World Bugs: The authors connect their theoretical findings to practical issues observed in major LLM serving engines (like Hugging Face TGI, vLLM, OpenLLM) and constrained generation systems (like SynCode). These systems have historically failed to handle non-ASCII characters correctly due to this problem.
  • Mitigation Evaluation: The paper evaluates a common mitigation strategy: replacing ill-formed byte sequences with a replacement character (U+FFFD). While this helps, it doesn't fully restore the homomorphism property and can still lead to incorrect outputs.
  • SynCode Improvement: An experiment with the SynCode system showed a significant improvement in handling Unicode characters when switching from character-level to byte-level constraints, reducing crash rates from 100% to 0% and increasing accuracy from 0% to 62% on an emoji prediction task.

Limitations and Future Work

  • Inherent Limitation: The paper acknowledges that the problem of generating ill-formed UTF-8 is inherent to byte-level tokenizers and cannot be fully "fixed" without changing the tokenizer's fundamental approach.
  • Partial Fix: Algorithm 1 (incremental decoding) is presented as a partial fix that helps in certain scenarios but has inherent limitations.
  • Future Work: The authors suggest several avenues for future research:
  • Surveying more foundation models and their tokenization strategies.
  • Testing LLM applications more thoroughly with non-ASCII characters.
  • Expanding Algorithm 1 to handle concatenation in both directions (not just appending).
  • Experimenting with multiple constrained generation algorithms.
  • Investigating the conjecture that the number of bytes per character influences tokenization splits.
  • Exploring how to process all natural languages uniformly, which remains an open challenge.

Summary updated

BibTeX
@inproceedings{firestone2025utf,
  author = {Preston Firestone and Shubham Ugare and Gagandeep Singh and Sasa Misailovic},
  title = {{UTF-8 Plumbing: Byte-level Tokenizers Unavoidably Enable LLMs to Generate Ill-formed UTF-8}},
  booktitle = {COLM},
  year = {2025},
  url = {https://openreview.net/forum?id=8ExXncFpf6},
}
Figure