A saved machine-learning model depends on the software that created it. When loading a scikit-learn model raises InconsistentVersionWarning, restoring the training environment resolves the version mismatch without changing the original model.
The warning identifies different scikit-learn versions at training and loading time. Matching that version alone is insufficient when Python, NumPy, SciPy, serialization packages, or custom transformers have also changed; the training run's dependency record and source code are needed to recreate its environment.
Loading a model across scikit-learn versions is unsupported, even if it returns predictions. If the application must keep newer dependencies, retraining from the original data and code produces a replacement model whose quality must be checked before use.
Steps to fix InconsistentVersionWarning when loading a scikit-learn model:
- Create diagnose_model.py in the project directory using your text editor with the version-warning policy below.
Loading a pickle or joblib file can execute arbitrary code. Only load a model whose source and integrity you trust, including during diagnosis; a warning filter does not make an untrusted file safe.
- diagnose_model.py
import warnings import joblib from sklearn.exceptions import InconsistentVersionWarning warnings.simplefilter("error", InconsistentVersionWarning)
The filter turns InconsistentVersionWarning into an exception before any model is loaded, so a mismatched estimator cannot silently continue through diagnosis.
- Append the model-loading diagnostic at the end of diagnose_model.py, replacing model.joblib with your model's path.
try: joblib.load("model.joblib") except InconsistentVersionWarning as warning: print(f"Saved with: {warning.original_sklearn_version}") print(f"Loading with: {warning.current_sklearn_version}") raise SystemExit(1) print("Loaded without InconsistentVersionWarning")
The same joblib.load call reads a model saved with pickle.dump, such as model.pkl. The exception exposes the original and current scikit-learn versions; exit status 1 stops the mismatched load. The final message appears only when loading raises no version warning.
- Save diagnose_model.py.
- Run the diagnostic with the Python interpreter that reports the warning.
$ python diagnose_model.py Saved with: 1.8.0 Loading with: 1.9.1
The two version values depend on the saved model and the active environment. A mismatch exits with status 1; a model that loads without the warning prints that result instead. Other exceptions, such as a missing custom module, remain visible and need their own repair.
With a mismatch confirmed, restore the training environment when the original model must keep running and its Python version and package list survive, or retrain when the application must keep newer dependencies or those records are gone.
Restore the training environment
- Locate the Python version and pinned dependency file recorded with the training run.
A training record might specify Python 3.12 and a file named requirements-training.txt containing the exact package versions. A freeze from the environment that is currently failing cannot reconstruct the old environment. If the original environment still exists, its own freeze file and the report from
$ python -c "import sklearn; sklearn.show_versions()"
are the authoritative starting point. With only the saved version from the diagnostic, a file holding scikit-learn==1.8.0 is a best-effort start that leaves Python, NumPy and SciPy unmatched and carries no compatibility guarantee.
Related: How to create a requirements freeze file in Python
Related: How to check the installed scikit-learn version - Locate the reference inputs and predictions saved with the training run.
verify_model.py reads them from reference.joblib, a pair holding feature rows in the form the model was fitted on and their expected predictions. If the training environment still works, save that file there with your own model path and a few of your rows as X_check.
import joblib model = joblib.load("model.joblib") expected = model.predict(X_check) joblib.dump((X_check, expected), "reference.joblib")
Rows and predictions the application logged before the upgrade can fill the same pair, and the trust warning above applies to this file too. Predictions from the failing environment cannot, because they are not independent reference data. Without a reference, run diagnose_model.py with .venv-model/bin/python at the final check instead; a warning-free load shows that the versions match, not that predictions do, so retraining remains the alternative.
- Create a separate virtual environment with your recorded Python version, replacing 3.12 in the command.
$ python3.12 -m venv .venv-model
The separate environment leaves the application's upgraded dependencies and original model intact. The shell paths shown apply to Linux and macOS. If python3.12 is not found, as on Ubuntu 26.04 where the package archive offers only Python 3.14, create the same directory with conda so the later commands stay unchanged.
$ conda create --prefix .venv-model --channel conda-forge \ --override-channels python=3.12 --yes
Related: How to create a virtual environment in Python
Related: How to install Miniconda on Linux
Related: How to create an Anaconda environment with a specific Python version - Install the training dependencies into the new environment, replacing requirements-training.txt with your file.
$ .venv-model/bin/python -m pip install \ -r requirements-training.txt
The recorded file must include the original scikit-learn version, its dependencies, and any packages providing custom model components. A model built on custom transformers also needs their original code on the import path. Do not edit serialized version metadata or suppress the warning to bypass a mismatch.
Related: How to install Python packages from a requirements file
Related: How to create a custom transformer in scikit-learn - Create verify_model.py in the project directory using your text editor, replacing model.joblib and reference.joblib with your paths.
- verify_model.py
import warnings import joblib import numpy as np import sklearn from sklearn.exceptions import InconsistentVersionWarning warnings.simplefilter("error", InconsistentVersionWarning) model = joblib.load("model.joblib") X_check, expected = joblib.load("reference.joblib") predictions = model.predict(X_check) np.testing.assert_array_equal(predictions, expected) print(f"scikit-learn: {sklearn.__version__}") print(f"Predictions: {predictions.tolist()}") print("Predictions match")
The equality check suits class labels. For a regressor, replace the assert_array_equal line with the following line and set the tolerance your application requires.
np.testing.assert_allclose(predictions, expected, atol=1e-9)
- Save verify_model.py.
- Verify that the original model loads and reproduces its reference predictions in the restored environment.
$ .venv-model/bin/python verify_model.py scikit-learn: 1.8.0 Predictions: [0, 1, 2] Predictions match
The displayed predictions belong to a three-row classification check; other models return their own values. A version mismatch stops loading, and different predictions raise an assertion error. The application must use this restored environment to retain the compatible loader.
Retrain in the current environment
Retraining needs the original training recipe, preprocessing, and training-data snapshot. A saved estimator alone is not enough to reproduce the training run.
- Open the original training script in your text editor.
The script is named train.py here and must train the complete estimator or preprocessing pipeline from the original training data, without loading the incompatible model.
- Replace the script's fit call with a split that holds out rows, using your own feature, label and model names.
from sklearn.model_selection import train_test_split X_train, X_heldout, y_train, y_heldout = train_test_split( X, y, test_size=0.2, random_state=0 ) model.fit(X_train, y_train)
The fit never sees the held-out fifth of the rows, so the later score comes from unseen data. A script that already holds out rows keeps its own split and supplies those rows to the next replacement.
- Replace the script's joblib.dump call or pickle.dump block with the following lines, using your own model and held-out names.
import joblib joblib.dump(model, "model-retrained.joblib") joblib.dump((X_heldout, y_heldout), "heldout.joblib")
The new filename preserves the original artifact for recovery. Re-saving the old estimator after a cross-version load does not establish compatibility.
- Save the training script.
- Retrain the model with the target environment's Python interpreter.
$ python train.py
The target environment needs the application's full dependency set and custom code, including any changes required by newer APIs. Retraining can change predictions and quality; the old environment remains available until the replacement passes the application's evaluation criteria.
- Create check_retrained.py in your text editor to test the newly saved model on held-out data, replacing 0.95 with your required score.
- check_retrained.py
import warnings import joblib from sklearn.exceptions import InconsistentVersionWarning warnings.simplefilter("error", InconsistentVersionWarning) model = joblib.load("model-retrained.joblib") X_heldout, y_heldout = joblib.load("heldout.joblib") score = model.score(X_heldout, y_heldout) print(f"Held-out score: {score:.3f}") assert score >= 0.95, "Score is below the requirement"
The score method returns accuracy for a classifier and R² for a regressor. An application with its own metric uses that metric and threshold from its earlier evaluation.
- Save check_retrained.py.
- Confirm that the retrained model reloads and meets the held-out score in the target environment.
$ python check_retrained.py Held-out score: 1.000
A version warning aborts loading; a score below the threshold raises an assertion error. A passing check establishes the new artifact's load and evaluation result, while broader production validation still depends on the application's requirements.
Related: scikit-learn model compatibility and persistence