Sub topic 9
Recording versions next to the model
This chapter assumes you can save and load a fitted estimator with joblib, and that you know a saved file is a snapshot of Python objects tied to the libraries that created it. Here you will store the library versions beside the model file and check them before you load it.
Why a model file is not enough
A .joblib file holds a pickled object graph. It does not say which scikit-learn, NumPy, or Python produced it. Months later, someone loads the file in a different environment. The load may fail with an error, or it may succeed and then behave differently. Scikit-learn warns when it detects a version mismatch, but it does not guarantee the file will work.
A small metadata file stored next to the model answers one question: which environment can load this file?
What to record
Record at least these items:
- The scikit-learn version. This is the most important one, because the estimator classes and their internal attributes change between releases.
- The joblib version, because it writes the file container.
- The Python version, because pickle protocols and some object layouts depend on it.
You can also record NumPy and SciPy versions, the training date, and the feature names the model expects. Keep the file small and use plain JSON, so any tool can read it without loading the model.
Writing the metadata
The following code writes the three versions to a JSON file that shares the model's base name.
import json
import sys
import joblib
import sklearn
meta = {
"scikit_learn": sklearn.__version__,
"joblib": joblib.__version__,
"python": sys.version.split()[0],
}
with open("iris_model.meta.json", "w") as f:
json.dump(meta, f, indent=2)The result looks like this, with your own version numbers:
{
"scikit_learn": "1.5.2",
"joblib": "1.4.2",
"python": "3.12.4"
}Run this in the same process or environment that trained and saved the model. If you run it in a different environment, you record the wrong versions.
Checking the metadata before loading
Writing the file only helps if something reads it. A short check before loading turns a silent mismatch into a clear message. This version assumes iris_model.joblib and iris_model.meta.json already exist.
import json
import sklearn
from joblib import load
with open("iris_model.meta.json") as f:
meta = json.load(f)
if meta["scikit_learn"] != sklearn.__version__:
raise RuntimeError(
f"Model was saved with scikit-learn {meta['scikit_learn']}, "
f"but this environment has {sklearn.__version__}."
)
model = load("iris_model.joblib")This check is strict. It rejects any difference, including patch releases that are probably harmless. You can relax it to compare only the major and minor numbers if your team agrees that patch releases are safe. Choose the policy deliberately, and write it down.
How the pieces fit
The metadata is read first. Only a match allows the model file to load.
Recreating the environment
When the check fails, the metadata tells you what to install. In a fresh virtual environment you can pin the recorded version, for example pip install scikit-learn==1.5.2. Then load the model there. Many teams go one step further and save a requirements.txt from pip freeze next to the model, which captures every dependency and not just the three above.
Common mistakes
- Recording versions after the fact. If you write the metadata from a different environment than the one that trained the model, the file is wrong and gives false confidence.
- Storing metadata only in a notebook or wiki. It gets separated from the model file. Keep both files in the same folder and move them together.
- Treating metadata as a safety guarantee. A matching version string does not prove the model file is safe to load. Pickle-based files can still run arbitrary code, so you also need to trust where the file came from.
- Ignoring warnings. Scikit-learn raises
InconsistentVersionWarningwhen you load a model saved under a different version. Do not filter it out. In tests, you can turn it into an error withwarnings.simplefilter("error").
Exercises
Exercise
Why is it a mistake to run the metadata-writing script on a different machine from the one that trained the model?
Show answer
The script records the versions installed where it runs, not where the model was trained. If the environments differ, the metadata describes the wrong versions, so the later check passes or fails for the wrong reasons.
Exercise
Your load check compares the full scikit-learn version string, and it rejects a file saved with 1.5.1 when you have 1.5.2. Name one way to relax the check, and one risk of doing so.
Show answer
Compare only the major and minor numbers (here, 1.5) and ignore the patch number. The risk is that a patch release might change behavior or internals in a way that breaks or alters the loaded model, and the relaxed check would not catch it.