Skip to main content

hfpin

Pin the Hugging Face models and datasets in your code to exact commits.

# Before: downloads whatever is on the Hub today
model = AutoModel.from_pretrained("google-bert/bert-base-uncased")

# After `hfpin pin`: always the same files
model = AutoModel.from_pretrained("google-bert/bert-base-uncased", revision="86b5e0934494bd15c9632b12f734a8a67f723594")

Why

from_pretrained("org/model") downloads the latest commit of the main branch. That commit can change at any time:

  • Silent changes. The owner updates weights, tokenizer or config, and your results change with no code change on your side.
  • Supply chain attacks. If the owner's account is compromised, the attacker's files go straight into your app. With trust_remote_code=True, that includes Python code that runs on your machine.
  • Reproducibility. A paper, a test, or a training run from last month may not give the same result today.

Pinning a commit sha (revision="<sha>") fixes all three. It is the same idea as pinning GitHub Actions to a sha, and Hugging Face recommends it. Doing it by hand means looking up a 40-character sha for every model, which is why almost nobody does. hfpin does it for you.

Install

pip install hfpin

Requires Python 3.10+. No dependencies.

Usage

Find unpinned downloads

$ hfpin check
train.py:5:7: unpinned model 'bert-base-uncased' (AutoTokenizer.from_pretrained): add revision=<commit sha>
train.py:10:7: model 'distilbert/distilbert-base-uncased-finetuned-sst-2-english' (pipeline) uses revision 'main', which can change: use a commit sha
train.py:11:6: unpinned dataset 'stanfordnlp/imdb' (load_dataset): add revision=<commit sha>
train.py:13:7: warning: trust_remote_code runs Python code from 'microsoft/phi-2' and the code can change at any time

3 problem(s) found. Run `hfpin pin` to fix.

check works offline and exits with code 1 when it finds a problem, so it fits in CI and pre-commit hooks.

Options:

Option What it does
--online Also checks that every repo and revision exists on the Hub. Catches typos, model names made up by AI assistants, and renamed repos (see below).
--deny-license PATTERN Fails on models or datasets whose license matches, for example --deny-license "cc-by-nc*". Repeatable. Implies --online.
--allow-mutable Accepts branch and tag revisions such as revision="v1.0".
--format text|json|github Output format. github prints workflow annotations.
--exclude GLOB Skips matching files. Repeatable.

Pin them

$ hfpin pin
train.py:5:7: bert-base-uncased@main -> 86b5e0934494 (now 'google-bert/bert-base-uncased', use --rename)
train.py:10:7: distilbert/distilbert-base-uncased-finetuned-sst-2-english@main -> 714eb0fa89d2
train.py:11:6: stanfordnlp/imdb@main -> e6281661ce1c
Pinned 3 download(s) in 1 file(s).

pin looks up the current commit of each repo (or of the branch or tag you already use) and edits only the call, keeping your formatting. Use --dry-run to see a diff first. For private or gated repos, log in with huggingface-cli login or set HF_TOKEN.

To update a model later, delete its revision= and run hfpin pin again.

Fix renamed and legacy ids

Many popular repos moved under an organization, for example gsm8k is now openai/gsm8k and bert-base-uncased is now google-bert/bert-base-uncased. The Hub still redirects the old names, but huggingface_hub 2.x rejects ids without a namespace:

HfUriError: Invalid HF URI 'hf://datasets/gsm8k@.../.huggingface.yaml'. Repository id must be 'namespace/name', got 'gsm8k'.

hfpin check --online reports these. Ids without a namespace are errors, and other renamed repos are warnings. hfpin pin --rename replaces them with the canonical id (and pins them too):

$ hfpin pin --rename
gsm8k.py:12:19: gsm8k -> openai/gsm8k, gsm8k@main -> 740312add88f
Pinned 1 download(s), renamed 1 id(s) in 1 file(s).

GitHub Action

- uses: actions/checkout@v4
- uses: DipPansuriya/hfpin@v0.2.0
  with:
    paths: src          # default: .
    args: --online --deny-license "cc-by-nc*"   # optional
    hf-token: ${{ secrets.HF_TOKEN }}           # optional, for private or gated repos

Problems show up as annotations on the pull request diff.

What it detects

Call Repo type
*.from_pretrained(...) (transformers, diffusers, peft, timm, ...) model
pipeline(task, model=...) model
SentenceTransformer(...) model
hf_hub_download(...), snapshot_download(...) model, dataset or space (repo_type=)
load_dataset(...) dataset

It only looks at repo names written as string literals. It skips local paths, built-in dataset loaders such as load_dataset("csv", ...), and calls whose revision= is a variable.

To ignore a call, add # hfpin: ignore on any of its lines.

Compared with Bandit

Bandit rule B615 warns about unpinned Hugging Face downloads. hfpin also fixes them, checks that the repos exist, and checks licenses.

License

MIT

Metadata

Release files for hfpin 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hfpin 0.2.0
File Size Uploaded Provenance Actions
hfpin-0.2.0.tar.gz 14.1 kB With provenance Details for hfpin-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hfpin 0.2.0
File Interpreter ABI Platform Actions
hfpin-0.2.0-py3-none-any.whl Python 3 none any Details for hfpin-0.2.0-py3-none-any.whl

Total release size: 27.1 kB

Release files / hfpin-0.2.0.tar.gz

Download URL hfpin-0.2.0.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f3acbee369a98206c7b2597cdd66107166a5169d47717a1925dd1e48747c104a
BLAKE2b-256 checksum
How to use checksums
a9023732c305d8669d74a488f91ca3d7d26fbbb8c74a44b2d6a3917bffcae63f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / hfpin-0.2.0-py3-none-any.whl

Download URL hfpin-0.2.0-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f2a7399f8d0e0ddbc168d8561bae0a7c15764ad0d8cc1140e312d9bfa2d449d
BLAKE2b-256 checksum
How to use checksums
d64da05a0dfccdd21494be0ae38a22fde513a49a1941931e7ba6c9965694b9ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for hfpin 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for hfpin-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page