Model File Formats: Pickle, .pt, Safetensors and GGUF, and Which Ones Can Run Code
For executives
A model is not one file but a bundle: weights, configuration, a tokenizer, often a chat template and sometimes Python code. Several of the formats used to store those pieces can run code when they are loaded. That is why downloading a model from a public hub and loading it is closer to installing software than to opening a document, and why unsafe deserialization is the most common vulnerability class in NVIDIA's AI software in 2026. Choosing safer formats and refusing to run model-supplied code removes most of the risk; knowing which format does what is the starting point.
Why model formats are an AI runtime issue
Training and inference pipelines load model files constantly, from public hubs, shared storage and other teams, on machines that hold GPUs, credentials and data. On a CPU workload, loading a data file rarely means running code. In the Python AI ecosystem it often does, because the most common formats were designed to rebuild arbitrary Python objects, not just store numbers.
The formats
| Format | Typical files | Can loading run code? |
|---|---|---|
| Python pickle | .pkl, .pickle | Yes. Loading can import any module and call any function the file names |
| PyTorch checkpoint | .pt, .pth, .bin | Yes with torch.load(weights_only=False); restricted with weights_only=True, the default since PyTorch 2.6 |
| Safetensors | .safetensors | No. A JSON header plus raw tensor bytes, no executable content |
| GGUF | .gguf | Weights cannot, but metadata such as a chat template can, if the server renders it unsafely |
| ONNX | .onnx | Not by itself: the graph is data, though custom operators rely on native libraries the application chooses to load |
| NumPy | .npy, .npz | Only with allow_pickle=True |
| Repository code | configuration with auto_map, modeling_*.py | Yes, when trust_remote_code is enabled |
Pickle and PyTorch checkpoints
Pickle stores objects as a small program for a stack-based virtual machine. When loaded, the program can import a module and call a function with arguments it supplies, through the __reduce__ method. A payload that runs a shell command is three lines.
A PyTorch .pt or .pth checkpoint is a ZIP archive whose index is a pickle, with the tensor data stored alongside. torch.load used to unpickle it fully by default. PyTorch 2.6 changed the default to weights_only=True, which only allows tensors and a short list of safe types. Code that sets weights_only=False, or runs an older PyTorch, is still exposed: NVIDIA's March 2026 Apex flaw (CVE-2025-33244, 9.0 Critical) affects environments with PyTorch earlier than 2.6.
Safetensors
Safetensors, created by Hugging Face, stores a JSON header describing each tensor's name, type and shape, followed by the raw bytes. Loading it reads numbers into memory and nothing else, which is why it has become the default format for weights on Hugging Face. It solves the weights problem only: tokenizers, configuration and templates still travel in other files.
Templates and metadata: GGUF and chat templates
GGUF, the format used by llama.cpp and many local inference tools, carries metadata alongside the weights, including a Jinja2 chat template. A template rendered without a sandbox is code: the Llama Drama flaw in llama-cpp-python (CVE-2024-34359, 9.7) and SGLang's CVE-2026-5760 (9.8) both turned a malicious template in a model file into code execution on the server.
Repository code: trust_remote_code
Some architectures ship their own Python modeling code in the model repository. Loaders run it only when trust_remote_code is enabled, and it is code from whoever controls the repository. The setting has itself been bypassed: in 2026, vLLM fixed flaws that loaded such code with the setting off (CVE-2026-22807), hard-coded it on for specific models (CVE-2026-4944), and relied on an assert check that disappears when Python runs with optimizations (CVE-2026-41523).
Scanners help, and are evaded
Hub scanners such as picklescan inspect pickles for dangerous imports. Attackers adapt: ReversingLabs' nullifAI (February 2025) used deliberately broken pickle files that the scanner could not parse but Python would still partly execute (The Hacker News). Scanning reduces the risk; it does not make pickle safe.
Detection
Whatever the format, malicious model content runs inside the process that loads it: the training job or inference server, with its GPU access and credentials. The signal is behavior at load time that the framework never shows otherwise: unexpected module imports, exec or eval calls, child processes, network connections or files written while a checkpoint, tokenizer or template is being loaded.
Practical guidance
- Use safetensors for weights wherever possible.
- Keep torch.load on weights_only=True, and PyTorch at 2.6 or later.
- Leave trust_remote_code off, and verify it is off in every loader in the stack.
- Render chat templates only in a sandboxed environment, and treat templates from untrusted models as code.
- Pin models by hash, not by name or tag, and load them only from storage you control.
Related articles
- Unsafe deserialization in AI frameworks: why loading a model or checkpoint can run code
- Inference server exploitation: Dynamo-Triton, Dynamo, TensorRT-LLM, vLLM and SGLang
- In-memory model weight tampering
- The AI software supply chain: images, packages, startup hooks and GPU binaries
- Hands Off: Detecting Model Weight Tampering (Stealthium research)