Skip to main content

Fixed: "Saving scikit-learn models in the pickle or cloudpickle format requires exercising caution" in MLflow

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Turning on MLflow's scikit-learn autologging printed a warning with every saved model: WARNING mlflow.sklearn: Saving scikit-learn models in the pickle or cloudpickle format requires exercising caution. Autologging saves models with cloudpickle unless you say otherwise, and a pickled model can run code when it's loaded. Passing serialization_format="skops" to mlflow.sklearn.autolog() saves the model in the skops format, which the warning itself recommends, and the warning is gone.

What I Was Doing​

I was turning on autologging for a scikit-learn model for the first time, on MLflow 3.18.0, scikit-learn 1.9.1 and Python 3.15.2: one line, mlflow.sklearn.autolog(), then fitting a linear regression to scikit-learn's diabetes data:

autolog_basic.py
import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("autolog-demo")
mlflow.sklearn.autolog()

X, y = load_diabetes(return_X_y=True, as_frame=True)
model = LinearRegression().fit(X, y)
Terminal
uv run python autolog_basic.py

What I Expected​

A run with the model's parameters, metrics and the model itself, and no warnings.

What I Got​

The run was created and the model saved, with this warning in the middle:

Output (excerpt)
2026/11/21 11:01:03 INFO mlflow.utils.autologging_utils: Created MLflow autologging run with ID '754a480ac2d641cca0054cb76c83a2d9', which will track hyperparameters, performance metrics, model artifacts, and lineage information for the current sklearn workflow
2026/11/21 11:01:03 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 11:01:03 WARNING mlflow.sklearn: Saving scikit-learn models in the pickle or cloudpickle format requires exercising caution because these formats rely on Python's object serialization mechanism, which can execute arbitrary code during deserialization. The recommended safe alternative is the 'skops' format. For more information, see: https://scikit-learn.org/stable/model_persistence.html

The output of autolog_basic.py in a terminal: the autologging run is created, the uv project is detected, and a WARNING from mlflow.sklearn says saving scikit-learn models in the pickle or cloudpickle format requires exercising caution and recommends the skops format.

The saved model's MLmodel file, on the model's Artifacts tab in the MLflow UI, confirms the format: model.pkl, with serialization_format: cloudpickle:

The model's Artifacts tab in the MLflow UI with MLmodel open: the files MLmodel, conda.yaml, model.pkl, python_env.yaml and requirements.txt, and in MLmodel the sklearn flavor with pickled_model model.pkl and serialization_format cloudpickle.

What I Checked​

The page the warning links to. scikit-learn's model persistence guide lists pickle and cloudpickle with "Loading can execute arbitrary code", and skops as "More secure than pickle based formats": with skops, "loading the persisted model doesn't automatically allow arbitrary code execution".

MLflow's reference. mlflow.sklearn.autolog takes serialization_format, "one of the following: 'pickle', 'cloudpickle' or 'skops'", with 'cloudpickle' as its default. mlflow.sklearn.log_model, by contrast, uses skops by default outside Databricks, and its reference says "The 'skops' format guarantees safe deserialization."

What it needs. skops is installed with MLflow 3.18, so the project needed no new package.

Why It Happens​

Autologging saves every trained model, and its default format is cloudpickle. Loading a pickled file can run whatever code the file contains, so a model from someone you don't trust is a risk. MLflow warns about it each time it saves a model in that format.

The Fix​

Choose the skops format in the autolog() call:

The fixed autolog_skops.py in a macOS-style code window: the same script as before, with mlflow.sklearn.autolog called with serialization_format set to skops.

autolog_skops.py
import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("autolog-demo")
mlflow.sklearn.autolog(serialization_format="skops")

X, y = load_diabetes(return_X_y=True, as_frame=True)
model = LinearRegression().fit(X, y)

After the Fix​

Terminal
uv run python autolog_skops.py
Output
2026/11/21 11:04:16 INFO mlflow.utils.autologging_utils: Created MLflow autologging run with ID 'e2920bc77c064181860f5393e2819eb2', which will track hyperparameters, performance metrics, model artifacts, and lineage information for the current sklearn workflow
2026/11/21 11:04:17 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 11:04:17 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 11:04:17 INFO mlflow.utils.environment: Detected uv project at /Users/you/source/mlflow-demo. Attempting to export requirements via 'uv export'.
2026/11/21 11:04:17 INFO mlflow.utils.uv_utils: Exported 91 dependencies via uv
2026/11/21 11:04:17 INFO mlflow.utils.environment: Successfully exported 91 requirements from uv project. Skipping package capture based inference.
2026/11/21 11:04:17 WARNING mlflow.utils.environment: Failed to resolve installed pip version. ``pip`` will be added to conda.yaml environment spec without a version specifier.
🏃 View run abundant-stork-167 at: http://127.0.0.1:8080/#/experiments/3/runs/e2920bc77c064181860f5393e2819eb2
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/3

The output of autolog_skops.py in a terminal: the run is created and the uv requirements exported, with no pickle or cloudpickle caution; only the separate pip version warning remains.

No caution this time. The new model's MLmodel shows the change: model.skops, with serialization_format: skops:

The new model's Artifacts tab with MLmodel open: the files now include model.skops instead of model.pkl, and the sklearn flavor shows pickled_model model.skops and serialization_format skops.

The warning that's left, Failed to resolve installed pip version, is a separate one about the model's environment files: Fixed: "Failed to resolve installed pip version".

Common Mistakes​

MistakeWhat happensDo this instead
Silencing the warning instead of changing the formatThe models are still picklesChoose serialization_format="skops"
Loading a pickled model from someone you don't trustLoading can run any code in the fileLoad only models you or your team saved

Troubleshooting​

SymptomLikely causeFix
WARNING mlflow.utils.autologging_utils: Encountered unexpected error during sklearn autologging: The saved sklearn model references untrusted types...The model contains parts skops doesn't trust by default, such as HistGradientBoostingClassifier's TreePredictorFixed: Encountered unexpected error during sklearn autologging
The caution still appearsAnother log_model or autolog() call without the settingPass serialization_format="skops" there too

FAQ​

Does skops work for every model?​

Not through autologging. For a linear regression it saved without a word, but a HistGradientBoostingClassifier uses a part, TreePredictor, that skops won't load without being told it's trusted. Autologging has no setting for that list, so it printed Encountered unexpected error during sklearn autologging: The saved sklearn model references untrusted types, the model was marked FAILED with no files, and the run got no metrics either. For such a model, keep the default format for autologging, or turn model saving off with log_models=False and save the model yourself with mlflow.sklearn.log_model, which takes a skops_trusted_types list: Fixed: "Encountered unexpected error during sklearn autologging: The saved sklearn model references untrusted types".

Does the format change the model?​

No. Only the file it's stored in changes; the model makes the same predictions.

Conclusion​

The caution came from autologging's default model format, cloudpickle. mlflow.sklearn.autolog(serialization_format="skops") saved the model as model.skops instead, and the warning disappeared. For models with parts skops doesn't trust by default, save the model yourself with log_model and its trusted types.

Additional Resources​