Skip to content

Use PreTrainedTokenizerBase for tokenizer type hints#5629

Merged
qgallouedec merged 5 commits into
mainfrom
fix-type-hint
Apr 23, 2026
Merged

Use PreTrainedTokenizerBase for tokenizer type hints#5629
qgallouedec merged 5 commits into
mainfrom
fix-type-hint

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Apr 22, 2026

Copy link
Copy Markdown
Member

In TRL, type hints for tokenizer parameters should use PreTrainedTokenizerBase, not PreTrainedTokenizer.

In transformers, PreTrainedTokenizer is an alias for PythonBackend (slow tokenizer only). The fast tokenizer (TokenizersBackend) inherits directly from PreTrainedTokenizerBase and is NOT a PreTrainedTokenizer. Since AutoTokenizer.from_pretrained(...) returns a fast tokenizer by default for most models, PreTrainedTokenizer hints fail to cover the common case. PreTrainedTokenizerBase is the common ancestor and exposes .apply_chat_template, .chat_template, .vocab, .eos_token, etc.

How to apply: When adding or editing a function that accepts a tokenizer (not model-specific), use PreTrainedTokenizerBase. Only use PreTrainedTokenizer if the code truly requires slow-tokenizer-only behavior (rare).


Note

Low Risk
Low risk type-hint-only change; runtime behavior should be unchanged, but downstream type checkers may surface new/changed annotations.

Overview
Broadens tokenizer type annotations from PreTrainedTokenizer to PreTrainedTokenizerBase across chat-template utilities (e.g. clone_chat_template, get_training_chat_template, parse_response) and BCO dataset tokenization (_tokenize) so fast tokenizers and processors are covered.

Cleans up related typing imports (drops TYPE_CHECKING/Optional usage) and updates the ProcessingClassT TypeVar/docstrings to match the new base type.

Reviewed by Cursor Bugbot for commit 87c2dbf. Bugbot is set up for automated code reviews on this repo. Configure here.

@qgallouedec qgallouedec changed the title Use PreTrainedTokenizerBase for tokenizer type hints Use PreTrainedTokenizerBase for tokenizer type hints Apr 22, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 382914bc3b

ℹ️ 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".

Comment thread trl/experimental/bco/bco_trainer.py Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 90c1e99. Configure here.

Comment thread trl/experimental/bco/bco_trainer.py
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@albertvillanova albertvillanova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks.

@qgallouedec qgallouedec merged commit a9cfe47 into main Apr 23, 2026
13 of 15 checks passed
@qgallouedec qgallouedec deleted the fix-type-hint branch April 23, 2026 12:45
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.

3 participants