Skip to content
mentoor

Topic 1 of 5 · 4 min read

Unsafe model file formats and loaders

Goal: Identify model-loading code that can run arbitrary code and switch it to a safe format.

A model file is a program until proven otherwise: some formats run code the moment you load them, so the format and the loader flags decide whether a download is safe.

Check yourself · 1 of 3

Why can loading a pickle-based model file be dangerous?

Choose one answer

Why it matters

Teams download weights from public hubs the way they install packages. Many older PyTorch checkpoints (.pt, .pth, .bin) are Python pickle files. Pickle is not just data: it can tell Python to import a module and call a function while it reads the file. The Python docs warn that unpickling untrusted data can execute arbitrary code. So torch.load("model.bin") on a file from a stranger can run the stranger's code, with your user's permissions, before a single tensor is used. Hubs scan uploads, but the Hugging Face docs are clear that scanning is a best-effort layer and not a guarantee.

How to do it

  1. Prefer a data-only format. Safetensors stores tensors and a small header and nothing else. Loading it cannot run code. For GGUF, ONNX and similar formats, treat the loader as parsing untrusted input and keep it updated.
  2. Turn off code execution in loaders. In transformers, trust_remote_code=True downloads and runs Python from the model repo. Leave it off by default. If a model truly needs it, read the code first and pin the exact commit.
  3. Restrict pickle when you cannot avoid it. Use torch.load(path, weights_only=True), which limits what can be unpickled. Convert the file to safetensors once, in a sandbox, then discard the original.
  4. Pin and verify. Download a specific revision, not main, and check its hash or signature (see the provenance topic).
  5. Load in a sandbox. Run first-time loads in a container with no network and no credentials.

Before and after

# Before: arbitrary code can run on load
from transformers import AutoModel
import torch

model = AutoModel.from_pretrained("someone/cool-model", trust_remote_code=True)
state = torch.load("weights.bin")
# After: data-only format, pinned revision, no remote code
from transformers import AutoModel

model = AutoModel.from_pretrained(
    "someone/cool-model",
    revision="3f2a9c1",          # a commit, not a moving branch
    use_safetensors=True,        # refuse pickle files
    trust_remote_code=False,
)

What goes wrong

  • "The hub scanned it." A scanner looks for known bad patterns. A new trick can pass, so a clean badge is not proof.
  • Copying a snippet. An AI assistant or a README often adds trust_remote_code=True to make an error go away. That one flag trades safety for convenience.
  • Moving revisions. A repo you vetted last month can change. Loading main loads whatever is there today.
  • Loading on a laptop with cloud keys. The code runs as you, with your tokens and SSH keys in reach.

Examples

The helpful error fix

model = AutoModel.from_pretrained("acme/chat-7b")
# ValueError: requires you to execute the configuration file ... set trust_remote_code=True

An assistant suggests adding trust_remote_code=True. Before accepting, open the repo's .py files and read what they do on import. If you cannot justify it, pick a model that ships its architecture inside the library.

Converting an old checkpoint

import torch
from safetensors.torch import save_file

# Run inside a throwaway container with no network
state = torch.load("old.bin", weights_only=True)
save_file(state, "model.safetensors")

The risky load happens once, in isolation. Everything after that uses the safe file.

Key points

  • Pickle-based checkpoints can execute arbitrary code when they are loaded, before any tensor is used.
  • Safetensors holds only tensors and a header, so loading it cannot run code.
  • trust_remote_code=True runs Python from the model repository and should stay off unless you have read and pinned that code.
  • Pin a model to a specific revision and load first-time files in a sandbox with no network or credentials.
  • Hub scanning reduces risk but is not a guarantee; a clean badge is not proof.

Common mistakes

  • Treating a model download as data, not as code that will run on your machine.
  • Adding trust_remote_code=True because a snippet or an assistant said it fixes an error.
  • Loading a moving branch name instead of a pinned commit.
  • Relying on a hub's scan badge instead of using a data-only format.

Check yourself · 2 of 3

Which model storage format is designed so that loading it cannot run code?

Choose one answer

Check yourself · 3 of 3

An assistant tells you to add trust_remote_code=True to fix a loading error. What is the safest first step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic