
Open-source AI agent security starts with PyTorch 2.0: an untrusted model can execute code before inference begins. PyTorch `.pt` and `.pth` files use pickle by default, and Python pickle executes arbitrary code during deserialization. That means the dangerous moment is not necessarily when an agent answers a user or calls a tool. It can be the moment a developer, CI runner, training node, or production inference server loads a model file. (Yaklang AI/ML security guidance; AI Pentest Playbook)
For a small business or solo builder, the practical answer is simple: treat every unfamiliar model artifact as untrusted software.
Prefer SafeTensors, scan files before loading them, use `weights_only=True` for PyTorch loading guidance covering PyTorch 2.0 and later.
And keep model loading away from credentials and production systems.
The contrarian point is that the model itself is not the first thing to trust. The file format and loading path deserve the same scrutiny as any other dependency.
The exploit runs before the agent does
A malicious model file does not need to wait for inference. The cited security material states that loading an untrusted PyTorch model creates a code-execution risk before inference begins. No training and no “running the model” are required because deserialization is the exploit. (Byakugan)
That risk comes from pickle’s behavior.
Python pickle uses the `__reduce__` mechanism as an arbitrary-code-execution path.
So a crafted object can trigger behavior while the file is being reconstructed in memory.
The file may look like weights, but the loader is processing serialized Python objects.
The familiar PyTorch extensions do not make the problem safer. Pickle-based PyTorch model files include `.pt`, `.pth`, `.bin`. And `.ckpt`, while raw `.pkl` and `.pickle` files are classified as critical risk in the cited security guidance. (Yaklang AI/ML security guidance)
A crafted file can invoke `os.system(…)`, open a network socket, drop an additional payload, or exfiltrate credentials during loading.
That turns “I downloaded a model for my agent” into “I allowed code from that artifact to run in the environment that opened it.” (Byakugan)
Your laptop is part of the attack surface
Model loading is often treated as a harmless setup step. It is not harmless when the artifact is untrusted and the loader can execute code.
The affected environments include developer laptops, CI runners, training nodes, and production inference servers.
A model downloaded from a hub can therefore reach the machine that loads it, not just the process that later serves predictions. (AI Pentest Playbook)
That matters more for small operators given that one machine often carries several jobs.
A laptop may hold source code and credentials. A CI runner may have access to deployment steps. A production inference server may sit beside the data an agent is supposed to process.
The security mistake is assuming that a model is passive as its purpose is prediction.
A model artifact is still a file interpreted by software, and some formats contain behavior that activates during loading.
The same caution applies outside PyTorch.
Joblib files use pickle internally and are classified as high risk in the cited guidance. NumPy `.npy` and `.npz` files can enable code execution when `allow_pickle=True`, while Keras and HDF5 files can contain executable behavior through Lambda layers. (Yaklang AI/ML security guidance; Byakugan)
File formats give you a better first filter
You do not need to treat every format as identical. The format is an early decision point before you inspect the publisher, run a loader, or connect an agent to the artifact.
| Format | Security position in the cited guidance |
|—|—|
| `.pt` and `.pth` | Critical risk since pickle is used by default |
| `.pkl` and `.pickle` | Critical risk as raw pickle |
| `.joblib` | High risk given that it uses pickle internally |
| `.npy` and `.npz` | Medium risk when `allow_pickle=True` |
| `.h5` and `.keras` | High risk in one cited scanner as of Lambda layers |
| `.onnx` | Limited risk involving custom operators |
| `.gguf` | Data-only on load, with parser-abuse checks still required |
| `.safetensors` | Tensor-only and described as having no code-execution path |
The safer choices still require judgment. GGUF is described as data-only and not carrying code on load, but its parser should receive sanity checks. ONNX has limited risk involving custom operators, so “not pickle” does not mean “ignore the file.” (Byakugan)
SafeTensors is the strongest default in the supplied evidence.
It is described as a tensor-only format with no code-execution path. And it has no `__reduce__` mechanism or opcode virtual machine. Hugging Face Transformers heavily recommends SafeTensors to prevent arbitrary code execution on a user’s system and recommends preferring it over pickle-based formats. (Byakugan; Hugging Face Transformers security policy)
That does not mean a format choice replaces operational controls.
It means you can remove one major class of loading risk before asking an agent to use the model.
A practical loading policy for small builders
The right policy is not complicated, but it has to happen before the model enters your normal workflow.
– Prefer SafeTensors when the model is available in that format. Hugging Face Transformers provides a `use_safetensors` parameter to avoid unsafe formats such as pickle. (Hugging Face Transformers security policy)
– Treat `.pt`, `.pth`, `.bin`, `.ckpt`, `.pkl`, `.pickle`, and `.joblib` as untrusted artifacts until you have verified the loading path. The cited guidance identifies these extensions as pickle-related or high-risk formats. (Yaklang AI/ML security guidance)
– For PyTorch loading guidance covering PyTorch 2.0 and later, use `torch.load(…, weights_only=True)`. This reduces the loading behavior you permit, but it does not turn an unknown file into a trusted dependency. (Yaklang AI/ML security guidance)
– Scan model artifacts before loading them. Static scanners can inspect unsafe serialization, pickle opcodes, and embedded code without executing the file. (AI Pentest Playbook)
– Load unfamiliar artifacts in an isolated environment with no unnecessary credentials or network access. This is an operational recommendation based on the documented ability to open sockets, drop payloads, and exfiltrate credentials during loading. (Byakugan)
The important sequence is inspect, isolate, then load.
Many builders reverse it since downloading and loading are often one convenient code path.
That convenience is exactly what a malicious model file exploits. An AI agent may be the visible product. But the serialized artifact is part of the software supply chain underneath it.
The model is not the trust boundary
Open-source AI agent security is often framed around prompts, tools, and permissions. Those controls matter, but they do not protect a machine from code that runs before the agent begins handling a request.
The stronger boundary is the model-loading step.
If the file can execute code during deserialization, then a prompt policy cannot repair the mistake after the loader has already run.
For a small business, the useful standard is not “download only popular models.” Popularity is not a security control. The standard is “know the format, scan before loading, use safer loading options. And isolate the process that handles anything unfamiliar.”
SafeTensors should be the default when available. And pickle-based artifacts should require a reason, a controlled environment, and an inspection step. That is less convenient than calling `torch.load(…)` on whatever file appeared in a repository.
But convenience is a poor trade for credentials or source code.
The next model your agent uses is not just a set of weights. Before it is inference input, it is an executable artifact handled by a loader. Check that artifact first, then let the agent touch it.
