How to Pin a Hugging Face Model to an Exact Revision
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
AutoConfig.from_pretrained("<model>", revision="<commit hash, branch or tag>")
pipeline("<task>", model="<model>", revision="<commit hash, branch or tag>")
| Part | What it means |
|---|---|
<model> | The model on the Hub, as <owner>/<name> |
revision | Which 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
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:
uv run hf models info distilbert/distilbert-base-uncased-finetuned-sst-2-english --expand sha
{
"id": "distilbert/distilbert-base-uncased-finetuned-sst-2-english",
"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
from transformers import AutoConfig
config = AutoConfig.from_pretrained(
"distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0fa89d2f80546fda750413ed43d93601a13",
)
print(config.id2label)
uv run python pinned.py
{0: 'NEGATIVE', 1: '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
from transformers import AutoConfig
config = AutoConfig.from_pretrained(
"distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="v2",
)
print(config.id2label)
uv run python pinned_wrong.py
) 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.

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
| Mistake | What happens | Do this instead |
|---|---|---|
Pinning to main | main moves whenever the owner pushes, so it pins nothing | Use the full commit hash |
| A revision that doesn't exist | OSError: 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 tokenizer | They're loaded separately and could come from different commits | Pass 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
- Pipelines: revision — the
revisionargument. - Installation: cache directory — how a pinned commit uses the cache.
- Pin a revision — the same idea in huggingface_hub.
