Skip to main content

How to Load an MLflow Model with mlflow.pyfunc.load_model

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A logged model is only useful once you load it again. mlflow.pyfunc.load_model loads any MLflow model, from scikit-learn or anything else, as a Python function with one predict method. Your code doesn't need to know which library trained the model. This guide loads one by its models:/ URI, with real output from MLflow 3.18.0.

Syntax​

Syntax
model = mlflow.pyfunc.load_model(model_uri)
predictions = model.predict(data)
PartWhat it means
model_uriWhere the model is: models:/<model ID> for a logged model, the model_uri that log_model returned
modelA PyFuncModel: the model behind a common interface
dataThe input, usually a pandas DataFrame with the model's columns

Prerequisites​

  • An MLflow tracking server at http://127.0.0.1:8080. MLflow's tracking server guide shows how to start one.
  • mlflow and scikit-learn in your project, for example with uv add mlflow scikit-learn pandas.

Step 1: Log a Model to Load​

This script logs a linear regression on scikit-learn's diabetes data into the experiment named on the command line, and prints its URI:

log_diabetes_model.py
import sys

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(sys.argv[1])

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

with mlflow.start_run():
model_info = mlflow.sklearn.log_model(model, name="diabetes-model", input_example=X.head(3))

print("Model URI:", model_info.model_uri)
Terminal
uv run python log_diabetes_model.py pyfunc-load-demo
Output (last lines)
🏃 View run invincible-fish-273 at: http://127.0.0.1:8080/#/experiments/12/runs/556c8a67776e4e1c9bc2af32ed5b636a
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/12
Model URI: models:/m-177f678f96414766b76dece8d770307e

Step 2: Load It and Predict​

load_pyfunc.py takes the URI on the command line, loads the model and predicts the first three rows:

The load_pyfunc.py script in a macOS-style code window: it loads the model from the URI given on the command line with mlflow.pyfunc.load_model, loads the diabetes features, and prints the model's class name and its predictions for three rows.

load_pyfunc.py
import sys

import mlflow
from sklearn.datasets import load_diabetes

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

model = mlflow.pyfunc.load_model(sys.argv[1])

X, _ = load_diabetes(return_X_y=True, as_frame=True)
print(type(model).__name__)
print(model.predict(X.head(3)).round(1))
Terminal
uv run python load_pyfunc.py models:/m-177f678f96414766b76dece8d770307e
Output
PyFuncModel
[206.1 68.1 176.9]

The output of load_pyfunc.py in a terminal: PyFuncModel, then the predictions 206.1, 68.1 and 176.9.

The loaded object is a PyFuncModel, not the scikit-learn model, and its predict gave three disease progression values. Before predicting, it checked the three rows against the model's signature: How to Check Data Against an MLflow Model's Signature shows what that check refuses.

Common Mistakes​

MistakeWhat happensDo this instead
Reaching for scikit-learn attributes such as coef_ on the loaded modelAttributeError: 'PyFuncModel' object has no attribute 'coef_'Load it with mlflow.sklearn.load_model
Building the URI from the run's artifact pathMLflow 3 keeps models outside the run's filesUse models:/<model ID>

Troubleshooting​

SymptomLikely causeFix
MlflowException: Failed to enforce schema of data ...The input doesn't match the signatureHow to Check Data Against an MLflow Model's Signature
Loading PyFunc models is disallowed because a PyFunc model names the Python module that loads it...MLFLOW_ALLOW_MODEL_CODE_EXECUTION is falseHow to Refuse to Load Untrusted Models in MLflow

FAQ​

Does load_model download the model?​

Yes: it fetches the model's files from the tracking server before loading them.

Which predict method does it call?​

The one MLmodel names under predict_fn: predict unless the model was logged with another, as in How to Make an MLflow Model Return Probabilities.

Conclusion​

mlflow.pyfunc.load_model("models:/m-…") brought the logged linear regression back as a PyFuncModel, and its predict returned the same values the model gives in scikit-learn, after checking the input against the signature.

Additional Resources​