Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 41 ยท Model Packaging and Registry

Saving and Loading Models

Learn how to serialize trained models with pickle and joblib, why version mismatches and untrusted pickles are dangerous, and when portable formats such as ONNX are the better choice.

Training produces a model in memory. To use it anywhere else, such as a prediction service or a nightly job, you must serialize it: turn it into bytes on disk, and later deserialize it back. The choice of format affects security, portability and how long the model keeps working after a library upgrade.

pickle and joblib

Python's pickle can store almost any object. joblib is a pickle-based tool that stores large NumPy arrays efficiently and is the common choice for scikit-learn models.

Python Round trip a pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Save the whole pipeline (scaler plus model), not just the final estimator. Otherwise the serving code has to repeat preprocessing by hand, which invites skew. See scikit-learn pipelines.

With joblib the calls are joblib.dump(model, "model.joblib") and joblib.load("model.joblib").

The two big dangers of pickle

Pickle risks

Code execution Loading a pickle can run any code.
Version coupling Needs the same library versions.
Rule Only load files you created.

A pickle is a program, not just data.

1 / 2

Security. Unpickling runs code chosen by whoever made the file. The next example is harmless, but it shows how it works.

Python Why unpickling untrusted data is unsafe

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Replace print with a function that deletes files or opens a network connection and you have an attack. Never load a model file from an untrusted source.

Version coupling. A pickle stores the names of classes, not their code. If scikit-learn changes an internal attribute between versions, loading can fail, or worse, load and silently misbehave. Always save the library versions next to the model and load it in the same environment, as covered in packaging and environments.

Python Store a manifest next to the model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Portable and safer formats

Format Good for Notes
pickle / joblib scikit-learn inside Python Fast, easy, unsafe from strangers, version-bound.
ONNX Cross-language and runtime serving Framework-neutral graph; run with ONNX Runtime from many languages.
safetensors Neural-network weights Stores only tensors, so loading cannot run code.
Native formats torch.save, SavedModel, XGBoost JSON Each framework's own supported way; prefer ones that avoid pickle.
PMML / skops Specific interchange cases skops adds trust checks for scikit-learn files.

Choose the least powerful format that meets your need: if the serving environment is Python with the same pinned libraries, a checksummed joblib file is fine; if the service is written in Java or C++, export to ONNX.

Never put model files in a public place and then load them elsewhere without a checksum. Treat model files like executables.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why save the whole pipeline instead of only the final estimator?

    Bundling preprocessing with the model keeps training and serving transformations identical.
  2. 2. What is the main security risk of pickle?

    Unpickling can call any function the file's author chose, so only load pickles you created or fully trust.
  3. 3. A model pickled with one scikit-learn version fails to load in another. What is this called?

    Pickle stores class names, not code, so the loading environment must match the training environment.
  4. 4. When is ONNX a good choice?

    ONNX is a framework-neutral format, useful for serving from other languages and runtimes.

Summary

  • Serialize the full pipeline, not just the last step.
  • pickle and joblib are convenient but run code on load and depend on library versions.
  • Store a manifest with a checksum and library versions, and verify it before loading.
  • Use portable or tensor-only formats (ONNX, safetensors) when crossing languages or trust boundaries.

Next: Model Metadata and Model Cards