High severity beginner · Fix: 2-5 min

EnvironmentError

huggingface_hub.utils._errors.EnvironmentError

What this error means
Huggingface Hub raises EnvironmentError when a private model is accessed without a valid authentication token.

Stack trace

traceback
huggingface_hub.utils._errors.EnvironmentError: token required to access private model
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/utils/_errors.py", line 45, in raise_for_status
    raise EnvironmentError("token required to access private model")
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/hf_api.py", line 123, in model_info
    raise_for_status(response)
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/hf_api.py", line 456, in model_info
    return self._request(
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/hf_api.py", line 400, in _request
    raise EnvironmentError("token required to access private model")
QUICK FIX
Set the HF_TOKEN environment variable to a valid Huggingface access token before accessing private models.

Why it happens

This error occurs because the Huggingface Hub requires an authentication token to access private models. Without setting the HF_TOKEN environment variable or passing a valid token, the API refuses access and raises this EnvironmentError.

Detection

Check for EnvironmentError exceptions when calling Huggingface Hub methods that fetch private models. Logging the exception message 'token required to access private model' helps identify missing authentication.

Causes & fixes

1

No HF_TOKEN environment variable set for authentication

✓ Fix

Set the HF_TOKEN environment variable with a valid Huggingface access token before running the code.

2

Using Huggingface Hub API without passing the token parameter for private models

✓ Fix

Pass the token explicitly to the API client or methods when accessing private models.

3

Expired or revoked Huggingface access token

✓ Fix

Generate a new access token from Huggingface website and update the HF_TOKEN environment variable.

Code: broken vs fixed

Broken - triggers the error
python
from huggingface_hub import HfApi

api = HfApi()
model_info = api.model_info("private-user/private-model")  # Raises EnvironmentError: token required
Fixed - works correctly
python
import os
from huggingface_hub import HfApi

os.environ["HF_TOKEN"] = "your_valid_token_here"  # Set your token securely

api = HfApi(token=os.environ["HF_TOKEN"])
model_info = api.model_info("private-user/private-model")  # Works with token
print(model_info.modelId)
Added HF_TOKEN environment variable and passed it to HfApi to authenticate and access the private model successfully.

Workaround

Catch EnvironmentError and prompt the user to set the HF_TOKEN environment variable or provide a token dynamically before retrying the request.

Prevention

Always store and load Huggingface access tokens securely in environment variables or secret managers and pass them explicitly when accessing private models.

Python 3.7+ · huggingface_hub >=0.10.0 · tested on 0.14.1
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.