Skip to content

[hub] Add download command to the CLI#2266

Open
assafvayner wants to merge 2 commits into
mainfrom
worktree-add-download-to-cli
Open

[hub] Add download command to the CLI#2266
assafvayner wants to merge 2 commits into
mainfrom
worktree-add-download-to-cli

Conversation

@assafvayner

@assafvayner assafvayner commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Adds a download command group to the @huggingface/hub CLI, one subcommand per download library function.

Subcommands

command library fn destination
hfjs download file <repo> <path> [local-path] downloadFile arbitrary local path (default: basename in cwd), with a progress bar
hfjs download cache <repo> <path> downloadFileToCacheDir HF cache dir
hfjs download snapshot <repo> snapshotDownload HF cache dir (whole repo)

Common flags on all three: --repo-type {dataset|model|space}, --revision, --token, --quiet/-q. cache/snapshot also accept --cache-dir. Repo type can be given via flag or name prefix (datasets/...). Downloads use Xet (file passes xet: true explicitly; cache/snapshot inherit Xet-on by default).

download file on a missing file prints an error and exits non-zero without creating a file.

Implementation

  • New streamBlobToFile util streams a lazy Blob to disk with a cumulative-progress callback, removing the partial file on error. It's extracted from the CLI so it's unit-testable (cli.ts runs on import).
  • cli.ts gets the download command group, modeled on the existing branch/repo groups.
  • README updated with examples.

Testing

  • Unit tests for streamBlobToFile (exact bytes, cumulative progress, error cleanup).
  • Manual end-to-end against public repos: file (default + explicit path, quiet), not-found → exit 1 with no file, cache, and snapshot.

Note

Low Risk
Additive CLI and docs only; reuses existing library download functions with local file writes guarded on missing files.

Overview
Adds a download command group to the @huggingface/hub CLI so users can pull Hub content from the terminal, mirroring existing upload flows.

download file fetches one file via downloadFile (with xet: true), streams it to a local path (default: basename in cwd) using new streamBlobToFile (progress callback, deletes partial files on failure), optional cli-progress bar, and exits non-zero without writing when the path is missing. download cache and download snapshot call downloadFileToCacheDir and snapshotDownload into the HF cache (optional --cache-dir).

All subcommands share --repo-type, --revision, --token, and --quiet, with help wired like branch. The hub README documents npx/hfjs download examples and help download.

Reviewed by Cursor Bugbot for commit f9e1d4b. Bugbot is set up for automated code reviews on this repo. Configure here.

Add a `download` command group with three subcommands, one per download
library function:

- `download file <repo> <path> [local-path]` — downloadFile to an arbitrary
  local path, with a progress bar (xet: true)
- `download cache <repo> <path>` — downloadFileToCacheDir into the HF cache dir
- `download snapshot <repo>` — snapshotDownload the whole repo into the cache dir

Extract a tested `streamBlobToFile` helper to stream a lazy Blob to disk with
progress, removing the partial file on error.
@assafvayner assafvayner marked this pull request as ready for review July 1, 2026 01:04
@assafvayner assafvayner requested a review from coyotte508 as a code owner July 1, 2026 01:04
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really belong to a utils function, only used in cli, behavior too specific (eg catch error & unlink file)

Comment thread packages/hub/README.md
Comment on lines +128 to +135
# Download a single file to a local path (defaults to ./config.json)
npx @huggingface/hub download file coyotte508/test-model config.json
npx @huggingface/hub download file coyotte508/test-model config.json ./local-config.json
# Download a single file into the Hugging Face cache directory
npx @huggingface/hub download cache coyotte508/test-model config.json
# Download an entire repo into the Hugging Face cache directory
npx @huggingface/hub download snapshot coyotte508/test-model
npx @huggingface/hub download snapshot --repo-type dataset coyotte508/test-dataset

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moonbot what's the huggingface_hub's hf cli syntax?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants