Views
No views yet
tokenizer.json.
The Python package is named basetenkenizer.
pip install basetenkenizertokenizer.json on first use and
then uses the local Hugging Face cache. Private or gated models use the usual
HF_TOKEN environment variable.1from basetenkenizer import Tokenizer
2
3tokenizer = Tokenizer.from_model("baseten/kimi-k3-tokenizer")
4ids = tokenizer.encode("Hello from Kimi K3").idsEncodeSegment
objects to encode_segments as (segment.text, segment.allow_special). A
minimal user message followed by an assistant-generation prefix looks like
this:1user_message = "Explain speculative decoding."
2
3segments = [
4 ("<|open|>", True),
5 ("message", False),
6 (" role", False),
7 ('="', False),
8 ("user", False),
9 ('"', False),
10 ("<|sep|>", True),
11 (user_message, False),
12 ("<|close|>", True),
13 ("message", False),
14 ("<|sep|>", True),
15 ("<|end_of_msg|>", True),
16 ("<|open|>", True),
17 ("message", False),
18 (" role", False),
19 ('="', False),
20 ("assistant", False),
21 ('"', False),
22 ("<|sep|>", True),
23 ("<|open|>", True),
24 ("response", False),
25 ("<|sep|>", True),
26]
27
28encoding = tokenizer.encode_segments(segments)
29input_ids = encoding.idsbuild_chat_segments renderer for production
conversations involving tools, images, reasoning content, response schemas,
or multiple message types. Do not concatenate its segments before encoding:
doing so loses the special-token safety boundary.encode_segments?(text, allow_special) pair:allow_special=True recognizes tokenizer control tokens emitted by a trusted
chat renderer.allow_special=False treats control-token-looking strings in user, tool, or
attribute content as ordinary text.tiktoken_safe=True is the default. It reproduces the chunk boundaries used
by legacy tiktoken tokenizers, including on very long inputs, so token IDs stay
compatible. Set it to False only when exact tiktoken parity is not required
and whole-segment BPE encoding is intentional.1from basetenkenizer import Tokenizer
2
3tokenizer = Tokenizer.from_model("deepseek-ai/DeepSeek-V3.2")
4encoding = tokenizer.encode(
5 "A very long prompt that is now much faster.",
6 add_special_tokens=False,
7)
8
9print(encoding.ids)
10print(tokenizer.decode(encoding.ids))Tokenizer.from_file("tokenizer.json") loads a local tokenizer. Encoding
objects expose ids, attention_mask, type_ids, and
special_tokens_mask; selected fields can be moved into NumPy arrays with
encoding.into_numpy(...).1from basetenkenizer import Tokenizer
2
3tokenizer = Tokenizer.from_model("moonshotai/Kimi-K2.7-Code")
4ids = tokenizer.encode("def hello():\n return 'world'").idsencode_segments:1user_message = "Write a Python HTTP server."
2
3segments = [
4 ("<|im_user|>user<|im_middle|>", True),
5 (user_message, False),
6 ("<|im_end|>", True),
7 ("<|im_assistant|>assistant<|im_middle|><think>", True),
8]
9
10encoding = tokenizer.encode_segments(segments)
11input_ids = encoding.idspatch_transformers before loading a tokenizer:1import basetenkenizer
2
3basetenkenizer.patch_transformers()
4
5from transformers import AutoTokenizer
6
7tokenizer = AutoTokenizer.from_pretrained("openai/gpt-oss-120b")
8tokens = tokenizer("Hello, world!")patch_transformers(apply_chat_template=True) to also use the native renderer
for supported render-only apply_chat_template(..., tokenize=False) calls;
unsupported templates automatically fall back to Transformers.basetenkenizer package is licensed under the MIT License.