Skip to main content

bpe-continue

PyPI Python versions License

Extend a trained BPE tokenizer with new vocabulary, without invalidating the old one.

Ordinary BPE training starts from single characters. bpe-continue starts from a tokenizer that is already trained and treats its tokens as the smallest units new merges may be built from. Nothing is discarded, so the result is a strict superset of the tokenizer you started with.

from tokenizers import Tokenizer
from bpe_continue import continue_training

base = Tokenizer.from_pretrained("gpt2")
extended = continue_training(base, files=["corpus.txt"], vocab_size=60000)
extended.save("extended.json")

The output is an ordinary tokenizer.json. Load it with tokenizers or transformers like any other tokenizer; this package is only needed to produce it.

The problem it solves

A pretrained tokenizer over-segments text that was not well represented in its training data — an unfamiliar language, a technical domain, a markup or code dialect. Sequences get longer, the context window holds less, and throughput drops.

Training a replacement tokenizer fixes the segmentation but reassigns every token id, so a pretrained embedding matrix no longer means anything and the model has to be retrained from scratch. Continued training avoids that trade-off: existing ids keep their meaning, and new vocabulary is appended.

Guarantees

  • Every existing token keeps its id. Rows of a pretrained embedding matrix stay valid. To adopt an extended tokenizer you grow the matrix by the number of tokens learned and initialise only the new rows.
  • Every existing merge keeps its rank, and new merges rank after all of them. New merges can therefore only glue whole existing tokens together: any text tokenizes to a coarsening of what the base produced, never to a different split. Text your corpus covers becomes shorter; everything else is byte-for-byte unchanged.
  • Added tokens keep their ids. Special and control tokens that live above the model vocabulary are folded into it at the ids they already hold, so they survive a save/load cycle rather than being renumbered when the vocabulary grows.
  • The input tokenizer is never modified. A new tokenizer is returned.

Installation

pip install bpe-continue

It installs alongside tokenizers rather than replacing it, so the rest of your environment is unaffected.

Usage

From files

Files are streamed, so the corpus does not have to fit in memory.

extended = continue_training(base, files=["a.txt", "b.txt"], vocab_size=60000)

base may be a Tokenizer, a path to a tokenizer.json, or the contents of one:

extended = continue_training("base/tokenizer.json", files=["corpus.txt"], vocab_size=60000)

From memory

extended = continue_training(base, texts=["a document", "another"], vocab_size=60000)

Command line

bpe-continue base.json extended.json corpus/*.txt --vocab-size 60000

Add --sample held-out.txt to report how much shorter that file's tokenization became.

Parameters

Parameter Description
base Tokenizer to extend: a Tokenizer, a path to a tokenizer.json, or its contents.
vocab_size Upper bound on the size of the final vocabulary. Nothing is discarded, so it must exceed the base's size; the difference is at most how many tokens are learned.
files / texts The training corpus. Exactly one of the two.
min_frequency Minimum occurrences before a pair may be merged. Default 0.
special_tokens Special tokens to add. Ones the base already has stay where they are; new ones are appended after everything else.
max_token_length Refuse to create tokens longer than this many characters.
show_progress Whether to draw progress bars. Default True.

Requirements for the base tokenizer

  • It must use a BPE model. Continued training means keeping the existing merges, and only BPE has them. WordPiece, WordLevel and Unigram are rejected with an explicit error.
  • Every character in the corpus must be reachable from its vocabulary. Byte-level tokenizers — which covers most modern ones — can represent any input, so this is automatic. A tokenizer that would silently drop characters instead raises an error naming the word, rather than training on a quietly corrupted corpus.

Both tokenizer.json merge formats are accepted: the space-joined strings written before tokenizers 0.20, and the token pairs written since.

How it works

  1. The corpus is split into words by the base tokenizer's own normalizer and pre-tokenizer, so it is processed exactly as the base would process it.
  2. Each word is split into atoms by the base model — its tokens, not characters.
  3. The vocabulary is seeded with the base's tokens at their existing ids, and its added tokens are folded in at theirs.
  4. The standard BPE merge loop runs on top, appending merges after the base's.

Because step 4 only ever combines units produced by step 2, no existing token boundary can be split differently. That is where the guarantees above come from.

Notes

  • vocab_size counts the whole final vocabulary, including any added tokens folded in at step 3. A tokenizer with 50,000 model tokens and 256 added tokens starts from 50,256.
  • vocab_size is a ceiling rather than a quota. Training also stops when no pair of tokens repeats often enough to be worth merging, so a small or homogeneous corpus can produce fewer new tokens than you asked for. This is the same behaviour as ordinary BPE training.
  • Continued training can only make the tokenization of a given text shorter or leave it unchanged. It cannot repair a base tokenizer whose pre-tokenizer splits your text badly, since word boundaries are fixed before BPE runs.

Licence

Apache-2.0. Built on a modified copy of Hugging Face's tokenizers Rust crate; see NOTICE for details. This is an independent project, not affiliated with or endorsed by Hugging Face.

Metadata

Release files for bpe-continue 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bpe-continue 0.1.0
File Size Uploaded Provenance Actions
bpe_continue-0.1.0.tar.gz 175.9 kB With provenance Details for bpe_continue-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for bpe-continue 0.1.0
File Actions
bpe_continue-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for bpe_continue-0.1.0-cp39-abi3-win_amd64.whl
bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
bpe_continue-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for bpe_continue-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
bpe_continue-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for bpe_continue-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 9.6 MB

Release files / bpe_continue-0.1.0.tar.gz

Download URL bpe_continue-0.1.0.tar.gz
Size 175.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b1b0a3b87129c0e5d61dc2d4ec20843e74fbf00ca8e70557c384c17ba1dfd71b
BLAKE2b-256 checksum
How to use checksums
3a5b2afb930c3213cbc1c7bec3bfa2d1f4a84cc92ba43d6fc1905a916a4142e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Release files / bpe_continue-0.1.0-cp39-abi3-win_amd64.whl

Download URL bpe_continue-0.1.0-cp39-abi3-win_amd64.whl
Size 1.7 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
981a5cdc0b18b6e2cfaf9034d22fd0152cb85e7817ab864a46a95b3eba5e7607
BLAKE2b-256 checksum
How to use checksums
4058fd00bc5ecfdf324e7607dc4d5ede290f7343b17709c870ad02ad5a1795bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Release files / bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.0 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
1d848f076cc2503645ecec080cd9b0e59dae5c97bdc6eb79b924adfa40ce89d8
BLAKE2b-256 checksum
How to use checksums
c5990266637dd8ff73991116f286d5d39ac8dad2377841fa00a3cb84ae92eb30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Release files / bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL bpe_continue-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 2.1 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
08a3e80a7b3a246f7324782f4281516bcb0433b79cd45eded28ba34277a643a0
BLAKE2b-256 checksum
How to use checksums
886c33f9db69eec24f62131545de7a7c02729984056305347d681a4afc33e3cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Release files / bpe_continue-0.1.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL bpe_continue-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 1.8 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4823b163dc72ef55132808fa7667b30e746cf43c143b69c608e4500240bd12b0
BLAKE2b-256 checksum
How to use checksums
36c946c5b90fd15107e8a8b2fb379bb659b11fc87df7ea81eb43f3047d45237f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Release files / bpe_continue-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL bpe_continue-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 1.8 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
eb971ec36daf279ec9d9510bc7d4d5eb6adc73114f4172e166bad1afa5b1bbeb
BLAKE2b-256 checksum
How to use checksums
66184347c4baaf949f21e4e1b5053bef5bcc181e0d76f212c0fe899d955c5bf4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Security for bpe-continue 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for bpe_continue-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page