Programming Language
- Rust
- Python :: Implementation :: CPython
- Python :: 3
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
- Python :: 3.14
Topic
- Scientific/Engineering :: Artificial Intelligence
- Text Processing
- Text Processing :: Linguistic
Intended Audience
- Developers
- Science/Research
License
- OSI Approved :: MIT License
- OSI Approved :: Apache Software License
Development Status
- 4 - Beta
Operating System
- OS Independent
Typing
- Typed
Python bindings for the tokie Rust tokenizer. The tokie Python package is 0.3.0 and wraps the Rust tokie 0.2.0 crate. encode, encode_plus, and batch release the GIL and return NumPy arrays. There is no Encoding object and no .ids attribute; use .tolist() only to print or assert.
decode and decode_bytes take the array returned by encode, encode_plus, or a slice of batch's flat uint32 ids. Pass it directly; do not call .tolist() first. The array must be one-dimensional, native-endian, C-contiguous, and aligned uint32. Lists, tuples, and arrays of another dtype are rejected; 0.2.0 also accepted lists.
Installation
pip install -U tokie
Pin a floor with pip install "tokie>=0.3.0" when a resolver needs one.
Quick start
import tokie
tokenizer = tokie.Tokenizer.from_pretrained("openai-community/gpt2")
# from_json(path) and from_file(path) are aliases for open(path).
ids = tokenizer.encode("Hello world") # uint32 array, template off
print(ids.tolist())
print(tokenizer.decode(ids))
print(tokenizer.decode_bytes(ids)) # raw bytes
# The single-sequence template. GPT-2 has none, so this matches encode.
print(tokenizer.encode_plus("Hello world").tolist())
flat, ends = tokenizer.batch(["Hello world", "a12b"]) # ends are cumulative
print(tokenizer.count_tokens("Hello world")) # template off
print(tokenizer.count_tokens_batch(["Hello world", "a12b"]))
print(tokenizer.vocab_size) # highest id + 1
print(tokenizer.pad_token_id) # [PAD] then <pad>
print(tokenizer.num_special_tokens_to_add(is_pair=False))
tokenizer.save("gpt2.tokie")
encode leaves the template off. encode_plus applies it and accepts pair, truncate, and pad; truncation is from the right, longest sequence first, and padding is fixed on the right. One max_len covers both and counts the template ids.
from_pretrained opens exactly one .tokie file from the repo, whatever its name. A repo with no .tokie file is parsed from tokenizer.json; several .tokie files is an error. There is no runtime .tkz fallback: a genuine legacy TOKI image is refused by magic, not by extension, while a JSON file merely named .tkz is parsed as JSON. open reads either file from disk by its magic, from_json(path) and from_file(path) are path aliases for open, and json(text) parses JSON text.
decode returns str (lossy only where needed). decode_bytes returns raw bytes. Both take the uint32 array as their first argument and keep specials by default; skip=True omits them. For ids built by hand, convert explicitly: numpy.asarray(values, dtype=numpy.uint32). That is your conversion, not an API fallback. For an arbitrary existing array use numpy.array(values, dtype=numpy.uint32, copy=True, order="C"); numpy.ascontiguousarray does not fix an already C-contiguous but misaligned buffer. get_vocab() returns raw token strings and may have fewer entries than vocab_size.
Benchmark
bench.py times Tokenizer.encode on one file against the tokie bench command, which is the Rust path.
License
MIT OR Apache-2.0