Skip to content

Fix model_input_names singleton issue causing shared state - #42051

Merged
itazap merged 2 commits into
huggingface:mainfrom
yashwantbezawada:fix/model-input-names-singleton-42024
Nov 3, 2026
Merged

itazap merged 2 commits into
huggingface:mainfrom
yashwantbezawada:fix/model-input-names-singleton-42024

Conversation

@yashwantbezawada

@yashwantbezawada yashwantbezawada commented Nov 6, 2025 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #42024

I found this while testing tokenizers - when you modify model_input_names on one tokenizer instance, it was affecting all other instances of the same tokenizer class.

The problem is that model_input_names is defined as a class-level list, and in the __init__ method (line 1417), when no custom model_input_names is provided, it was just referencing the class attribute directly instead of making a copy.

So all instances were sharing the same list object. This is a classic Python gotcha with mutable class attributes.

The fix is simple - wrap it in list() to create a new list for each instance:

Before:

self.model_input_names = kwargs.pop("model_input_names", self.model_input_names)

After:

self.model_input_names = list(kwargs.pop("model_input_names", self.model_input_names))

This ensures each tokenizer instance gets its own independent copy of the list. Now modifications to one instance won't affect others.

The reproduction from the issue shows the problem clearly - with the fix, the second tokenizer instance will have the original list values instead of inheriting the modifications from the first instance.

@yashwantbezawada
yashwantbezawada force-pushed the fix/model-input-names-singleton-42024 branch from 4d018bf to 6ba1ffb Compare November 6, 2025 02:17
@Rocketknight1

Copy link
Copy Markdown
Member

This solution LGTM but cc @ArthurZucker @itazap!

@ArthurZucker

Copy link
Copy Markdown
Collaborator

@itazap could you take a look when you get a chance? This is waiting on a maintainer: needs maintainer review (Rocketknight1 LGTM'd and cc'd ArthurZucker/itazap).

Yashwant Bezawada and others added 2 commits November 3, 2026 23:10
Fixes huggingface#42024

The model_input_names attribute was defined as a class-level list, and
when initializing tokenizer instances, they were all pointing to the same
list object. This meant modifying model_input_names on one instance would
affect all other instances.

The issue was in tokenization_utils_base.py line 1417:
```python
self.model_input_names = kwargs.pop("model_input_names", self.model_input_names)
```

When no model_input_names is passed in kwargs, it would use the class
attribute directly (self.model_input_names), creating a reference to the
shared list instead of creating a new list for the instance.

Fixed by wrapping it in list() to ensure each instance gets its own copy:
```python
self.model_input_names = list(kwargs.pop("model_input_names", self.model_input_names))
```

This is a standard pattern for handling mutable default values in Python.
@itazap
itazap force-pushed the fix/model-input-names-singleton-42024 branch from 6ba1ffb to 58ab3b2 Compare November 3, 2026 22:15

@itazap itazap 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.

thank you and I apologize for the delay!

@itazap
itazap enabled auto-merge November 3, 2026 22:16
@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.

@itazap
itazap added this pull request to the merge queue Nov 3, 2026
Merged via the queue into huggingface:main with commit 41ce391 Nov 3, 2026
128 checks 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.

PreTrainedTokenizerBase.model_input_names is a singleton

5 participants