Send HUGGING_FACE_TOKEN for public but gated Hugging Face repos - #538
Open
MaxGhenis wants to merge 1 commit into
Open
Send HUGGING_FACE_TOKEN for public but gated Hugging Face repos#538MaxGhenis wants to merge 1 commit into
MaxGhenis wants to merge 1 commit into
Conversation
download_huggingface_dataset only authenticated when ModelInfo.private was true. A public but gated repo reports private=False with gated="auto" or "manual", so the helper passed token=None and the Hub answered 401 unless the caller happened to export HF_TOKEN. Treat a repo as requiring authentication when private is true or gated is truthy, and pass the HUGGING_FACE_TOKEN through the existing get_or_prompt_hf_token() path. Public, ungated repos still download with token=None and never prompt, and non-interactive runs without secrets still pass None rather than an empty string. Fixes #529. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This was referenced Sep 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #529.
Summary
download_huggingface_datasetdecided whether to send a token by testingModelInfo.privateonly.policyengine/policyengine-uk-data-privatehas been public but gated (private=False,gated="manual") since 31 July 2026, so the helper passedtoken=Nonetohf_hub_downloadand the gate returned401 GatedRepoErroreven when a gate-approvedHUGGING_FACE_TOKENwas in the environment. The Hub only falls back to its ownHF_TOKENvariable when no explicit token is passed, which is why PolicyEngine/policyengine-uk#1817 had to export both names as a workaround (see the comment in.github/workflows/code_changes.yamlandpr_code_changes.yamlthere).Behaviour change
A repo now requires authentication when
privateis true orgatedis truthy.ModelInfo.gatedisFalsefor ungated repos,"auto"or"manual"for gated repos, andNonewhen the field is absent, so a truthiness test covers every state. In that case the existingget_or_prompt_hf_token()supplies the token fromHUGGING_FACE_TOKEN(or an interactive prompt on a TTY).Unchanged:
token=Noneand never prompt.Nonerather than an empty string (the Handle empty HUGGING_FACE_TOKEN gracefully #422 behaviour), so Dependabot-style CI keeps working for public repos.RepositoryNotFoundErrorpath still treats the repo as private.HUGGING_FACE_TOKENunset, a gated repo now prompts for a token exactly as a private repo always has. Pressing Enter passestoken=None, and huggingface_hub then applies its own implicit token (HF_TOKENor the login file) if one is configured.Verified live:
GET /api/models/policyengine/policyengine-uk-data-privatereturnsprivate: false, gated: "manual", and an unauthenticatedHEADon a resolve URL returns 401.Tests
New tests in
tests/core/tools/test_hugging_face.py:test_download_gated_public_repo_passes_env_token[manual|auto]mocksmodel_inforeturningprivate=False, gated=...and asserts the token read fromHUGGING_FACE_TOKENby the realget_or_prompt_hf_tokenreacheshf_hub_download. Both fail onmasterand pass here.test_download_private_flag_repo_passes_env_tokencoversprivate=Truereturned bymodel_info(the existing private test only covers the 404 path).test_download_gated_repo_non_interactive_without_tokenasserts CI without secrets getstoken=Noneand no prompt.test_download_public_ungated_repo_never_prompts[False|None]guards against widening the predicate into a prompt on every public download.Checks run locally:
uvx ruff format --check .anduvx ruff check .pass;ruff check --select F821is clean for the changed files, and mypy onhugging_face.pydrops from four errors to the one pre-existingversion: str = Noneerror at line 49.uv run pytest tests: 702 passed, 4 skipped, 1 xfailed.policyengine-core test policyengine_core/country_template/tests -c policyengine_core.country_template: 39 passed.Not run:
make documentation(no docs changed; CI runs it).Related
🤖 Generated with Claude Code