Skip to main content

How to Refuse to Load Untrusted Models in MLflow

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Loading a machine learning model can run code. A pickle file runs whatever it holds, and even the safer skops format trusts the types its own file lists. MLflow has one switch that refuses all such models: the environment variable MLFLOW_ALLOW_MODEL_CODE_EXECUTION. This guide turns it off and on, with real output from MLflow 3.18.0.

What the Setting Covers​

MLflow's reference describes MLFLOW_ALLOW_MODEL_CODE_EXECUTION as whether "to allow loading models that run code supplied by the model": "pickle or cloudpickle artifacts, code directories logged with code_paths, models from code, models on the Python path, Transformers custom code, LlamaIndex settings that name classes to construct, and all PyFunc models, because a PyFunc model names the Python module that loads it."

  • Default: true. MLflow loads these models.
  • false. MLflow refuses them. The reference: "Set it to false to refuse such models. Only set it to true for models you trust."

An older variable, MLFLOW_ALLOW_PICKLE_DESERIALIZATION, is deprecated in its favor; it refused only pickle and cloudpickle models.

Prerequisites​

Step 1: Load a Model with the Setting Off​

The example model is a linear regression trained on scikit-learn's diabetes data and logged in the skops format, models:/m-563cd9af3b2a46dd8f9e3ed8f4c583c5. load_model.py loads it as a scikit-learn model:

load_model.py
import sys

import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")

model = mlflow.sklearn.load_model(sys.argv[1])
print("Loaded", type(model).__name__)

Run it with the variable set to false:

Terminal
export MLFLOW_ALLOW_MODEL_CODE_EXECUTION=false
uv run python load_model.py models:/m-563cd9af3b2a46dd8f9e3ed8f4c583c5
Output (last line)
mlflow.exceptions.MlflowException: Deserializing this model is disallowed. Loading a model can execute arbitrary code from the model artifact, including models saved in the 'skops' format, whose trusted-types allow-list is read from the model itself. Only load models from trusted sources; set 'MLFLOW_ALLOW_MODEL_CODE_EXECUTION' to 'true' to allow loading.

The end of the output in a terminal: MlflowException: Deserializing this model is disallowed, explaining that loading can execute arbitrary code from the model artifact, including skops models whose trusted-types list is read from the model itself.

Refused, even though the model is in the skops format. The message gives the reason: skops checks types against a trusted list, but that list comes from the model's own MLmodel file, written by whoever logged it.

Step 2: Load It with the Setting On​

Turn the variable back to its default and run the same command:

Terminal
unset MLFLOW_ALLOW_MODEL_CODE_EXECUTION
uv run python load_model.py models:/m-563cd9af3b2a46dd8f9e3ed8f4c583c5
Output
Loaded LinearRegression

The output in a terminal: Loaded LinearRegression.

When to Turn It Off​

Turn it off where a process must never load a model from someone it doesn't trust, for example a service that accepts model URIs from users. Where you load only models you or your team logged, the default is what lets you work. Either way, the safest habit is the one MLflow's message ends with: "Only load models from trusted sources".

Common Mistakes​

MistakeWhat happensDo this instead
Expecting skops models to pass with the setting offThey're refused too: their trusted list comes from the modelLoad them only from sources you trust
Using MLFLOW_ALLOW_PICKLE_DESERIALIZATIONDeprecated, and it refused only pickle and cloudpickleMLFLOW_ALLOW_MODEL_CODE_EXECUTION

Troubleshooting​

SymptomLikely causeFix
Deserializing this model is disallowed...MLFLOW_ALLOW_MODEL_CODE_EXECUTION is false in this environmentLoad models you trust only where it's true
Loading PyFunc models is disallowed because a PyFunc model names the Python module that loads it...The same setting, with mlflow.pyfunc.load_modelThe same
WARNING mlflow.models.model: Failed to validate serving input example when loggingThe setting is false while logging a model with an input exampleLog models where the setting is true

FAQ​

Does it affect logging models?​

The model is still logged, files and all, but MLflow can't check its input example, which loads the model. With the setting off, logging the same model printed WARNING mlflow.models.model: Failed to validate serving input example and Got error: Loading PyFunc models is disallowed because a PyFunc model names the Python module that loads it....

Does it change the default for everyone?​

No: it's an environment variable, so it applies where you set it, for example in one terminal or one service.

Conclusion​

With MLFLOW_ALLOW_MODEL_CODE_EXECUTION=false, MLflow refused to load even a skops model, because loading any model can run code its file chooses; with the default, true, it loaded. Set it to false wherever models from unknown sources could arrive.

Additional Resources​