Skip to main content

How to Pin a Hugging Face Model to an Exact Revision

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A model on the Hugging Face Hub is a Git repository, and its owner can push new commits to it at any time. Without a pin, your code loads whatever is newest, so its answers can change one day without you changing a line. The revision argument pins it to one commit. This guide finds a model's commit and pins it with Transformers 5.24.1.

Syntax​

Syntax
AutoConfig.from_pretrained("<model>", revision="<commit hash, branch or tag>")
pipeline("<task>", model="<model>", revision="<commit hash, branch or tag>")
PartWhat it means
<model>The model on the Hub, as <owner>/<name>
revisionWhich version to load. A full commit hash pins it for good; a branch such as main follows its latest commit

The Transformers documentation describes revision as "The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co". Without it, the default is "main", the newest commit.

The Example​

distilbert/distilbert-base-uncased-finetuned-sst-2-english, a small sentiment model. The script loads only its configuration, a small JSON file, so it runs fast; models, tokenizers and pipelines take the same argument.

Prerequisites​

  • uv. This guide uses uv 0.13.10. The project uses Python 3.14, which uv downloads if needed.

Step 1: Create the Project​

Terminal
uv init --python 3.14 revision-demo
cd revision-demo
uv add transformers torch

Step 2: Find the Model's Current Commit​

hf models info asks the Hub about a model. --expand sha limits the answer to its current commit:

Terminal
uv run hf models info distilbert/distilbert-base-uncased-finetuned-sst-2-english --expand sha
Output
{
"id": "distilbert/distilbert-base-uncased-finetuned-sst-2-english",
"sha": "714eb0fa89d2f80546fda750413ed43d93601a13"
}

The terminal after uv run hf models info with --expand sha: JSON with the model's id and its sha, 714eb0fa89d2f80546fda750413ed43d93601a13.

The sha is the full commit hash, 40 characters long. The model page's Files and versions tab shows the same commits in its history.

Step 3: Load the Model at That Commit​

pinned.py
from transformers import AutoConfig

config = AutoConfig.from_pretrained(
"distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0fa89d2f80546fda750413ed43d93601a13",
)
print(config.id2label)
Terminal
uv run python pinned.py
Output
{0: 'NEGATIVE', 1: 'POSITIVE'}

The terminal after uv run python pinned.py: the model's two labels as a dictionary, 0 for NEGATIVE and 1 for POSITIVE.

The configuration came from that exact commit, and printed the model's two labels. If the owner pushes a new commit tomorrow, this code still loads this one.

The installation guide notes a second benefit: "If you pass a commit hash, Transformers uses the local cache for that commit's files and does not re-check the Hub for each one". With a branch name, it asks the Hub once which commit the branch points to.

Step 4: See What a Wrong Revision Does​

pinned_wrong.py
from transformers import AutoConfig

config = AutoConfig.from_pretrained(
"distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="v2",
)
print(config.id2label)
Terminal
uv run python pinned_wrong.py
Output (last lines)
) from e
OSError: v2 is not a valid git identifier (branch name, tag name or commit id) that exists for this model name. Check the model page at 'https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english' for available revisions.

The terminal after uv run python pinned_wrong.py: the last lines of a traceback ending in OSError: v2 is not a valid git identifier (branch name, tag name or commit id) that exists for this model name.

This model has no v2 tag, so loading stopped with an error that says what to check. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Pinning to mainmain moves whenever the owner pushes, so it pins nothingUse the full commit hash
A revision that doesn't existOSError: v2 is not a valid git identifier (branch name, tag name or commit id) that exists for this model name.Copy the hash from hf models info … --expand sha or the model's commit history
Pinning the model but not the tokenizerThey're loaded separately and could come from different commitsPass the same revision to both, or use a pipeline, which passes it to both

FAQ​

Can I use a short hash like 714eb0f?​

Transformers itself uses short hashes for its default models. A full hash can never be ambiguous, so it's the safer choice in your own code.

When should I move to a newer commit?​

When you've tested the newer version and want what it changes. Update the hash on purpose, like any other dependency.

Conclusion​

hf models info … --expand sha returned the model's commit, revision="714eb0fa…" loaded exactly that version, and a revision that doesn't exist stopped with a clear OSError. A pinned model can't change under your code.

Additional Resources​