8 min read

Never open a model you didn't write. Here's how a small AI device does it anyway

In much of the Python ML world, loading a model file can run code that came with it. How a Raspberry Pi learning agent treats every model like third-party code, with four checks: weights only, a pinned source, a fixed shape, and a fair contest before it takes charge.
Headline "Never open a model you didn't write. Then do it safely." beside the Kestrel mark and four checks in order: format, provenance, shape, merit.

Here's a question worth asking your ML team this week: what happens when we open a model file?

In much of the Python ML ecosystem, the honest answer is "more than you'd think". Many common model formats are built on Python's pickle, a format that saves Python objects and, when it loads them, can run code stored in the file. PyTorch's own documentation warns about this and offers a weights-only mode for loading. Stable-Baselines3, a popular reinforcement-learning library, saves a model as a zip that holds pickled Python objects next to the weights. Opening a model can mean running a program.

That isn't a scandal. It's a well-documented trade-off in convenient tools. But it means a model file deserves the same care as any third-party code, and most teams never ask the question until someone else does.

My side project opens model files it didn't write as a matter of routine. This is how it does it, in four checks.

Kestrel is my nights-and-weekends project, built with Claude as my AI teammate: an open-source Raspberry Pi device for authorized Wi-Fi security research, with a dashboard on your phone and a small learning agent that trains on the device and, optionally, on a desktop. It's unaffiliated with my employer, and the views here are my own.

Where the models come from

Kestrel's optional learning agent decides, every 30 seconds, how the device should listen for the next window: which channels, how long to stay on each, and in what mode. A simple scripted sweep is always there as the fallback and the yardstick. A learned model has to earn its place.

Those models arrive by three routes:

  • The device itself. On a board with enough memory, it fine-tunes a new model from its own logs.
  • A desktop. The optional remote-train plugin runs the training on a more powerful computer over SSH and brings the result back.
  • A memory card. You can copy a model onto the card's boot partition, and the device imports it at the next boot.

Two of those three routes mean a file arriving from another machine. So "what happens when we open it?" was a design question from the start.

Check 1, format: load the numbers, not the program

A Stable-Baselines3 model file is a zip. Inside are the weights, stored as PyTorch tensor files, and a "data" entry with the model's settings, some of them Python objects serialized with pickle. The library's one-line way to open a model restores all of it, pickled objects included.

Kestrel never uses that one-line load. Instead, it does two things:

  • It builds a brand-new, empty model from its own code, for the agent's fixed shape: 395 numbers in, a choice among 32 actions out. The network's layout and every setting come from the device, not from the file.
  • It copies in only the weights. The library call it uses asks for the parameters alone, so the pickled "data" entry is never read, and the tensor files go through PyTorch's loader in weights-only mode, which accepts tensors and plain data, not arbitrary objects.

The device decides what the model is. The file only supplies the numbers. The same weights-only approach is used everywhere a model is opened: to run it, to score it in a contest, and to warm-start a new training run from it.

There's a test for it, too, run wherever the ML libraries are installed: it booby-traps the one-line loader so that any call fails the test, then saves and reopens a model of each of the four algorithms the agent supports and checks that every weight arrived intact.

Two ways to open the same model zip. The one-line load restores everything, including pickled Python objects that can run code. Kestrel's way builds a fresh model from its own code for the fixed shape, then copies in only the tensors, read in weights-only mode; the pickled data entry is never read.
The device decides what the model is. The file only supplies the numbers.

Check 2, provenance: know who you're talking to

For the desktop route, it matters which desktop. Kestrel connects over SSH with the device's own key, and the desktop's identity is confirmed once, by hand, when you run setup. From then on, every connection requires that same recorded identity. An unattended run refuses a host it hasn't seen before rather than trusting it on first sight. The desktop's password is typed once during setup to authorize the key, and stored nowhere.

The desktop also runs the device's own code. Before every training run, the device copies its installed package up to the desktop, and the run uses that copy. Same trainer, same rules, more compute.

And the part of Kestrel that downloads a model never opens it. The plugin runs inside the always-on dashboard service, which deliberately never loads the ML libraries. It pulls the file into a staging folder and leaves it there. Only the agent opens it, and only through check 1.

The memory card route is different by design: copying a model onto the card is the operator's own deliberate choice, much like installing software you picked. Even so, the import reads the model's label and accepts only one of the four known algorithm names, so a file on a card can't choose what code gets imported. The model still passes checks 1 and 3 every time it's loaded.

Check 3, shape: the model must fit the contract

Every model travels with a small label file, a sidecar, recording which algorithm trained it and which spec version it was built for. The spec is the agent's fixed contract: what it sees and what it can choose. It's at version 2 today, and it changes only when that contract does.

A model built for a different spec version doesn't get in:

  • the remote-train plugin doesn't stage it;
  • a waiting candidate with the wrong version is discarded before any contest;
  • a model in the active slot with the wrong version isn't loaded, and the scripted sweep drives instead.

The label isn't the only line of defence. Because the device builds the empty model itself, the weights have to fit it exactly. A file whose networks don't match fails to load, whatever its label says.

Each refusal has a planned outcome. A refused candidate is simply dropped, and the model in charge carries on. If it's the model in charge that can't load, for this or any other reason, the scripted sweep drives and the dashboard's agent card says why in plain words.

Check 4, merit: win a fair contest first

A well-formed model can still be a bad one. So a new model, from the device or the desktop, is a candidate, and it replaces the model in charge only by winning a contest on held-out hours that no model ever trains on. It has to beat the scripted sweep, beat the model in charge on the same windows, by a paired difference that clears a significance bar, and improve on its lift by at least 10%. Losers are deleted, and a desktop model that lost is remembered, so it isn't pulled again.

I wrote about how those contests work, and the bug that made them necessary, in Our model never lost a contest. That was the problem. Here they do a second job: even a perfectly well-formed model can't take charge on its say-so.

Four checks in order. Format: a fresh model from the device's own code, weights only. Provenance: the desktop's recorded identity, and the downloader never opens the file. Shape: the spec version and an exact fit. Merit: a fair contest on held-out hours. A refused candidate is dropped and the model in charge carries on; if the model in charge is refused, the scripted sweep drives.
Four checks, and a safe default when any of them says no.

One small room

Checks are the first layer. Where the model runs is the second.

The agent runs in its own service, as the device's ordinary user, never as an administrator. Its service is set so that neither it nor anything it starts can ever gain more rights than it began with. Memory caps bound the agent and its training runs together, so a runaway training run is stopped before it can take down the rest of the device. The ML libraries live only in this one process; the always-on dashboard never loads them.

The agent's reach is narrow on purpose. It steers the device only by writing a scan plan that the capture service reads. It never edits the device's settings and never starts the capture tool itself. And it does nothing at all unless capture is on, which ships off and needs your explicit confirmation that you're authorized to test: Kestrel is for networks you own or have written permission to test, and nothing else.

The three routes a model takes. On-device training writes a candidate. The desktop, reached only with its recorded identity, returns a model that the dashboard service stages without opening. A memory card import, the operator's own choice, goes into the active slot after its label is checked. Every model is opened only by the agent, through the weights-only loader, inside its own unprivileged, memory-capped service.
Three routes in, one way to open a model, one small room to run it in.

For the engineers in the room

  • Stack: Stable-Baselines3 and sb3-contrib 2.9, PyTorch 2.10 and gymnasium on Python 3.11, arm64. Four algorithms (RecurrentPPO by default, PPO, DQN, QR-DQN), swappable from config because the spaces are a plain Discrete(32) and Box.
  • The loader: policies._load_weights builds the model with make_algorithm against a gymnasium env that only declares the spec's spaces and is never reset or stepped, then calls set_parameters(path, exact_match=True, device="cpu"). That reads the zip with load_data=False, so the cloudpickled "data" entry is skipped, and loads the .pth entries with torch.load(..., weights_only=True).
  • Inference-only builds shrink the training buffers (a default DQN replay buffer holds a million observations) without changing the networks, so the weights still fit exactly.
  • Warm starts use the same set_parameters path, with the same spec check first.
  • Host keys: every unattended ssh and rsync runs with StrictHostKeyChecking=yes; the key is accepted once, interactively, during setup.
  • The unit: kestrel-agent.service runs as pi with NoNewPrivileges=yes, MemoryHigh and MemoryMax, and training runs in a separate, niced child process.

For data and engineering leaders

  • Ask the question. "What happens when we open a model file?" If the answer is a one-line load of a pickle-based format that came from outside, you're running a stranger's script.
  • Separate the architecture from the weights. Build the model from your own code and load only numbers: a weights-only loader, or a format that stores only tensors (safetensors is a widely used one).
  • Pin where models come from. Confirm a training host's identity once, deliberately, and require it after. "Trust on first use" is a habit worth breaking for anything that ships models.
  • Version the contract. A spec version in a label, and an exact fit at load, turn "it loaded but behaves strangely" into "it was refused, and here's why".
  • Make merit a gate. A model that loads isn't a model that's better. Let it take charge only by winning on data it never trained on.
  • Fail safe, in a small room. Decide what runs when a model is refused, and run models with no more rights than they need.

Working with an AI teammate made this practical on a side project. Claude traced every path a model takes from file to decision and drafted the changes and tests. The calls, like refusing the one-line loader everywhere and pinning host identities, stayed mine.

For investors

Every AI company has a model supply chain, whether or not it calls it that: where models come from, how they're opened, who can put one into production, and what happens when one is bad. It's a fair due-diligence question, and a revealing one. Ask a team to walk you through what happens when they open a model file. A crisp answer, with layered checks and a safe default, says a lot about how they treat everything else.

If posts like this are useful to you, subscribe and the next one lands in your inbox.

When did your team last ask what happens when it opens a model file? 🪶

Kestrel is a personal, open-source project built on nights and weekends, unaffiliated with my employer, and for authorized testing only. The user guide and source are public.