tokie 0.3.0


pip install tokie

  Latest version

Released: Nov 12, 2026


Meta
Requires Python: >=3.10

Classifiers

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

tokie

GitHub · crates.io

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

Wheel compatibility matrix

Platform CPython 3.10 CPython 3.11 CPython 3.12 CPython 3.13 CPython 3.14
macosx_10_12_x86_64
macosx_11_0_arm64
manylinux2014_x86_64
manylinux_2_17_x86_64
manylinux_2_28_aarch64
pyemscripten_2026_0_wasm32
win_amd64

Files in release

tokie-0.3.0-cp310-cp310-macosx_10_12_x86_64.whl (2.1MiB)
tokie-0.3.0-cp310-cp310-macosx_11_0_arm64.whl (2.0MiB)
tokie-0.3.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (2.3MiB)
tokie-0.3.0-cp310-cp310-manylinux_2_28_aarch64.whl (2.2MiB)
tokie-0.3.0-cp310-cp310-win_amd64.whl (1.9MiB)
tokie-0.3.0-cp311-cp311-macosx_10_12_x86_64.whl (2.1MiB)
tokie-0.3.0-cp311-cp311-macosx_11_0_arm64.whl (2.0MiB)
tokie-0.3.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (2.3MiB)
tokie-0.3.0-cp311-cp311-manylinux_2_28_aarch64.whl (2.2MiB)
tokie-0.3.0-cp311-cp311-win_amd64.whl (1.9MiB)
tokie-0.3.0-cp312-cp312-macosx_10_12_x86_64.whl (2.1MiB)
tokie-0.3.0-cp312-cp312-macosx_11_0_arm64.whl (2.0MiB)
tokie-0.3.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (2.3MiB)
tokie-0.3.0-cp312-cp312-manylinux_2_28_aarch64.whl (2.2MiB)
tokie-0.3.0-cp312-cp312-win_amd64.whl (1.9MiB)
tokie-0.3.0-cp313-cp313-macosx_10_12_x86_64.whl (2.1MiB)
tokie-0.3.0-cp313-cp313-macosx_11_0_arm64.whl (2.0MiB)
tokie-0.3.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (2.3MiB)
tokie-0.3.0-cp313-cp313-manylinux_2_28_aarch64.whl (2.2MiB)
tokie-0.3.0-cp313-cp313-win_amd64.whl (1.9MiB)
tokie-0.3.0-cp314-cp314-macosx_10_12_x86_64.whl (2.1MiB)
tokie-0.3.0-cp314-cp314-macosx_11_0_arm64.whl (2.0MiB)
tokie-0.3.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (2.3MiB)
tokie-0.3.0-cp314-cp314-manylinux_2_28_aarch64.whl (2.2MiB)
tokie-0.3.0-cp314-cp314-pyemscripten_2026_0_wasm32.whl (506.5KiB)
tokie-0.3.0-cp314-cp314-win_amd64.whl (1.9MiB)
tokie-0.3.0.tar.gz (360.7KiB)
Extras: None
Dependencies:
numpy