skip to content

A Hugging Face Hub download of a gated Llama repo works on your laptop but fails in CI with GatedRepoError — what do you check and fix?

level: seniorimportance: should knowfreq 13%

answer

  1. whose identity does the CI job send?
  2. access is granted per user account
  3. HF_TOKEN overrides the saved token
  4. fine-grained token with read on that repo
  5. GatedRepoError versus RepositoryNotFoundError

basics

~20 s

Gated access is granted to individual user accounts, so CI fails when its token belongs to an account that was never approved or lacks read permission on that repo. Check which account the job sends, then use an approved account's token.

solid answer

~40 s

`GatedRepoError` means the repo exists and is gated, and the account behind the request is not on its authorized list. Access is requested in a browser and granted to an individual user, never to a whole organization, so a teammate's approval does not cover a service account. First find out which token the job really sends: `huggingface_hub` uses a Trusted Publishers exchange when `HF_OIDC_RESOURCE` is set, otherwise `HF_TOKEN`, otherwise the token file under `HF_HOME`, and `hf auth whoami` prints the account. Then fix the credential: a token from an approved account, ideally a fine-grained token with read access to that repo, stored as a CI secret and exported as `HF_TOKEN`. If the request is still pending under manual approval, no token helps until the authors accept it.

code

python · 13 lines
python
from huggingface_hub import auth_check, whoami
from huggingface_hub.errors import GatedRepoError, RepositoryNotFoundError

REPO = "meta-llama/Llama-3.2-1B-Instruct"

print("authenticating as:", whoami()["name"])
try:
    auth_check(REPO)
except GatedRepoError:  # must come first: it subclasses RepositoryNotFoundError
    raise SystemExit(f"{REPO}: this account is not on the gated repo's access list")
except RepositoryNotFoundError:
    raise SystemExit(f"{REPO}: wrong id, or a private repo this token cannot see")
print("access confirmed")

go deeper

for a junior

Recall that gated repos need an approved account and a token, that hf auth login or HF_TOKEN provides the token, and that the access request is made in a browser.

for a middle

Explain the order in which huggingface_hub picks a token, and tell GatedRepoError apart from RepositoryNotFoundError and RevisionNotFoundError.

for a senior

Diagnose the identity a CI job really sends, choose an approved account and a fine-grained token scoped to the repo, and add a preflight check so access failures surface immediately.

for a principal

Decide how a team owns gated-model access: which accounts hold approvals, how tokens are scoped and rotated, and when short-lived CI identities replace stored secrets.

## How gating works on the Hub A **gated** repo is a public repo whose files are only served after a user agrees to share their contact details with the authors, and sometimes answers extra questions the authors configured. The mechanics matter for automation: - **Requests are made in a browser.** A logged-in user opens the model page and submits the form; there is no request flow for a script to complete on its own. - **Approval is automatic or manual.** With automatic approval access is immediate; with manual approval the request waits in a pending list until the authors accept it. - **Access belongs to a user, not an organization.** The Hub documentation states that access requests are always granted to individual users rather than to entire organizations. - **Access can be withdrawn.** Authors can reject or reset access at any time, so a job that worked last month can start failing without any change on your side. - **Organization members may be gated too.** An organization can require its own members to request access to its gated models; admins and the repo's creator keep direct access. ## Whose token reaches the Hub `huggingface_hub` picks one token per request, in this order: 1. A short-lived token from a **Trusted Publishers** OIDC exchange, when `HF_OIDC_RESOURCE` is set (typically on GitHub Actions). 2. The **`HF_TOKEN`** environment variable. 3. The **token file** saved by `hf auth login`, at `HF_TOKEN_PATH` or `$HF_HOME/token`. Two consequences catch teams out. A stale `HF_TOKEN` secret silently wins over a fresh `hf auth login` inside the image. And if `HF_HUB_DISABLE_IMPLICIT_TOKEN=1` is set, a token is only sent implicitly on write calls, so a gated download goes out unauthenticated unless you pass `token=` explicitly. `hf auth whoami` shows which account the active token belongs to. ## Reading the error | Exception | What it tells you | Typical fix | |---|---|---| | `GatedRepoError` | The repo exists and is gated; this account is not on its authorized list | Use an approved account's token, or wait for manual approval | | `RepositoryNotFoundError` | Wrong `repo_id` or `repo_type`, or a private repo the request cannot see | Fix the id, or authenticate as an account with access | | `RevisionNotFoundError` | The pinned branch, tag or commit does not exist in the repo | Fix the revision | `GatedRepoError` is a **subclass** of `RepositoryNotFoundError`, kept that way for backward compatibility, so an `except` block must catch the gated case first. Transformers wraps the gated case in an `OSError` whose message begins "You are trying to access a gated repo", which is what most people see in a CI log. ## Fixing it for CI - **Pick the account deliberately.** The approved account must be the one whose token the job sends. A dedicated machine account works only if that account itself requested and received access. - **Prefer a fine-grained token.** The Hub's guidance for production is a fine-grained token with read access to the specific gated model, so a leak exposes that model and nothing else. A read token also works for repos its account can read, but reaches every one of them. - **Keep tokens out of images and logs.** Inject `HF_TOKEN` from the CI secret store at run time; one token per use lets you revoke one without breaking the others. - **Or store no token at all.** A Trusted Publishers **CI/CD identity** on your account lets a CI job exchange its OIDC identity for a read-only token with the `gated-repos` scope, valid for 60 minutes. It can read gated repos your account has access to, and cannot write or read your private repos. - **Fail fast.** Call `auth_check(repo_id)` at the start of the job, so an access problem fails in seconds with a clear message instead of halfway through a long pipeline. What the licence behind the gate permits is a separate licensing question; this answer is only about getting an authorized identity to the files. ## A quick triage order 1. Run `hf auth whoami` in the failing job and compare the account with the one that was approved. 2. Check whether `HF_TOKEN` or `HF_OIDC_RESOURCE` is set and overriding what you expected. 3. Check the token's permissions cover that repo. 4. Open the model page as that account and confirm the request is accepted, not pending.

  • Why must a Python except clause for GatedRepoError come before one for RepositoryNotFoundError?
    In `huggingface_hub`, `GatedRepoError` subclasses `RepositoryNotFoundError` for backward compatibility. If the broader clause comes first, it also catches the gated case, and the job reports a missing or private repo when the real problem is an account that has not been granted access.
  • How can a GitHub Actions job read a gated model without storing any Hugging Face token as a secret?
    Add a CI/CD identity under Trusted Publishers in your account's authentication settings and set `HF_OIDC_RESOURCE` to your username in the job. The `hf` CLI exchanges the runner's OIDC token for a read-only Hub token with the `gated-repos` scope that expires after 60 minutes. It reads gated repos your account was granted, nothing else.

  • Once one member of our organization is approved, every organization token can download the gated repo.
  • GatedRepoError means the repo id is misspelled.
  • Running hf auth login in the image fixes it even while a stale HF_TOKEN is set.
  • The CI job can request gated access itself with an API call before downloading.
  • A write token bypasses a gated repo's access list.