Tokenizer Backends#
Tokenizer backends provide the token-counting mechanism used by HybridChunker to enforce per-chunk token budgets. All backends implement the BaseTokenizer abstract interface, a Pydantic BaseModel + ABC that requires three methods:
| Method | Purpose |
|---|---|
count_tokens(text) | Returns the integer token count for a string |
get_max_tokens() | Returns the configured token budget ceiling |
get_tokenizer() | Returns the underlying tokenizer object |
Key invariant: the tokenizer passed to HybridChunker must match the embedding model's tokenizer — token counts are only meaningful relative to that model's context window.
HuggingFaceTokenizer#
File: docling_core/transforms/chunker/tokenizer/huggingface.py
Install: pip install 'docling-core[chunking]' (pulls in transformers + huggingface-hub)
Wraps any PreTrainedTokenizerBase from 🤗 Transformers.
- Token counting calls
tokenizer.tokenize(text=text)and returnslen(...). max_tokensis optional at construction. When omitted, the validator attempts to auto-detect it by downloadingsentence_bert_config.jsonfrom the HuggingFace Hub for the model and readingmax_seq_length. If that file is absent, construction raisesRuntimeError.- Convenience constructor
HuggingFaceTokenizer.from_pretrained(model_name, max_tokens=None)wrapsAutoTokenizer.from_pretrainedand accepts an optional explicitmax_tokens. - The module exposes
get_default_tokenizer(), which returns aHuggingFaceTokenizerbacked bysentence-transformers/all-MiniLM-L6-v2— this is also the default used byHybridChunkerwhen no tokenizer is supplied.
⚠️ HuggingFace Transformers may emit
"Token indices sequence length is longer than the specified maximum sequence length"duringHybridChunkerinitialization. This is a false alarm and can be safely ignored.
OpenAITokenizer#
File: docling_core/transforms/chunker/tokenizer/openai.py
Install: pip install 'docling-core[chunking-openai]' (pulls in tiktoken)
Wraps a tiktoken.Encoding object.
- Both
tokenizer(atiktoken.Encoding) andmax_tokens(anint) are required at construction — there is no auto-detection. - Token counting calls
tokenizer.encode(text=text)and returnslen(...). - A
model_validatorat construction time checks thattiktokenis importable and raisesImportErrorwith the install hint if not.
Typical usage:
import tiktoken
from docling_core.transforms.chunker.tokenizer.openai import OpenAITokenizer
tokenizer = OpenAITokenizer(
tokenizer=tiktoken.encoding_for_model("gpt-4o"),
max_tokens=128 * 1024,
)
Comparison at a Glance#
HuggingFaceTokenizer | OpenAITokenizer | |
|---|---|---|
| Extra install | [chunking] | [chunking-openai] |
| Underlying lib | transformers | tiktoken |
max_tokens | Optional (auto-detected from Hub) | Required |
| Count method | tokenizer.tokenize() | tokenizer.encode() |
Default for HybridChunker | ✅ (all-MiniLM-L6-v2) | ❌ |
| Performance benchmarks | None published | None published |
No official benchmarks comparing the two implementations have been published.
Adding a Custom Backend#
Subclass BaseTokenizer (a Pydantic BaseModel) and implement the three abstract methods. Pass the instance directly to HybridChunker(tokenizer=...).