Quick fix
# One shot: switch to the mirror (add to ~/.bashrc to persist)
export HF_ENDPOINT=https://hf-mirror.com
# Verify immediately (small file, should succeed in seconds)
huggingface-cli download sshleifer/tiny-gpt2 config.json
# or python:
# from huggingface_hub import hf_hub_download
# hf_hub_download(repo_id="sshleifer/tiny-gpt2", filename="config.json")
What you will run into
Running AI projects in China, transformers / huggingface-cli model downloads hang at Fetching xx files or spin forever, then time out (Could not connect to huggingface.co-style).
You retry again and again thinking it is bandwidth — but the problem is not bandwidth, it is that huggingface.co itself is unreachable from CN networks.
Why this happens
The official Hugging Face domain (huggingface.co) has no acceleration for CN networks; direct connections are dropped at the transport layer (measured 2026-09-01: curl https://huggingface.co/ times out at 8s, code=000).
The CN mirror hf-mirror.com is stable (same test: 200, 0.6s).
Key mechanism: every download entry point in huggingface_hub (hf_hub_download / snapshot_download / huggingface-cli / transformers.from_pretrained) honors the HF_ENDPOINT env var — change one env var, all code works via the mirror with zero changes.
That is the core value: no code edits, no dependency changes.
Step by step
- First confirm it is a direct-connection problem (one-shot diagnosis):
curl -s -o /dev/null -w '%{http_code} %{time_total}s
' --max-time 8 https://huggingface.co/
# 000 / timeout = direct unreachable, go to step 2
curl -s -o /dev/null -w '%{http_code} %{time_total}s
' --max-time 8 https://hf-mirror.com/
# 200 = mirror available
- Set HF_ENDPOINT and retry (no code changes). Still failing, check the specific error:
- cert/proxy errors → check proxy env vars (see
proxy-env-varentry, case-sensitivity trap) andHF_HUB_DISABLE_XET=1(newer versions use the Xet protocol by default; some networks need it disabled to fall back to plain HTTP). not found/ 404 → wrong repo/file name; the mirror mirrors official structure, verify the path against the official URL.
- Cache location:
HF_HOME(default~/.cache/huggingface) controls the download cache; delete it to re-download, unrelated to the mirror. - Offline: copy the downloaded
HF_HOMEdirectory to an offline machine and set the sameHF_HOME— transformers loads from local cache first.
How to confirm it works
# 1. Env var active
echo $HF_ENDPOINT # should print https://hf-mirror.com
# 2. Actual download succeeds (timing visible)
huggingface-cli download sshleifer/tiny-gpt2 config.json
# local path printed = success; second download hits cache and returns instantly
Other environments
- transformers / langchain / vllm and other upper-layer libs: they all call huggingface_hub internally, so
HF_ENDPOINTworks for all of them — set it once, benefit everywhere. - ModelScope (Alibaba): works direct in China, no mirror needed;
modelscope download --model Qwen/Qwen2.5-0.5B-Instruct-GGUFmeasured working (2026-08-30, 491MB/58.5s). For projects using both ecosystems: setHF_ENDPOINTon the HF side, leave ModelScope alone. - Docker containers: containers do not inherit host env vars — pass
docker run -e HF_ENDPOINT=https://hf-mirror.comor composeenvironment:. - Windows:
set HF_ENDPOINT=https://hf-mirror.com(cmd) or$env:HF_ENDPOINT="https://hf-mirror.com"(PowerShell), same behavior.
Verification evidence
- 2026-09-01 measured on this machine (WSL Ubuntu 26.04 no proxy, huggingface_hub 1.29.0):
local repro log 2026-09-01 curl https://huggingface.co/: 8s timeout code=000 (direct unreachable, proven);curl https://hf-mirror.com/: 200 / 0.6s (mirror available, proven);- direct
hf_hub_download(sshleifer/tiny-gpt2, config.json): hung 12s, killed by timeout (exit=124, proven); - same call with
HF_ENDPOINT=https://hf-mirror.com: success in 1.2s, 662 bytes (proven); zero code changes.
Applicable tools (1)
This problem is linked from the tool pages below — reachable from either side.
pythonSource & evidence
← Coding Agents & Terminal domain | Problem Catalog Experience | Facing another issue? Go to the Helpdesk