Skip to main content

Python bindings wrapping the bpe-openai Rust tokenizer crate with a tiktoken-compatible API.

Project description

bpe-openai

CI Release Wheels

bpe-openai is a tiktoken-compatible tokenizer API backed by the Rust bpe-openai crate. Pathlit maintains the Python wrapper, while the core tokenizer implementation is developed by GitHub and published in the rust-gems project. Published wheels bundle the tokenizer data, so the package works out of the box on Python 3.9 and newer.

Installation

pip install bpe-openai

Installing from a source distribution requires a Rust toolchain:

pip install --no-binary bpe-openai bpe-openai==<version>

Quick start

import bpe_openai as bpe

enc = bpe.get_encoding("cl100k_base")
tokens = enc.encode("Smoke tests keep releases honest.")
print(tokens)
print(enc.decode(tokens))

# Model-aware helper
chat_enc = bpe.encoding_for_model("gpt-4o")

Compatibility snapshot

API / Feature Status Notes
get_encoding, encoding_for_model Supports cl100k_base, o200k_base, voyage3_base, and related models
Encoding.encode, Encoding.decode Rust backend ensures parity with tiktoken
Encoding.encode_batch Matches tiktoken's batching behaviour
Custom special tokens ⚠️ Not yet configurable at runtime
Legacy GPT-2 / r50k / p50k encodings ⚠️ Planned; current focus is on modern OpenAI models
Metrics hook (set_metrics_hook) Emits model, token count, latency, backend version

Legend: ✅ fully supported · ⚠️ partial / planned

Why we built it

Long, repetitive prompts can hit pathological slow paths in tiktoken. To stress-test both libraries we encoded inputs of the form "A" * n and measured latency. bpe-openai stays effectively flat while tiktoken grows sharply with input length, unlocking workloads like prompt templating and log chunking that stalled with the reference implementation.

Encoding time vs. input length

Smoke test

After installing from PyPI, run a quick confidence check:

python - <<'PY'
import bpe_openai as bpe
enc = bpe.get_encoding("cl100k_base")
text = "Smoke tests keep releases honest."
tokens = enc.encode(text)
assert enc.decode(tokens) == text
print("✅ bpe-openai smoke test passed.")
PY

Repository layout

  • python/ – Python package wrapping the Rust tokenizer as a CPython extension.
  • rust/ – Rust crate that loads tokenizer specs and exposes the fast BPE APIs.
  • scripts/ – Helper utilities for benchmarking, parity checks, and data sync.
  • vendor/ – Vendored tokenizer definitions sourced from GitHub's upstream release.

Development

python -m venv .venv
source .venv/bin/activate
python scripts/sync_tokenizer_data.py  # ensures tokenizer assets are copied
pip install maturin
cd python
maturin develop --release
pytest

The repository includes scripts/sync_tokenizer_data.py to copy the vendored .tiktoken.gz files into python/bpe_openai/data/ before building wheels or an sdist.

Release process (maintainers)

  1. Update the version string in python/pyproject.toml. The runtime fallback reads the same file, so one change keeps packaging metadata and bpe_openai.__version__ in sync.
  2. Run python scripts/sync_tokenizer_data.py to refresh bundled assets.
  3. Commit the changes and tag the release (git tag vX.Y.Z).
  4. Push the tag. The Release Wheels GitHub Actions workflow builds wheels, publishes a GitHub release, and uploads to PyPI via Trusted Publishing.

Contributing

Contributions are warmly welcomed—whether you are filing a bug, improving parity with new OpenAI models, or squeezing out more performance. Open an issue or pull request and the Pathlit team will review quickly.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bpe_openai-0.1.3.tar.gz (7.5 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bpe_openai-0.1.3-cp39-abi3-win_amd64.whl (41.4 MB view details)

Uploaded CPython 3.9+Windows x86-64

bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (41.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

bpe_openai-0.1.3-cp39-abi3-macosx_11_0_arm64.whl (42.0 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file bpe_openai-0.1.3.tar.gz.

File metadata

  • Download URL: bpe_openai-0.1.3.tar.gz
  • Upload date:
  • Size: 7.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bpe_openai-0.1.3.tar.gz
Algorithm Hash digest
SHA256 837caac932460c6d25e0f47df63c29a91a5892c32ea2df447721c5dccfdc5860
MD5 c21538ab9c80657b22dc095542bb0995
BLAKE2b-256 7705b16dbea7251cc3a97ab4a15dab29a3747af8bb8eeb62b74414153146dd31

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_openai-0.1.3.tar.gz:

Publisher: release-wheels.yml on Pathlit-Inc/bpe-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_openai-0.1.3-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: bpe_openai-0.1.3-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 41.4 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bpe_openai-0.1.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 629fb26d1fe39a70b39554ebcacfaed305f3fd4321af9d4fe2aa2487f382d1a8
MD5 e3e547a038834ae8207bf796270e36d1
BLAKE2b-256 1ef1c62865407f12eecdc10a73f7eef4d1c88201f5025d25933fc4b00cf54ced

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_openai-0.1.3-cp39-abi3-win_amd64.whl:

Publisher: release-wheels.yml on Pathlit-Inc/bpe-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a1698150fc30bc2fe5921d5114e4f96646c3d41fab47fb2e4a61bfffeae82bbf
MD5 7dae6877a8a5e961b0ae367234c02179
BLAKE2b-256 f20f3ef5056124e4e4bc921715a4e75a6d6a4d92c767ba57c93575decc386cee

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-wheels.yml on Pathlit-Inc/bpe-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 70139513460c030582650486191a3077fbd96e1d0e7b750ac769633921bbc66c
MD5 81fde3f87e7dc047717d98c2e7de3eb1
BLAKE2b-256 fe41ccdccf6e7437882753f87d820e6ca7bc1125d34d6d8a86e9f52042952fbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_openai-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release-wheels.yml on Pathlit-Inc/bpe-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_openai-0.1.3-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bpe_openai-0.1.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e86495dad7b8c3ebdcaa9e01d635311b1e19c8f8acfa2dc1d94e4b7ad711270f
MD5 be5a1fa5846231b8285efa6d8fdfa6a7
BLAKE2b-256 fb7709a2c8f59c829442336e20cc6c0f8e667cff9d8180fae754be6b0c3e1902

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_openai-0.1.3-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release-wheels.yml on Pathlit-Inc/bpe-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page