fix(tokenizer): point users at from_hf_hub on unknown model (#229) by NishchayMahor · Pull Request #231 · mistralai/mistral-common · GitHub
Skip to content

fix(tokenizer): point users at from_hf_hub on unknown model (#229) - #231

Merged
juliendenize merged 3 commits into
mistralai:mainfrom
NishchayMahor:fix/from-model-error-message
Jun 1, 2026
Merged

juliendenize merged 3 commits into
mistralai:mainfrom
NishchayMahor:fix/from-model-error-message

Conversation

@NishchayMahor

Copy link
Copy Markdown
Contributor

`MistralTokenizer.from_model` raises `TokenizerException("Unrecognized model: X")` whenever the model name isn't in `MODEL_NAME_TO_TOKENIZER_CLS`. That mapping only covers older snapshots (latest entry is `2411`), so anything new — `mistral-small-3`, `mistral-medium-3`, `magistral-medium`, `saba`, ... — hits this branch and the caller has no idea what to do next.

This swaps the bare message for one that points at the existing escape hatches and echoes the failing name so typos are obvious:

```text
Unrecognized model: 'mistral-small-3-2501'. The static mapping in
MistralTokenizer.from_model only covers older model snapshots; newer or
custom models should be loaded with MistralTokenizer.from_hf_hub(repo_id=...)
or MistralTokenizer.from_file(...). Known names: codestral-2405, codestral-mamba-2407, ...
```

I considered also adding new entries to `MODEL_NAME_TO_TOKENIZER_CLS` for the recent models, but I don't want to guess at the right tokenizer version (v3-tekken vs v7 vs v11 with image config) for each model — that decision really has to come from your side. Happy to send a follow-up once the mapping for the newer snapshots is published.

Refs #229.

Tests

Existing `tests/test_mistral_tokenizer.py::test_from_model` already asserts `TokenizerException` for `"unknown-model"`. Extended it to also assert the message includes the offending name and the two escape hatches:

```
$ pytest tests/test_mistral_tokenizer.py::TestMistralToknizer::test_from_model
1 passed in 0.40s
```

…i#229)

The static MODEL_NAME_TO_TOKENIZER_CLS map only covers older model
snapshots. Newer models (mistral-small-3, mistral-medium-3, magistral,
saba, ...) fall through to a bare "Unrecognized model: X" which
doesn't tell the caller what to do next.

Surface the available escape hatches in the error itself — point at
MistralTokenizer.from_hf_hub() and .from_file(), and list the known
names so typos are obvious.

@juliendenize juliendenize left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey thanks for the contribution !

i think that the best would be to simply deprecate this from_model method and indeed point to from_file or from_hf_hub as we won't maintain the model mapping (it's been quite a while ^^)

raising as you did can be kept with minor change

lmk if you'd like to do the iteration otherwise i'll take over in another pr :)

Comment thread src/mistral_common/tokens/tokenizers/mistral.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"Unrecognized model: {model!r}. The static mapping in MistralTokenizer.from_model "
f"Unrecognized model: {model}. The static mapping in MistralTokenizer.from_model "

raise TokenizerException(
f"Unrecognized model: {model!r}. The static mapping in MistralTokenizer.from_model "
f"only covers older model snapshots; newer or custom models should be loaded with "
f"MistralTokenizer.from_hf_hub(repo_id=...) or MistralTokenizer.from_file(...). "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be consistent

Suggested change
f"MistralTokenizer.from_hf_hub(repo_id=...) or MistralTokenizer.from_file(...). "
f"MistralTokenizer.from_hf_hub(...) or MistralTokenizer.from_file(...). "

Comment thread tests/test_mistral_tokenizer.py Outdated
Comment on lines +32 to +34
# The error should point users at from_hf_hub / from_file so they can
# handle model snapshots that aren't in the static mapping, and echo
# the bad name so typos are obvious.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# The error should point users at from_hf_hub / from_file so they can
# handle model snapshots that aren't in the static mapping, and echo
# the bad name so typos are obvious.

The static MODEL_NAME_TO_TOKENIZER_CLS map isn't kept current with new
releases. Mark from_model with a FutureWarning pointing at from_hf_hub
and from_file, drop the known-name listing from the unknown-model error
(since the map isn't authoritative going forward), and migrate the
repo's own callers to .v1() / .v3() or wrap them in pytest.warns so
test output stays quiet.

Addresses @juliendenize's review on mistralai#231.
@NishchayMahor

NishchayMahor commented May 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Really appreciate the thoughtful review @juliendenize :D. The deprecation framing is a much cleaner direction than my original "better error" attempt, and the consistency notes on the suggestion blocks made the diff way tighter. Genuinely learned something from this iteration, so thanks for taking the time on it!

Pushed the deprecation:

  • from_model now emits a FutureWarning (modeled on normalizer_for_tokenizer_version in normalize.py) pointing at from_hf_hub / from_file, with removed in 1.12.0 to match the existing precedent.
  • Docstring updated to flag the deprecation up top.
  • Applied all four of your inline suggestions: {model} instead of {model!r}, the from_hf_hub(...) / from_file(...) consistency, and dropped the extra comment in the test.
  • Also dropped the Known names: ... listing from the error since you mentioned the map isn't being maintained, which felt misleading to enumerate going forward.
  • Migrated the in-repo callers so test output stays quiet: test_tokenizer_v7.py::test_from_model is wrapped in pytest.warns(FutureWarning) (since that test exists to exercise the deprecated method), and the two helper calls in test_mistral_tokenizer.py now use .v1() / .v3().

Local run is green (13 passed). Happy to iterate further if anything else needs adjusting. Really grateful for the steer either way :)

@juliendenize juliendenize left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for iterating looks good now

Comment thread src/mistral_common/tokens/tokenizers/mistral.py Outdated
Comment thread src/mistral_common/tokens/tokenizers/mistral.py Outdated
Comment thread src/mistral_common/tokens/tokenizers/mistral.py Outdated
Co-authored-by: Julien Denize <40604584+juliendenize@users.noreply.github.com>
@juliendenize
juliendenize merged commit b83b5ea into mistralai:main Jun 1, 2026
7 checks passed
@NishchayMahor

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants