Skip to content

Warn when a restricted Hugging Face download resolves no HUGGING_FACE_TOKEN - #540

Open
MaxGhenis wants to merge 1 commit into
masterfrom
fix/hf-no-token-warning
Open

Warn when a restricted Hugging Face download resolves no HUGGING_FACE_TOKEN#540
MaxGhenis wants to merge 1 commit into
masterfrom
fix/hf-no-token-warning

Conversation

@MaxGhenis

Copy link
Copy Markdown
Contributor

Summary

Diagnostic follow-up to #529 (the fix for gated repos itself is #538). download_huggingface_dataset now emits a UserWarning when the repo requires authentication (model_info reports private=True, or raises RepositoryNotFoundError) and get_or_prompt_hf_token() returned None. The warning names the repo, says no HUGGING_FACE_TOKEN was available, that huggingface_hub's own cached token (HF_TOKEN or the hf auth login file) will be used if present, and that a 401 that follows (RepositoryNotFoundError or GatedRepoError) means neither was set or the token is not approved for the repo.

Behaviour is otherwise unchanged, deliberately (per #422): token=None is still passed through to hf_hub_download, there is no raise, no prompt, and core does not read HF_TOKEN. The fallback is huggingface_hub's own: in 1.4.1, utils/_headers.py::get_token_to_send calls get_token() when token is None, and utils/_http.py::hf_raise_for_status maps a 401 to GatedRepoError (X-Error-Code: GatedRepo) or RepositoryNotFoundError.

The warning uses stacklevel=2, so it points at the caller (for example Dataset.download_from_huggingface), and it is not emitted for public repos, which also pass token=None. The module's import-time warnings.catch_warnings() block is a no-op and is not relied on.

Tests

  • New TestNoTokenWarning in tests/core/tools/test_hugging_face.py: the warning fires for private-flag and not-found lookups, across token unset, token empty, and only-HF_TOKEN-set environments non-interactively, and for an empty interactive prompt, with token=None passed on and no prompt; no warning for public repos or when a token was resolved from the environment or a prompt; message content and stacklevel are asserted.
  • Mutation check: with the source change reverted, the 7 warning-asserting tests fail and the 5 no-warning guards pass.
  • uv run pytest tests/core/tools/test_hugging_face.py: 27 passed. uv run pytest tests/core: 708 passed, 1 skipped, 1 xfailed. uvx ruff format --check . and uvx ruff check .: clean.
  • Not run: make test (full suite with coverage and reruns) and make documentation; CI covers them and no docs pages change.

Overlap with #538 and #539

Both are still open, so this branch is from master (9125de0). Checked locally by merging each into this branch:

    authentication_token: str | None = None
    if requires_authentication:
        authentication_token = get_or_prompt_hf_token()
        if authentication_token is None:
            warnings.warn(...)  # body unchanged from this PR

Documentation review

  • Documentation changes: a Warns: section in the download_huggingface_dataset docstring, and the towncrier fragment changelog.d/warn-hf-no-token.changed.md. No docs/ page describes this function's token handling, so nothing else needed updating.
  • Impact: low (a warning, no control-flow change). Confidence: high (huggingface_hub behaviour read from the installed 1.4.1 source; both lookup paths are tested).
  • Known gaps: none here. After Send HUGGING_FACE_TOKEN for public but gated Hugging Face repos #538 lands, the docstring's "requires authentication" wording already covers gated repos.

🤖 Generated with Claude Code

download_huggingface_dataset keeps passing token=None through to
hf_hub_download when a private repo yields no HUGGING_FACE_TOKEN (per
#422: huggingface_hub then applies its own cached token, so `hf auth
login` users keep working), but now emits a UserWarning first so that
the bare 401 huggingface_hub raises when that fallback is empty too can
be traced to the missing or unapproved token (#529).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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