Skip to content

Interview question · Software, Data & Model Supply Chain

Why are pickle-based model files dangerous, and what does safetensors fix and not fix?

Half the answer is 'pickle runs code'. The half that scores is everything safetensors leaves open.

mediumEssentialNewPreparation tags: Hugging Face, Google, MicrosoftUpdated Nov 2026

TL;DR: Pickle is a serialization format whose loader executes construction steps embedded in the file, so loading an untrusted pickle checkpoint runs the author's code with your permissions. Safetensors stores only a JSON header and raw tensor bytes, so loading cannot execute code. It does not fix backdoored or poisoned weights, custom model code loaded with trust_remote_code, malicious tokenizers, configs or notebooks shipped alongside, loader parser bugs, or tampering without hash and signature checks.

How to approach it

Explain the pickle mechanism, then safetensors' design, then spend as long on the limits as on the fix. Close with the intake process you would actually run.

A strong answer

FIG · PICKLE VS SAFETENSORS · WHAT LOADING CAN DO
Safetensors removes code execution on load, not every risk

Pickle checkpoint

Embedded construction steps

any callable

torch.load runs them

with your permissions

Author's code executes

Outcome, unsafe: Code execution

Never load untrusted pickles

Safetensors

JSON header + raw tensors

no code

Loading reads bytes

nothing executes

Still: backdoored weights, remote code

Outcome, safe: Safe to load

Provenance still matters

Why pickle is dangerous. Pickle encodes a small program for the unpickler, and objects can specify callables to invoke during reconstruction. A checkpoint saved with traditional torch.save is a pickle archive; loading it with an unrestricted loader executes whatever the uploader embedded. Malicious models have been found on public hubs. PyTorch 2.6 changed torch.load to default to weights_only=True, which restricts unpickling to tensors and allowlisted types, but older code, other libraries and explicit opt-outs remain common.

What safetensors fixes. Its structure is an 8-byte header length, a JSON header (tensor names, dtypes, shapes, offsets) and raw bytes. A loader parses numbers; nothing in the file selects code to run. It also memory-maps efficiently, which helped adoption.

What it does not fix.

Remaining riskWhyControl
Backdoors and poisoningBehaviour lives in the weightsProvenance, evaluation and red teaming; detection is limited
trust_remote_codeExecutes Python from the model repositoryReview and pin the commit, or avoid
Companion filesTokenizers, configs, scripts, notebooksInspect and allowlist what is loaded
Parser bugsAny parser of untrusted bytes can be exploitedPatch loaders; isolate
TamperingA modified safetensors file is still validHashes and signatures against trusted references
Licence and data issuesNot a format concernAI-BOM and review

Intake process. Prefer safetensors; convert legacy pickles once in an isolated sandbox; enforce weights_only=True; disallow remote code by default; pin model revisions by commit or hash; verify signatures where publishers provide them; scan for pickle opcodes and unexpected files; load and evaluate new models in an environment without credentials or egress; record the model in an AI-BOM.

What interviewers probe next

  • "How do model scanners detect malicious pickles?" They parse the pickle opcodes without executing them and flag imports and calls of dangerous functions; useful, but evasions exist, so they are a layer, not a guarantee.
  • "Is GGUF safe?" It is a data format, but its parsers have had vulnerabilities; treat loaders as attack surface and keep them patched.
  • "Where would you enforce this in a company?" A model registry or proxy as the only approved source, with checks at ingestion, plus policy that blocks direct hub downloads in production.

What if?

What if · interview follow-ups

Hold your answer as the constraints change

A data scientist downloads a model checkpoint from a public hub and loads it with torch.load on a laptop that has cloud credentials and source code access.

  1. Start here

    Why is loading a pickle checkpoint risky?

How it's scored

Weak: Says "use safetensors" and stops.

Hire: Explains pickle execution, safetensors' data-only design, and at least backdoors and remote code as limits.

Strong Hire: Covers the full limits table, weights_only defaults, scanning trade-offs, isolated conversion, and a centralised intake process.

Common mistakes

  • Believing a safe format means a safe model.
  • Loading unreviewed models with trust_remote_code=True.
  • Converting pickles on a machine with credentials.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 10, and we keep track of how far you get in every topic.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in doubles your free answers, from 5 to 10 per topic, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

No comments yet. Share a correction or a different way to answer it.