Fix KeyError in convert_to_native_format for dict vocab#44452
Merged
itazap merged 1 commit intohuggingface:mainfrom Mar 19, 2026
Merged
Conversation
Member
|
cc @itazap @ArthurZucker since it seems like a tokenizer regression (see also the original issue at #44451) |
|
Thank you, looking forward to it! This was the best model I'd found so far for annotation tasks on DA and IS. (Will keep exploring other options anyway) |
ArthurZucker
approved these changes
Mar 10, 2026
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
Can this be merged? It would be great to use this transformer again with newer versions of the package |
When loading tokenizers like vesteinn/ScandiBERT whose tokenizer_config specifies XLMRobertaTokenizer (model=Unigram) but whose tokenizer.json contains a dict-type vocab, the expression vocab[0] raises KeyError because dict keys are strings, not integers. Add an isinstance(vocab, list) guard so the list-to-tuple conversion is only attempted on list vocabs.
1317523 to
cd5cfd5
Compare
Member
|
Looks like it was ready to go and just had automerge issues, will try to merge it. |
|
That's great, thank you! |
|
Is this fixable? |
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.
Fix KeyError in
convert_to_native_formatfor dict vocabFixes #44451
Problem
AutoTokenizer.from_pretrained("vesteinn/ScandiBERT")raisesKeyError: 0inconvert_to_native_format.ScandiBERT's
tokenizer_config.jsonspecifiestokenizer_class: "XLMRobertaTokenizer", which hasmodel = Unigram. Whenconvert_to_native_formatreads thetokenizer.json, it enters the Unigram branch and executes:However, the
vocabfromtokenizer.jsonis a dict ({"token": score, ...}), not a list. Indexing a dict with[0]raisesKeyErrorbecause there is no key0.Fix
Add an
isinstance(vocab, list)check before attempting to indexvocab[0]:This is consistent with the guards used in the other branches (e.g., BPE/WordPiece branch on line 163). When
vocabis already a dict, no conversion is needed and it is passed through as-is.Regression
Introduced in v5 by #42894 (
use TokenizersBackend).