Fix OpenAITokenizerWrapper compatibility with Docling HybridChunker#23
Open
deril2605 wants to merge 1 commit intodaveebbelaar:mainfrom
Open
Fix OpenAITokenizerWrapper compatibility with Docling HybridChunker#23deril2605 wants to merge 1 commit intodaveebbelaar:mainfrom
deril2605 wants to merge 1 commit intodaveebbelaar:mainfrom
Conversation
Docling’s HybridChunker evaluates the provided tokenizer in a boolean context (e.g. `tokenizer or default_tokenizer`). Since OpenAITokenizerWrapper subclasses HuggingFace’s PreTrainedTokenizerBase, this triggered the base class __len__() method, which raises NotImplementedError by default, causing chunking to fail at initialization. This change adds explicit __len__() and __bool__() implementations to make the tokenizer safe for truthiness checks and compatible with Docling’s internal validation logic. Additionally, this fixes the vocabulary size calculation to align with HuggingFace semantics (vocab_size = max_token_id + 1) and corrects the get_vocab() mapping to return token → id pairs as expected. No tokenization behavior or chunking logic is changed; this is a compatibility and robustness fix only. The tokenizer continues to use tiktoken for local token counting and remains model-agnostic, suitable for OpenAI and Azure OpenAI workflows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Docling’s HybridChunker evaluates the provided tokenizer in a boolean context (e.g.
tokenizer or default_tokenizer). Since OpenAITokenizerWrapper subclasses HuggingFace’s PreTrainedTokenizerBase, this triggered the base class len() method, which raises NotImplementedError by default, causing chunking to fail at initialization.This change adds explicit len() and bool() implementations to make the tokenizer safe for truthiness checks and compatible with Docling’s internal validation logic.
Additionally, this fixes the vocabulary size calculation to align with HuggingFace semantics (vocab_size = max_token_id + 1) and corrects the get_vocab() mapping to return token → id pairs as expected.
No tokenization behavior or chunking logic is changed; this is a compatibility and robustness fix only. The tokenizer continues to use tiktoken for local token counting and remains model-agnostic, suitable for OpenAI and Azure OpenAI workflows.