Skip to content

Keep tokenizers loading when the Hub rate limits the Mistral tag lookup - #18662

Merged
danielhanchen merged 1 commit into
mainfrom
tokenizer-hub-429
Nov 20, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
tokenizer-hub-429

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem: On transformers 4.57.x through 5.5.4, loading any tokenizer with a vocab over 100k by repo id (Qwen3, Llama 3, Gemma, and so on) makes an anonymous model_info call to read the repo's tags and spot a Mistral base. That call has no error handling, so a single 429 fails from_pretrained even when every tokenizer file is already cached. Anonymous users on shared IPs (Colab, Kaggle, CI runners) are the ones who hit this: the anonymous API limit is 500 calls per 5 minutes per IP. On transformers 5.x the 429 also surfaces as a misleading OSError: Unable to load vocabulary from file.

transformers 5.6.0 fixed it upstream by treating a failed lookup as "not Mistral".

Fix: fix_transformers_mistral_regex_hub_error in import_fixes.py back-ports that guard. It wraps _patch_mistral_regex on PreTrainedTokenizerBase (4.57.x) or TokenizersBackend (5.0 to 5.5). An exception raised inside the is_base_mistral tag lookup returns the tokenizer unchanged, exactly as 5.6.0 does; any other error still raises. It is applied only when the installed is_base_mistral has no try:, so it does nothing on 5.6.0 and later, and applying it twice wraps once.

Testing

Warm-cache AutoTokenizer.from_pretrained("trl-internal-testing/tiny-Qwen3ForCausalLM") with the tag lookup answering 429:

transformers without the fix with the fix
4.57.6 HfHubHTTPError: 429 Client Error: Too Many Requests loads, same token ids
5.0.0 OSError: Unable to load vocabulary from file loads, same token ids
5.5.4 OSError: Unable to load vocabulary from file loads, same token ids
5.18.0 loads (guarded upstream) loads, guard not installed
  • tests/test_mistral_regex_hub_guard.py (new, 4 tests): a 429 in the lookup keeps the tokenizer, unguarded transformers fails the load, an error outside the lookup still raises, and the fix is idempotent. Run against main's import_fixes.py (UNSLOTH_IMPORT_FIXES), the 429 test fails.
  • tests/test_transformers_security_fixes.py still passes (34 passed together).

Below transformers 5.6.0 every tokenizer with a vocab over 100k loaded by repo id calls an anonymous model_info to check for a Mistral base, with no error handling, so a 429 (common on shared Colab and Kaggle IPs) fails from_pretrained even with every file cached. Back-port the 5.6.0 guard: a failure inside that lookup means not Mistral; any other error still raises. No-op on 5.6.0+.
@danielhanchen danielhanchen self-assigned this Nov 20, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T19:21:54.726539Z 683034d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 683034d4a5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 79daa69 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the tokenizer-hub-429 branch November 20, 2026 19:47
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…aseline (#18677)

## Summary

`Source lint (Python + shell + YAML + JSON + safety nets)` is red on
`main` (seen on #18675, reproduced on current `main`). Its blocking step
is `No new risky loader call sites`:

```
1 risky loader call site(s) not in the baseline:
  unsloth/import_fixes.py:1135  [dynamic-import] import_module
```

## Cause

#18662 (keep tokenizers loading when the Hub rate limits the Mistral tag
lookup) adds `fix_transformers_mistral_regex_hub_error`. It calls
`importlib.import_module(module_name)` over a fixed literal tuple:

- `transformers.tokenization_utils_base`
- `transformers.tokenization_utils_tokenizers`

The call has the same shape as the four already-reviewed
`import_module(module_name)` sites in `import_fixes.py`, so it lands in
their baseline group (digest `59e72c01edb14c86`) as a fifth instance.
The gate reports one site over the recorded count; the line it names
(1135) is simply the last one it counts. The new call is safe: the
module names are written out in the source. The baseline just was not
updated.

## Change

`scripts/risky_loaders_baseline.json`, regenerated with `python
scripts/lint_risky_loaders.py --update`:

- the group's count goes from 4 to 5;
- its reason now also names the two literal tokenizer owners of
`_patch_mistral_regex`.

No other entry changes.

## Testing

- `python scripts/lint_risky_loaders.py`: exits 1 on `main` with the
message above; with this change, `ok: 122 risky loader call site(s), all
recorded`.
- `python scripts/lint_risky_loaders.py --self-test`: ok.
- `tests/security/test_lint_risky_loaders.py`: 46 passed.
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.

1 participant