Tokenizers is a Python library providing fast and versatile tokenization tools, optimized for both research and production environments. The current version is 0.22.2, released on January 5, 2026. The library is actively maintained with regular updates to enhance performance and add features.
pip install tokenizersVerified import paths — ran on the pinned version, not inferred.
A simple example demonstrating how to load a pretrained tokenizer and tokenize a sample text.
Use Python 3.12 or earlier for installation; Python 3.13 is not supported due to PyO3 compatibility issues.
Use 'from tokenizers import Tokenizer' to import the Tokenizer class.
pip install tokenizers
Install Rust (e.g., using `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`) and ensure it's in your system's PATH before retrying `pip install tokenizers`.
Ensure the specified path points directly to a `tokenizer.json` file or a directory recognized as a valid model by the `tokenizers` library, or use a correct Hugging Face model identifier.
Ensure the input to `tokenizer.encode_batch()` is a list of strings, or for single input, ensure `tokenizer.encode()` receives a single string. Example: `tokenizer.encode_batch(['text1', 'text2'])`.
Access the token IDs via `encoding.ids`, attention mask via `encoding.attention_mask`, and token type IDs via `encoding.type_ids`. For example, `input_ids = encoding.ids`.