Tokenizers documentation
Decoders
Decoders
DecodeStream
Provides incremental decoding of token IDs as they are generated, yielding decoded text chunks as soon as they are available.
Unlike batch decoding, streaming decode is designed for use with autoregressive generation — tokens arrive one at a time and the decoder needs to handle multi-byte sequences (e.g., UTF-8 characters split across token boundaries) and byte-fallback tokens gracefully.
The decoder internally buffers tokens until it can produce a valid UTF-8 string
chunk, then yields that chunk and advances its internal state. This means
individual calls to step() may return
None when the current token completes a partial sequence that cannot yet
be decoded.
Example:
>>> from tokenizers import Tokenizer
>>> from tokenizers.decoders import DecodeStream
>>> tokenizer = Tokenizer.from_pretrained("gpt2")
>>> stream = DecodeStream(skip_special_tokens=True)
>>> # Simulate streaming token-by-token generation
>>> token_ids = tokenizer.encode("Hello, streaming world!").ids
>>> for token_id in token_ids:
... chunk = stream.step(tokenizer, token_id)
... if chunk is not None:
... print(chunk, end="", flush=True)step
( tokenizer id ) → Optional[str]
Parameters
- tokenizer (Tokenizer) — The tokenizer whose decoder pipeline will be used.
- id (
intorList[int]) — The next token ID, or a list of token IDs to append to the stream.
Returns
Optional[str]
The next decoded text chunk if enough tokens have
accumulated, or None if more tokens are still needed.
Add the next token ID (or list of IDs) to the stream and return the next decoded text chunk if one is available.
Because some characters span multiple tokens (e.g. multi-byte UTF-8
sequences or byte-fallback tokens), this method may return None
when the provided token does not yet complete a decodable unit. Callers
should simply continue feeding tokens until a non-None value is
returned.
BPEDecoder
ByteFallback
ByteFallback Decoder
ByteFallback is a decoder that handles tokens representing raw bytes in the
<0xNN> format (e.g., <0x61> for the byte 0x61 = 'a'). It converts
such tokens to their corresponding bytes and attempts to decode the resulting byte
sequence as UTF-8. This is used in LLaMA/SentencePiece models that use byte fallback
for unknown characters. Inconvertible byte tokens are replaced with the Unicode
replacement character (U+FFFD).
ByteLevel
ByteLevel Decoder
This decoder is to be used in tandem with the ByteLevel pre-tokenizer. It reverses the byte-to-unicode mapping applied during pre-tokenization, converting the special Unicode characters back into the original bytes to reconstruct the original string.
CTC
class tokenizers.decoders.CTC
( pad_token = '<pad>' word_delimiter_token = '|' cleanup = True )
Parameters
- pad_token (
str, optional, defaults to<pad>) — The pad token used by CTC to delimit a new token. - word_delimiter_token (
str, optional, defaults to|) — The word delimiter token. It will be replaced by a - cleanup (
bool, optional, defaults toTrue) — Whether to cleanup some tokenization artifacts. Mainly spaces before punctuation, and some abbreviated english forms.
CTC Decoder
Fuse
Fuse Decoder
Fuse simply concatenates every token into a single string without any separator. This is typically the last step in a decoder chain when other decoders need to operate on individual tokens before they are joined together.
Metaspace
class tokenizers.decoders.Metaspace
( )
Parameters
- replacement (
str, optional, defaults to▁) — The replacement character. Must be exactly one character. By default we use the ▁ (U+2581) meta symbol (Same as in SentencePiece). - prepend_scheme (
str, optional, defaults to"always") — Whether to add a space to the first word if there isn’t already one. This lets us treat hello exactly like say hello. Choices: “always”, “never”, “first”. First means the space is only added on the first token (relevant when special tokens are used or other pre_tokenizer are used).
Metaspace Decoder
Replace
Replace Decoder
This decoder is to be used in tandem with the Replace normalizer or a similar replace operation. It reverses a string replacement by substituting the replacement content back with the original pattern.
Sequence
Sequence Decoder
Chains multiple decoders together, applying them in order. Each decoder in the sequence processes the output of the previous one, allowing complex decoding pipelines to be built from simpler components.
Strip
Strip Decoder
Strips a given number of occurrences of a character from the left and/or right side of each token. This is useful for removing padding characters or special prefix/suffix markers added during tokenization.