{"course":{"slug":"hugging-face","title":"Hugging Face, End to End","url":"https://addaly.com/learn/hugging-face","level":"intermediate","track":"create"},"lesson":{"slug":"safetensors-and-pickle","title":"Loading a model can run someone else's code","url":"https://addaly.com/learn/hugging-face/safetensors-and-pickle","position":6,"of":75,"minutes":9,"answer":"Safetensors stores tensor data without pickle instructions. Loading custom repository code is a separate trust decision. Check the format, the code and the exact revision before running a model.","keyIdea":"Safetensors stores tensor data without pickle instructions. Loading custom repository code is a separate trust decision. Check the format, the code and the exact revision before running a model."},"body":{"format":"markdown","text":"## Separate the weights from the code\n\nBefore loading a model, ask two questions. What format stores the numerical weights? What software will the loading process run? The answers are separate.\n\nSome PyTorch checkpoints use pickle to reconstruct Python objects. Loading an unrestricted pickle from an untrusted source can run code in your process. The `.pt` and `.bin` extensions alone do not tell you how a file was made. Current PyTorch defaults to a restricted `weights_only` loader for normal `torch.load` calls that do not supply a custom pickle module. This limits which objects can be rebuilt; it is still not a reason to open an unknown checkpoint. If a file fails to load, do not disable that protection just to make it open. See [PyTorch serialization notes](https://docs.pytorch.org/docs/2.15/notes/serialization.html).\n\n## What safetensors stores\n\nThe file contains a length prefix, a JSON header that lists the tensor shapes, types and byte offsets, and the tensor bytes. There is no pickle instruction stream in that format. Offsets also let compatible tools inspect the header or read selected tensor data without loading everything at once. The exact performance depends on your hardware and loader. See the [safetensors format and library](https://huggingface.co/docs/safetensors/en/index).\n\n## A loading example with two explicit choices\n\nThis example uses an architecture supported by Transformers. It downloads model weights, so read the code first if you only want to inspect the repository.\n\n```python\nfrom transformers import AutoModel\n\nmodel = AutoModel.from_pretrained(\n    \"openai/whisper-tiny\",\n    revision=\"169d4a4341b33bc18d8881c4b69c2e104e1cc0af\",\n    use_safetensors=True,\n    trust_remote_code=False,\n)\n```\n\n`use_safetensors=True` asks Transformers to use safetensors rather than falling back to a different weights format. A load can still fail for many other reasons. Read the error before assuming that the format is missing.\n\n`trust_remote_code=False` keeps the Hub repository's custom model code out of this load. Enabling the flag lets Transformers run custom Python from the repository. Safetensors does not protect that separate route. A pinned revision makes the choice reproducible; it does not tell you that the pinned code is safe. If you cannot establish that you trust the code, choose a supported alternative. The [Transformers loading guide](https://huggingface.co/docs/transformers/en/models#custom-models) explains the distinction.\n\n## What a scan can and cannot tell you\n\nThe Hub reports scan results on files. The [Hub's description of pickle scanning](https://huggingface.co/docs/hub/security-pickle) calls out limits of import inspection. A clear scan, a popular organisation and a familiar file extension do not replace knowing what you are loading. Reading the imports is a start when reviewing code; it is not a complete review.\n\n## Try it in the browser\n\nOpen the Whisper Tiny repository at the pinned revision above. Find the safetensors weights and `config.json`. Write down the file format, the architecture and the revision. Now imagine the usage snippet asked you to enable custom code. Which of those three facts would change, and what new trust decision would you have to make? You can answer without installing anything or running a model."},"check":{"question":"A team standardises on `use_safetensors=True` for every model they load, and also loads a new vision-language architecture with `trust_remote_code=True`. What risk remains?","options":[{"text":"None in practice: the Hub's pickle scanner covers repository Python files, so a clean scan plus safetensors closes both paths.","correct":false,"tempting":"Overstates the scanner, which targets pickle opcode streams and is heuristic rather than a guarantee for arbitrary Python."},{"text":"Safetensors files can still carry executable payloads in their JSON header metadata, which the loader parses before reading tensors.","correct":false,"tempting":"The header describes data. The remaining risk in this scenario is the separate decision to run custom Python."},{"text":"The remote code is imported and run regardless of weight format, with the user's full permissions.","correct":true},{"text":"Mixing the two flags forces transformers to fall back to the pickle loader, silently undoing the safetensors preference.","correct":false,"tempting":"Invents an interaction between two independent options."}],"why":"Safetensors changes the weight format. Enabling custom code still allows the repository’s Python to run with the permissions of the loading process."},"prev":{"slug":"inside-a-repository","title":"What the files in a model repository are for","url":"https://addaly.com/learn/hugging-face/inside-a-repository"},"next":{"slug":"four-shelves","title":"Four shelves, and picking the wrong one costs a weekend","url":"https://addaly.com/learn/hugging-face/four-shelves"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Loading a model can run someone else's code', in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/safetensors-and-pickle (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Loading a model can run someone else’s code","courseTitle":"Hugging Face, End to End","module":{"index":1,"slug":"the-hub-as-a-repository-host","title":"The Hub, read properly"},"answer":null,"keyIdea":"Safetensors stores tensor data without pickle instructions. Loading custom repository code is a separate trust decision. Check the format, the code and the exact revision before running a model.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"separate-the-weights-from-the-code","runs":[{"text":"Separate the weights from the code"}]},{"t":"p","runs":[{"text":"Before loading a model, ask two questions. What format stores the numerical weights? What software will the loading process run? The answers are separate."}]},{"t":"p","runs":[{"text":"Some PyTorch checkpoints use pickle to reconstruct Python objects. Loading an unrestricted pickle from an untrusted source can run code in your process. The "},{"text":".pt","code":true},{"text":" and "},{"text":".bin","code":true},{"text":" extensions alone do not tell you how a file was made. Current PyTorch defaults to a restricted "},{"text":"weights_only","code":true},{"text":" loader for normal "},{"text":"torch.load","code":true},{"text":" calls that do not supply a custom pickle module. This limits which objects can be rebuilt; it is still not a reason to open an unknown checkpoint. If a file fails to load, do not disable that protection just to make it open. See "},{"text":"PyTorch serialization notes","href":"https://docs.pytorch.org/docs/2.15/notes/serialization.html"},{"text":"."}]},{"t":"h2","id":"what-safetensors-stores","runs":[{"text":"What safetensors stores"}]},{"t":"p","runs":[{"text":"The file contains a length prefix, a JSON header that lists the tensor shapes, types and byte offsets, and the tensor bytes. There is no pickle instruction stream in that format. Offsets also let compatible tools inspect the header or read selected tensor data without loading everything at once. The exact performance depends on your hardware and loader. See the "},{"text":"safetensors format and library","href":"https://huggingface.co/docs/safetensors/en/index"},{"text":"."}]},{"t":"h2","id":"a-loading-example-with-two-explicit-choices","runs":[{"text":"A loading example with two explicit choices"}]},{"t":"p","runs":[{"text":"This example uses an architecture supported by Transformers. It downloads model weights, so read the code first if you only want to inspect the repository."}]},{"t":"code","lang":"python","label":"Python","text":"from transformers import AutoModel\n\nmodel = AutoModel.from_pretrained(\n    \"openai/whisper-tiny\",\n    revision=\"169d4a4341b33bc18d8881c4b69c2e104e1cc0af\",\n    use_safetensors=True,\n    trust_remote_code=False,\n)"},{"t":"p","runs":[{"text":"use_safetensors=True","code":true},{"text":" asks Transformers to use safetensors rather than falling back to a different weights format. A load can still fail for many other reasons. Read the error before assuming that the format is missing."}]},{"t":"p","runs":[{"text":"trust_remote_code=False","code":true},{"text":" keeps the Hub repository’s custom model code out of this load. Enabling the flag lets Transformers run custom Python from the repository. Safetensors does not protect that separate route. A pinned revision makes the choice reproducible; it does not tell you that the pinned code is safe. If you cannot establish that you trust the code, choose a supported alternative. The "},{"text":"Transformers loading guide","href":"https://huggingface.co/docs/transformers/en/models#custom-models"},{"text":" explains the distinction."}]},{"t":"h2","id":"what-a-scan-can-and-cannot-tell-you","runs":[{"text":"What a scan can and cannot tell you"}]},{"t":"p","runs":[{"text":"The Hub reports scan results on files. The "},{"text":"Hub’s description of pickle scanning","href":"https://huggingface.co/docs/hub/security-pickle"},{"text":" calls out limits of import inspection. A clear scan, a popular organisation and a familiar file extension do not replace knowing what you are loading. Reading the imports is a start when reviewing code; it is not a complete review."}]},{"t":"h2","id":"try-it-in-the-browser","runs":[{"text":"Try it in the browser"}],"role":"try"},{"t":"p","runs":[{"text":"Open the Whisper Tiny repository at the pinned revision above. Find the safetensors weights and "},{"text":"config.json","code":true},{"text":". Write down the file format, the architecture and the revision. Now imagine the usage snippet asked you to enable custom code. Which of those three facts would change, and what new trust decision would you have to make? You can answer without installing anything or running a model."}]}],"check":{"question":[{"text":"A team standardises on "},{"text":"use_safetensors=True","code":true},{"text":" for every model they load, and also loads a new vision-language architecture with "},{"text":"trust_remote_code=True","code":true},{"text":". What risk remains?"}],"options":[{"text":[{"text":"None in practice: the Hub’s pickle scanner covers repository Python files, so a clean scan plus safetensors closes both paths."}],"correct":false,"tempting":[{"text":"Overstates the scanner, which targets pickle opcode streams and is heuristic rather than a guarantee for arbitrary Python."}]},{"text":[{"text":"Safetensors files can still carry executable payloads in their JSON header metadata, which the loader parses before reading tensors."}],"correct":false,"tempting":[{"text":"The header describes data. The remaining risk in this scenario is the separate decision to run custom Python."}]},{"text":[{"text":"The remote code is imported and run regardless of weight format, with the user’s full permissions."}],"correct":true,"tempting":null},{"text":[{"text":"Mixing the two flags forces transformers to fall back to the pickle loader, silently undoing the safetensors preference."}],"correct":false,"tempting":[{"text":"Invents an interaction between two independent options."}]}],"order":[0,1,3,2],"why":[{"text":"Safetensors changes the weight format. Enabling custom code still allows the repository’s Python to run with the permissions of the loading process."}]},"prev":{"slug":"inside-a-repository","title":"What the files in a model repository are for","minutes":10},"next":{"slug":"four-shelves","title":"Four shelves, and picking the wrong one costs a weekend","minutes":8}}}