Python bindings wrapping the bpe-openai Rust tokenizer crate with a tiktoken-compatible API.
Project description
bpe-openai
This directory contains the Python package that exposes the Rust bpe-openai
tokenizer via PyO3 bindings. The package is a thin wrapper over the Rust crate;
the compiled extension must be built before running tests or using the API.
Local development
python -m venv .venv
source .venv/bin/activate
pip install -r requirements-dev.txt # optional, see below
maturin develop --release
pytest
Running maturin develop --release builds the Rust extension in-place so the
package can be imported locally.
Packaging
./python/scripts/build_wheels.sh
Requirements:
- Docker daemon access (the script exits if it cannot talk to
/var/run/docker.sock). - Sufficient permissions to pull
quay.io/pypa/manylinux2014_x86_64(override withMANYLINUX_IMAGEif needed). - Optional: set
MANYLINUX_PYTHONto pick a different interpreter inside the container (defaults to/opt/python/cp39-cp39/bin/python3.9).
The container installs maturin on the fly and runs maturin build --release,
producing wheels under python/target/wheels/ that meet manylinux requirements
and therefore run on both old and new glibc releases.
Additional scripts are mirrored at the repo root (scripts/compare_with_tiktoken.py,
scripts/benchmark_scaling.py) for convenience when working outside the
python/ directory.
Parity Comparison
To compare this package’s behaviour with upstream tiktoken, install
tiktoken in a virtual environment alongside the wheel and run:
./python/scripts/compare_with_tiktoken.py
The command prints per-model encode/decode parity summaries and highlights any
differences. Pass --json to capture machine-readable output.
To inspect performance scaling, run:
./python/scripts/benchmark_scaling.py --batch-size 32 --output python/target/benchmark_scaling.png
The script now captures both single-document and batched (encode_batch) timings for
tiktoken and bpe_openai across the requested input lengths and, when matplotlib is installed,
plots the results (a thin wrapper lives at the repo root under scripts/benchmark_scaling.py).
tiktoken Compatibility Snapshot
| Feature / API | Support Status | Notes |
|---|---|---|
encoding_for_model |
⚠️ | Works for backend-exposed models (cl100k_base, o200k_base family, voyage3_base); legacy GPT-2 / p50k families are skipped |
get_encoding |
⚠️ | Supports cl100k_base, o200k_base, voyage3_base; legacy GPT-2 / r50k / p50k encodings not yet implemented |
Encoding.encode |
✅ | Calls the Rust bpe-openai tokenizer for exact parity |
Encoding.decode |
✅ | Mirrors tiktoken behaviour (UTF-8 validation included) |
Encoding.encode_batch / decode_batch |
✅ | Batch helpers mirror tiktoken signatures |
| Unsupported model errors | ✅ | Raises UnsupportedModelError with supported_models list |
| Custom special tokens | ❌ Not yet | Registering extra special tokens is not supported |
| Chunk limit enforcement | ✅ | Uses tokenizer counts and raises TokenLimitError |
Metrics hook (set_metrics_hook) |
✅ | Emits model, latency, token counts |
| Rust-backed performance parity | ✅ | Wheels/binaries use the upstream Rust implementation |
| Quickstart smoke test (drop-in replacement) | ✅ | Existing scripts run unmodified in tests |
Legend: ✅ fully supported • ⚠️ requires follow-up (documented limitation) • ❌ not yet implemented |
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bpe_openai-0.1.0.tar.gz.
File metadata
- Download URL: bpe_openai-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e90b5b4b3f9030b394148557136a8e7bf0bd40179354de2c26adeff40f08ada7
|
|
| MD5 |
036124b4df6d539e61da5b2dc64fe522
|
|
| BLAKE2b-256 |
76209526b285dd68291dd17761adba723a61f115a24ae30aad3b05ca68fa131a
|
Provenance
The following attestation bundles were made for bpe_openai-0.1.0.tar.gz:
Publisher:
release-wheels.yml on Pathlit-Inc/bpe-openai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bpe_openai-0.1.0.tar.gz -
Subject digest:
e90b5b4b3f9030b394148557136a8e7bf0bd40179354de2c26adeff40f08ada7 - Sigstore transparency entry: 596154661
- Sigstore integration time:
-
Permalink:
Pathlit-Inc/bpe-openai@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Pathlit-Inc
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-wheels.yml@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bpe_openai-0.1.0-cp39-abi3-win_amd64.whl.
File metadata
- Download URL: bpe_openai-0.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03a05cb0cdc58f151ef86f565df63db8904814d6ff45adf1cefe06c04bedcf6d
|
|
| MD5 |
50170b0fc986994f2826697522bd68e9
|
|
| BLAKE2b-256 |
d3eef5493a8b8372988ed834e515dec210a2dc8c75d5955bc912b413cc335469
|
Provenance
The following attestation bundles were made for bpe_openai-0.1.0-cp39-abi3-win_amd64.whl:
Publisher:
release-wheels.yml on Pathlit-Inc/bpe-openai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bpe_openai-0.1.0-cp39-abi3-win_amd64.whl -
Subject digest:
03a05cb0cdc58f151ef86f565df63db8904814d6ff45adf1cefe06c04bedcf6d - Sigstore transparency entry: 596154704
- Sigstore integration time:
-
Permalink:
Pathlit-Inc/bpe-openai@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Pathlit-Inc
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-wheels.yml@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bpe_openai-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: bpe_openai-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 41.6 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbacf09688bcf740c04425cab7cae0ef6c096009ea9611d124e7a9c8facdc549
|
|
| MD5 |
0a032128e32e1f554515f8fff8a5cf09
|
|
| BLAKE2b-256 |
18c469922240a768963fdf69a1a3fd941eb63d1d80778a9f098859a3fc5a3134
|
Provenance
The following attestation bundles were made for bpe_openai-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
release-wheels.yml on Pathlit-Inc/bpe-openai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bpe_openai-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
bbacf09688bcf740c04425cab7cae0ef6c096009ea9611d124e7a9c8facdc549 - Sigstore transparency entry: 596154682
- Sigstore integration time:
-
Permalink:
Pathlit-Inc/bpe-openai@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Pathlit-Inc
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-wheels.yml@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bpe_openai-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: bpe_openai-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 42.0 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2434a43da61d309f3b07d0153634117d2d120609e66fec5d7389cbd467ca2418
|
|
| MD5 |
3c52aafd969a1c07e10513b044208fb1
|
|
| BLAKE2b-256 |
2d36c59cf7f16f3510adbb14edde5ab08e8d2bedaaff87a5c13bb0af1007a655
|
Provenance
The following attestation bundles were made for bpe_openai-0.1.0-cp39-abi3-macosx_11_0_arm64.whl:
Publisher:
release-wheels.yml on Pathlit-Inc/bpe-openai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bpe_openai-0.1.0-cp39-abi3-macosx_11_0_arm64.whl -
Subject digest:
2434a43da61d309f3b07d0153634117d2d120609e66fec5d7389cbd467ca2418 - Sigstore transparency entry: 596154718
- Sigstore integration time:
-
Permalink:
Pathlit-Inc/bpe-openai@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Pathlit-Inc
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-wheels.yml@fde49fcb77e9af8b4361c6038cbfb51e00b5e9b6 -
Trigger Event:
push
-
Statement type: