hf-hub
AGENTS.md336 starsChanged 5 months agoApache-2.0View on GitHub
Async Rust client library for the Hugging Face Hub API. This is the Rust equivalent of the Python huggingface_hub library. The primary entry point is the HFClient struct, which wraps an Arc<HFClientInner> for cheap cloning. All methods are async and use reqwest as the HTTP client. Paginated endpoints return impl Stream<Item = Result<T>> via futures::stream::try_unfold. Methods take parameters via bon per-method builders finished with .send().await (mirroring reqwest / aws-sdk / octocrab style). Key capabilities: These rules apply to ALL code…
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
The file
Show its rules and text
We split this AGENTS.md into the rules it gives an agent
29 rules. About 0 are common, 18 are distinctive, and 11 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈18
Distinctive · estimate
This author's own choices. Worth a look.
11
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 11
Commit creation, commit history, diffs between revisions
- Distinctive: line 22
NO trivial comments — do not add comments that restate what the code does
- Very specific: line 24
No wildcard imports (e.g., use foo::*), except pub use re-exports in lib.rs
- Distinctive: line 25
All imports are at the top of the file or top of module
- Very specific: line 30
Use Result<T, E> with explicit error handling — never panic
- Distinctive: line 37
Prefer structured logging (tracing/log macros with fields, not string formatting)
- Very specific: line 41
Add all dependencies to Cargo.toml (workspace root) or hf-hub/Cargo.toml (crate-level)
- Distinctive: line 42
Prefer well-maintained crates from crates.io
- Very specific: line 49
Place in the same file using #[cfg(test)] modules
- Very specific: line 57
Run read-only tests: HF_TOKEN=HF_xxx cargo test -p integration-tests
- Very specific: line 58
Write operation tests (create/delete repos, upload files) require HF_TEST_WRITE=1
- Very specific: line 59
Run all tests including writes: HF_TOKEN=HF_xxx HF_TEST_WRITE=1 cargo test -p integration-tests
- Distinctive: line 65
ALWAYS run both after making changes — do not skip this step
- Distinctive: line 69
Verify that every change is minimal and necessary — do not include unrelated modifications
- Very specific: line 89
All public methods on HFClient, HFRepository, HFSpace, and HFBucket use bon's per-method #[builder(finish_fn = send)] pattern.
- Very specific: line 125
Every async method MUST have a manually-written blocking counterpart on the corresponding *Sync struct under #[cfg(feature = "blocking")].
- Very specific: line 158
When parameters need a backing struct for internal helpers (e.g., DownloadFileParams is plumbed through several private functions in …
- Distinctive: line 161
Never expose it publicly.
- Distinctive: line 261
Before writing any code:
- Distinctive: line 264
Plan: Write an implementation plan that includes testing strategy (unit tests, integration tests, manual verification steps).
- Distinctive: line 266
When changing public interfaces or adding user-facing capabilities:
- Very specific: line 268
ALWAYS update the relevant examples in examples/ and any affected README snippets so they match the current public API.
- Distinctive: line 269
ALWAYS add at least one example for new functionality unless an existing example already demonstrates that exact workflow clearly.
- Distinctive: line 270
Prefer examples that show the intended high-level interface, not just the lowest-level parameter structs, especially for new ergonomic APIs …
- Distinctive: line 274
When reviewing a pull request, follow these rules:
- Distinctive: line 279
Use phrases like "consider...", "what do you think about...", "we might want to..."
- Distinctive: line 281
When unsure, ask a clarifying question instead of assuming something is wrong
- Distinctive: line 295
Add inline comments at specific diff locations for targeted feedback
- Distinctive: line 297
Do NOT prefix substantive feedback (public API changes, correctness issues, missing tests) — these require attention
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
hf-hub
Async Rust client library for the Hugging Face Hub API. This is the Rust equivalent of the Python huggingface_hub library.
The primary entry point is the HFClient struct, which wraps an Arc<HFClientInner> for cheap cloning. All methods are async and use reqwest as the HTTP client. Paginated endpoints return impl Stream<Item = Result<T>> via futures::stream::try_unfold. Methods take parameters via bon per-method builders finished with .send().await (mirroring reqwest / aws-sdk / octocrab style).
Key capabilities:
- Repository info, listing, creation, deletion, and settings updates
- File upload, download, listing, and deletion
- Commit creation, commit history, diffs between revisions
- Branch and tag management
- User and organization info
- Xet high-performance transfers
Code Standards
These rules apply to ALL code written or modified in this repo:
Style
- NO trivial comments — do not add comments that restate what the code does
- Descriptive variable and function names
- No wildcard imports (e.g.,
use foo::*), exceptpub usere-exports inlib.rs - All imports are at the top of the file or top of module
- Latest stable Rust features are allowed
Error Handling
- Use
Result<T, E>with explicit error handling — never panic - Define custom error types using
thiserrorfor domain-specific errors - Provide helpful, actionable error messages
Performance
- Be mindful of allocations in hot paths
- Prefer structured logging (tracing/log macros with fields, not string formatting)
Dependencies
- Add all dependencies to
Cargo.toml(workspace root) orhf-hub/Cargo.toml(crate-level) - Prefer well-maintained crates from crates.io
- Shared dependencies belong in the workspace
[dependencies]table, not per-crate
Testing
Unit Tests
- Place in the same file using
#[cfg(test)]modules - Run:
cargo test -p hf-hub
Integration Tests
- Located in the
integration-testsworkspace crate, underintegration-tests/tests/(integration_test.rs,blocking_test.rs,bucket_sync_test.rs,bucket_xet_transfer_test.rs,cache_test.rs,download_test.rs,xet_transfer_test.rs). Shared helpers live inintegration-tests/src/test_utils.rs. - Require a valid
HF_TOKENenvironment variable and internet access - Tests skip gracefully when
HF_TOKENis not set (no failures) - Run read-only tests:
HF_TOKEN=HF_xxx cargo test -p integration-tests - Write operation tests (create/delete repos, upload files) require
HF_TEST_WRITE=1 - Run all tests including writes:
HF_TOKEN=HF_xxx HF_TEST_WRITE=1 cargo test -p integration-tests
Formatting and Linting
- Format:
cargo +nightly fmt - Lint:
cargo clippy -p hf-hub --all-features -- -D warnings - ALWAYS run both after making changes — do not skip this step
Minimal Changes
- Verify that every change is minimal and necessary — do not include unrelated modifications
WebAssembly compatibility
hf-hub supports wasm32-unknown-unknown; try not to break compatibility.
Filesystem- and tokio-runtime-dependent code is gated behind
#[cfg(not(target_family = "wasm"))] — keep new code out of wasm builds or
wasm-safe. Verify with ./scripts/verify_wasm.sh (CI runs the same check);
browser tests and runtime build flags live under wasm/.
Cargo features
| Feature | Default? | Notes |
blocking | off | Synchronous *Sync wrappers in blocking.rs. Pulls in tokio/rt. Native only. |
rustls-tls | off | Force the rustls TLS backend on reqwest's native build. No effect on wasm. |
default | (empty) | No features by default. |
Method builders (bon)
All public methods on HFClient, HFRepository, HFSpace, and HFBucket use bon's
per-method #[builder(finish_fn = send)] pattern. Do NOT introduce *Params wrapper structs —
parameters live directly on the method. Every method (including parameterless ones like info,
exists, pause, whoami) is finished with .send() for uniformity.
use bon::bon;
#[bon]
impl HFRepository {
#[builder(finish_fn = send)]
pub async fn my_method(
&self,
#[builder(into)] name: String,
revision: Option<String>,
) -> HFResult<MyOutput> { ... }
}Translation rules from typed-builder attributes (now removed):
| typed-builder | bon (as method param) |
#[builder(setter(into))] x: String | #[builder(into)] x: String |
#[builder(default, setter(strip_option))] x: Option<T> | x: Option<T> (implicit) |
#[builder(default, setter(into, strip_option))] x: Option<String> | #[builder(into)] x: Option<String> |
#[builder(default)] x: bool | #[builder(default)] x: bool |
#[builder(default = expr)] x: T | #[builder(default = expr)] x: T |
| required field, no attribute | x: T |
bon also generates a maybe_<field>(opt) setter alongside each Option<T> parameter — use it
when forwarding an existing Option<T> value (e.g., inside a sync mirror, or when porting a
helper that already has the field as an Option<T> local).
Mandatory blocking counterpart
Every async method MUST have a manually-written blocking counterpart on the corresponding
*Sync struct under #[cfg(feature = "blocking")]. Mirror the same #[builder] parameter list
verbatim. The body calls the inner async builder and blocks on the runtime. (self.runtime is a
RuntimeThread: the runtime lives on a background thread so sync methods are safe from inside
another tokio runtime, and its block_on takes any Send future — no 'static bound.)
#[cfg(feature = "blocking")]
#[bon]
impl crate::blocking::HFRepositorySync {
#[builder(finish_fn = send)]
pub fn my_method(
&self,
#[builder(into)] name: String,
revision: Option<String>,
) -> HFResult<MyOutput> {
self.runtime.block_on(
self.inner
.my_method()
.name(name)
.maybe_revision(revision)
.send(),
)
}
}For stream-returning methods, the sync counterpart collects into Vec<T>: pin the stream and
drain with while let Some (see existing examples in repository/listing.rs and
repository/mod.rs). Do NOT reintroduce the legacy sync_api! /
sync_api_stream! / sync_api_async_stream! macros — they were removed deliberately when the
crate moved to bon.
When parameters need a backing struct for internal helpers (e.g., DownloadFileParams is
plumbed through several private functions in repository/download.rs), keep that struct
private to the module with no #[derive(TypedBuilder)] and have the bon method assemble it.
Never expose it publicly.
Project Layout
Agents MUST update this section when adding new crates or large modules.
Types and API methods live together per component. Each component is either a single file (for smaller components) or a folder (when the impl block needs splitting). The user-facing types are re-exported flat at hf_hub::<component>::… (e.g., hf_hub::repository::CommitOperation); no hf_hub::types module exists.
Submodules inside a component folder are declared pub mod rather than mod. This makes the bon-generated builder structs (e.g., HFRepositoryListCommitsBuilder from repository/commits.rs) reachable through their canonical path so rustdoc renders them. The user-visible types still appear flat at hf_hub::<component>::… via the existing pub use re-exports — pub mod just adds the per-submodule path as an additional location where rustdoc documents items.
hf-hub/
├── Cargo.toml # Workspace root
├── AGENTS.md # This file (CLAUDE.md is a symlink to it)
├── .gitignore
├── hf-hub/ # Main library crate (package: hf-hub)
│ ├── Cargo.toml # Crate manifest, dependencies, features
│ ├── src/
│ │ ├── lib.rs # Module declarations, public re-exports, crate docs
│ │ ├── client.rs # HFClient, HFClientBuilder, HFClientInner, auth headers, URL builders
│ │ ├── blocking.rs # Synchronous *Sync handles (behind "blocking" feature)
│ │ ├── constants.rs # Env var names, default URLs, repo type helpers
│ │ ├── error.rs # HFError enum, HFResult alias, NotFoundContext
│ │ ├── pagination.rs # Generic paginate<T>() with Link header parsing
│ │ ├── retry.rs # Retry logic for transient HTTP failures
│ │ ├── progress.rs # ProgressEvent, ProgressHandler, Upload/DownloadEvent, FileProgress
│ │ ├── repository/ # Repo component: all HFRepository-bound APIs live here,
│ │ │ │ # flat public surface via `hf_hub::repository::…`
│ │ │ ├── mod.rs # HFRepository handle, RepoType, RepoInfo, ModelInfo/DatasetInfo/
│ │ │ │ # SpaceInfo, list/create/delete/move/update + flat re-exports
│ │ │ ├── commits.rs # Git history types + list_commits/list_refs/diff/branches/tags
│ │ │ ├── diff.rs # HFFileDiff, GitStatus, HFDiffParseError, parse_raw_diff, stream_raw_diff
│ │ │ ├── files.rs # Shared file types: BlobLfsInfo, RepoTreeEntry, FileMetadataInfo,
│ │ │ │ # CommitOperation, AddSource, CommitInfo, LastCommitInfo
│ │ │ ├── listing.rs # list_files, list_tree, get_paths_info, get_file_metadata
│ │ │ ├── download.rs # download_file, download_file_stream, download_file_to_bytes,
│ │ │ │ # snapshot_download (private helper structs live here)
│ │ │ └── upload.rs # upload_file, upload_folder, create_commit, delete_file/folder
│ │ │ # (private helper structs live here)
│ │ ├── spaces.rs # Spaces component: HFSpace handle, SpaceRuntime, SpaceVariable,
│ │ │ # runtime/hardware/secrets/variables/duplicate
│ │ ├── users.rs # Users component: User/Organization/OrgMembership, whoami,
│ │ │ # user+org lookup, followers/following
│ │ ├── xet.rs # Xet component (pub(crate)): XetConnectionInfo + xet transfer
│ │ │ # plumbing. Most methods are native-only (cached session, fs);
│ │ │ # the wasm `xet_download_stream` builds a fresh session per call
│ │ ├── buckets/
│ │ │ ├── mod.rs # HFBucket handle, bucket types, create/list/tree/batch/download
│ │ │ └── sync.rs # BucketSync* types, HFBucket::sync — plan computation and execution
│ │ └── cache/
│ │ ├── mod.rs # CachedFileInfo/CachedRepoInfo/HFCacheInfo + scan_cache API
│ │ └── storage.rs # pub(crate) on-disk plumbing: scan, locking, ref read/write, symlinks
│ ├── tests/
│ │ └── future_size.rs # Regression tests: heavyweight builder send() futures stay boxed/small
│ └── (unit tests live next to their modules in #[cfg(test)] blocks)
├── integration-tests/ # Integration tests crate (package: integration-tests)
│ ├── Cargo.toml # Depends on hf-hub with "blocking" feature
│ ├── src/
│ │ ├── lib.rs # Re-exports test_utils
│ │ └── test_utils.rs # Shared helpers: env-var names, token resolution, sha256_hex
│ └── tests/
│ ├── integration_test.rs # Read-only integration tests against live Hub API
│ ├── blocking_test.rs # Blocking wrapper parity tests
│ ├── bucket_sync_test.rs # Bucket sync plan/execution tests
│ ├── bucket_xet_transfer_test.rs # Bucket xet upload/download tests
│ ├── cache_test.rs # Cache scanning/locking tests
│ ├── download_test.rs # Download path tests
│ └── xet_transfer_test.rs # Xet transfer tests
├── examples/ # Example programs crate (package: examples)
│ ├── Cargo.toml # Crate manifest; each example declared with explicit path
│ └── *.rs # Flat example source files (repo, files, commits, buckets, users,
│ # spaces, progress, diff, download_upload, blocking_*, ...)
├── benches/ # Benchmark crate (package: hf-hub-benches)
│ ├── Cargo.toml # Criterion benchmark setup
│ └── sync_api.rs # Download/info benches for the sync API
├── wasm/ # WASM-only artifacts, excluded from the workspace
│ └── smoke/ # Tiny `wasm-bindgen` crate that exercises hf-hub's
│ # wasm-safe surface — verification target for the
│ # `wasm` CI job and `./scripts/verify_wasm.sh`
├── scripts/
│ └── verify_wasm.sh # `cargo check --target wasm32-unknown-unknown` for
│ # hf-hub + wasm/smoke
└── hfrs/ # CLI crate (package: hfrs)
├── Cargo.toml # Crate manifest, binary dependencies
├── src/
│ ├── main.rs # Binary entry point
│ ├── cli.rs # Clap definitions, Command enum, OutputFormat
│ ├── output.rs # JSON/table rendering for command results
│ ├── progress.rs # CliProgressHandler for upload/download progress
│ ├── util/ # Token management helpers
│ └── commands/ # Subcommand implementations (auth, buckets, cache,
│ # datasets, download, env, models, repos, spaces,
│ # upload, version)
└── tests/
├── cli_comparison.rs # CLI behavioral parity tests
└── helpers.rs # Test harness for invoking hfrs binaryFeature Development
Before writing any code:
- Branch: Confirm you are on a feature branch, not
main. If onmain, create a branch named<username>/<short-description>. - Plan: Write an implementation plan that includes testing strategy (unit tests, integration tests, manual verification steps). Add this plan as a comment on the PR.
When changing public interfaces or adding user-facing capabilities:
- ALWAYS update the relevant examples in
examples/and any affected README snippets so they match the current public API. - ALWAYS add at least one example for new functionality unless an existing example already demonstrates that exact workflow clearly.
- Prefer examples that show the intended high-level interface, not just the lowest-level parameter structs, especially for new ergonomic APIs like repo handles.
Code Review
When reviewing a pull request, follow these rules:
Tone
- Collegiate and constructive — write as a peer, not an authority
- Use phrases like "consider...", "what do you think about...", "we might want to..."
- Acknowledge good decisions and clean patterns, not just problems
- When unsure, ask a clarifying question instead of assuming something is wrong
What to Review
- Correctness — logic errors, edge cases, off-by-one errors
- Readability — naming consistency, code clarity, helpful error messages
- Maintainability — temporary workarounds tracked, types in the right crate, clean abstractions
- Testability — missing tests for new endpoints/logic, weakened assertions, coverage gaps
- Performance — unnecessary allocations in hot paths, unbounded response sizes, missing concurrency limits
- Security — auth checks on new routes, input validation, error message information leakage
How to Structure Feedback
- Post a summary comment on the PR: overview of the changes, key observations, cross-cutting concerns
- Add inline comments at specific diff locations for targeted feedback
- Prefix minor style suggestions with
nit:— these are optional and the author may skip them - Do NOT prefix substantive feedback (public API changes, correctness issues, missing tests) — these require attention
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

