Skip to content

Commit ca68c9e

Browse files
authored
fix(config): forward token kwarg to get_config_dict in PreTrainedConfig.from_pretrained (#50081)
`token` is an explicit parameter of `PreTrainedConfig.from_pretrained` but was never added to `kwargs` before calling `get_config_dict`. This caused it to arrive as `None` in `cached_files`, silently dropping the caller's token. Consequence: loading from private or gated repos using `token=` as a kwarg only worked if `HF_TOKEN` was also set as an env var. In clean environments (e.g. CI pipelines that pass the token explicitly without `hf auth login`), it would fail with a 401 / RepositoryNotFoundError. All other `from_pretrained` entry points (tokenizers, processors, feature extractors) already forward `token` correctly. This was specific to the config loading path. Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
1 parent 5e83c28 commit ca68c9e

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/transformers/configuration_utils.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ def from_pretrained(
877877
kwargs["cache_dir"] = cache_dir
878878
kwargs["force_download"] = force_download
879879
kwargs["local_files_only"] = local_files_only
880+
kwargs["token"] = token
880881
kwargs["revision"] = revision
881882

882883
config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)

‎tests/utils/test_configuration_utils.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,33 @@ def __init__(
476476
self.assertIsInstance(new_config_instance.nan, float)
477477

478478

479+
class ConfigTokenPropagationTest(unittest.TestCase):
480+
def test_token_kwarg_forwarded_to_get_config_dict(self):
481+
"""
482+
Regression test: `token` is an explicit parameter of `PreTrainedConfig.from_pretrained`
483+
but was not forwarded into `kwargs` before calling `get_config_dict`, so it arrived as
484+
`None` in `cached_files`. This broke loading from private/gated repos when `token=` was
485+
passed as a kwarg and `HF_TOKEN` env var was not set.
486+
"""
487+
captured = {}
488+
489+
def fake_get_config_dict(cls, pretrained_model_name_or_path, **kwargs):
490+
captured["token"] = kwargs.get("token")
491+
raise OSError("stop") # abort early, we only care about kwargs
492+
493+
with mock.patch.object(BertConfig, "get_config_dict", classmethod(fake_get_config_dict)):
494+
try:
495+
BertConfig.from_pretrained("dummy-repo", token="my-secret-token")
496+
except OSError:
497+
pass
498+
499+
self.assertEqual(
500+
captured.get("token"),
501+
"my-secret-token",
502+
"token kwarg was not forwarded to get_config_dict",
503+
)
504+
505+
479506
class ConfigSubclassKwOnlyTest(unittest.TestCase):
480507
"""Test that config subclasses with non-default fields following parent default fields
481508
no longer raise TypeError (fixed by kw_only=True in __init_subclass__). Regression

0 commit comments

Comments
 (0)