huggingface_hub is the Python library (and hf CLI) that almost every Hugging Face workflow touches, from downloading models to pushing datasets. Version 2.5.0, released on 26 November 2026, adds several features that are useful for ML engineers and anyone running AI agents: a one-command way to revoke leaked tokens, network policies for sandboxes, SQL queries over Hub datasets with hf_dataset(), transactional writes to buckets, and a change in how chat completions are routed.
This tutorial walks through each feature with code. Signatures and help text were checked against the published huggingface_hub==2.5.0 package; the source is the official v2.5.0 release notes.
Install or upgrade
pip install --upgrade "huggingface_hub==2.5.0"
python -c "import huggingface_hub as h; print(h.__version__)" # 2.5.0
hf --help
1. Revoke leaked tokens with hf auth revoke
Leaked access tokens are one of the most common security incidents in ML projects. A token gets committed in a notebook, pasted into a log, or shared in a Colab link. Until now you had to log in to the website to revoke it. In 2.5.0 you can do it from the terminal:
hf auth revoke hf_xxx hf_yyy
# ✓ Revocation requested for 2 token(s)
Or from Python, which is handy in secret-scanning automation:
from huggingface_hub import revoke_tokens
revoke_tokens(["hf_xxx"]) # returns None once the request is accepted
Some details worth knowing, from the release notes and the function’s docstring:
- No login needed. Anyone holding the raw token value can revoke it, and the token owner gets an email.
- Your own token is safe. The request is deliberately sent without your
Authorizationheader, so you can never revoke your active token by mistake. - Batching.
revoke_tokens()sends tokens in batches of 1,000. - No oracle. The Hub always answers with a generic “revocation requested” message, whether the token existed or not.
Wire it into CI
If you already run a secret scanner in CI, you can revoke Hugging Face tokens it finds automatically. A minimal sketch:
import re
import sys
from pathlib import Path
from huggingface_hub import revoke_tokens
TOKEN_RE = re.compile(r"\bhf_[A-Za-z0-9]{30,}\b")
found = set()
for path in Path(".").rglob("*"):
if path.is_file() and path.suffix in {".py", ".ipynb", ".md", ".txt", ".env"}:
found.update(TOKEN_RE.findall(path.read_text(errors="ignore")))
if found:
revoke_tokens(sorted(found))
print(f"Revocation requested for {len(found)} token(s); rotate them now.")
sys.exit(1)
The regex is a simple heuristic, not an official token format specification, so tune it for your codebase. Revocation is permanent, so only run this on tokens you are sure have leaked.
2. Sandboxes with no internet, or an allowlist
huggingface_hub sandboxes give agents a remote container to run code in. Giving an agent unrestricted internet access is risky: it can exfiltrate data or pull untrusted packages. Version 2.5.0 adds network policies:
from huggingface_hub import Sandbox
# No outbound network at all (volumes still work)
sbx = Sandbox.create("python:3.12", network="none")
# Only reach an internal model server and the Hub
sbx = Sandbox.create(
"python:3.12",
network={"allow": ["10.0.0.0/8", "203.0.113.10:443", "huggingface.co"]},
)
The same from the CLI:
hf sandbox create --network none
hf sandbox create --network 10.0.0.0/8,huggingface.co:443
According to the CLI help, the allowlist accepts IP ranges, IPs and hostnames with optional ports, and only TCP is allowed. The release notes say the policy is enforced by the sandbox server with seccomp and Landlock, applies to every process in the sandbox, and can’t be removed from inside, even by root. Pools support it per sandbox, so one host can run isolated and connected sandboxes side by side.
One safety detail: if the server can’t apply the policy (for example, a pool host started with an older huggingface_hub), creation fails with a SandboxError instead of silently giving you a connected sandbox. Catch that error rather than retrying without a policy.
3. Query datasets with SQL using hf_dataset()
hf datasets sql now auto-installs and loads the Hugging Face DuckDB extension, so you can reference datasets by name:
hf datasets sql "SELECT * FROM hf_dataset('stanfordnlp/imdb') LIMIT 3"
That is much nicer than building read_parquet(...) URLs by hand, and it is ideal for quickly inspecting a dataset before you download it. Some ideas:
# How balanced are the labels?
hf datasets sql "SELECT label, COUNT(*) AS n FROM hf_dataset('stanfordnlp/imdb') GROUP BY label"
# Average review length
hf datasets sql "SELECT AVG(LENGTH(text)) AS avg_chars FROM hf_dataset('stanfordnlp/imdb')"
The extension requires DuckDB 2.0+. If it can’t be installed (older DuckDB or an unsupported platform), a warning is logged and existing read_parquet workflows keep working. As a side effect, the extension also enables writing back to the Hub with COPY ... TO 'hf://datasets/username/dataset-name/...'.
4. Transactional writes to buckets with HfFileSystem
HfFileSystem exposes the Hub through the fsspec interface. In 2.5.0 it supports fsspec transactions for buckets. Files written inside a transaction block are uploaded when the block ends, and nothing is uploaded if the block raises:
from huggingface_hub import HfFileSystem
fs = HfFileSystem()
with fs.transaction:
with fs.open("buckets/my-username/my-bucket/checkpoints/last.ckpt", "wb") as f:
f.write(checkpoint_bytes)
# not uploaded yet
# uploaded now
The practical win is for libraries that wrap every save in a transaction. The release notes mention PyTorch Lightning’s atomic checkpoint save, which previously couldn’t write to hf://buckets/... paths. Two limitations: transactions are best effort (each file is uploaded separately on commit, so a failure can leave earlier files uploaded), and they work for buckets only. Repo writes, rm and cp_file inside a transaction raise NotImplementedError before anything is uploaded or deleted.
5. Chat completions now go through the Hub router
If you call InferenceClient.chat_completion() with a Hugging Face token and a specific provider, requests now go to the Hub router at /v1/chat/completions with the model set to model-id:provider. Before, they called provider-specific routes directly.
from huggingface_hub import InferenceClient
client = InferenceClient(provider="together") # uses your HF token
reply = client.chat_completion(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=[{"role": "user", "content": "One tip for faster fine-tuning?"}],
)
print(reply.choices[0].message.content)
Your code doesn’t change, but the path does: pinned-provider calls now follow the same route as auto calls, with automatic failover if a provider is down or rate limited. Calls made with a provider’s own API key still go directly to that provider. Check the provider and model availability on the Hub before relying on a specific pair.
Other changes to know
- Spaces OAuth without a client secret. PKCE is always on, and
OAUTH_CLIENT_SECRETis optional. Session cookies are now signed with a key fromSPACE_SIGNING_SECRET, so users of OAuth Spaces will need to log in again once the Space upgrades. EgressLimitErroris raised on HTTP 402EgressLimitReached.- Jobs fixes: names stay within 100 characters, clearer errors for invalid
--timeout, and a crash on longrun_uv_jobarguments is fixed.
Takeaway
huggingface_hub 2.5.0 is a security-minded release. Add hf auth revoke to your incident runbook, run agent sandboxes with network="none" or an allowlist by default, and use hf datasets sql with hf_dataset() to explore data before downloading it. If you checkpoint to Hub buckets, transactions finally make atomic-style saves work. Read the CLI guide and the sandbox guide for the full options.
