Skip to main content

How to Log a scikit-learn Model in MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Metrics tell you how good a model was; logging the model itself keeps the model, so you can load it, compare it and deploy it later. mlflow.sklearn.log_model saves a trained scikit-learn model to MLflow, and in MLflow 3 every logged model gets its own ID and its own page. This guide logs one, with real output from MLflow 3.18.0.

Syntax​

Syntax
model_info = mlflow.sklearn.log_model(sk_model, name="...")
PartWhat it means
sk_modelThe trained scikit-learn model or pipeline
nameThe model's name, shown in the MLflow UI. MLflow 3 uses name where MLflow 2 used artifact_path
model_infoWhat log_model returns: among others, model_info.model_id and model_info.model_uri, the address you load the model from later

Called inside a run, as here, the model is linked to that run. Other options, such as a signature, an input example or the file format, have guides of their own, linked below.

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: Train a Model and Log It​

The example fits a linear regression to scikit-learn's diabetes data and logs it as diabetes-model:

The log_model.py script in a macOS-style code window: it sets the tracking server and the experiment log-model-demo, fits a LinearRegression to the diabetes data, logs it with mlflow.sklearn.log_model named diabetes-model inside a run, and prints the model ID and model URI.

log_model.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("log-model-demo")

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")

print("Model ID: ", model_info.model_id)
print("Model URI:", model_info.model_uri)
Terminal
uv run python log_model.py
Output
2026/11/21 14:11:57 INFO mlflow.tracking.fluent: Experiment with name 'log-model-demo' does not exist. Creating a new experiment.
2026/11/21 14:11:57 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:11:58 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:11:58 INFO mlflow.utils.environment: Detected uv project at /Users/you/source/mlflow-demo. Attempting to export requirements via 'uv export'.
2026/11/21 14:11:58 INFO mlflow.utils.uv_utils: Exported 91 dependencies via uv
2026/11/21 14:11:58 INFO mlflow.utils.environment: Successfully exported 91 requirements from uv project. Skipping package capture based inference.
🏃 View run caring-stag-922 at: http://127.0.0.1:8080/#/experiments/4/runs/4a8d6a2a28024d32b3e43d20861c0f97
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/4
Model ID: m-ed4c6720521f4eb495b4dd17577dbcc7
Model URI: models:/m-ed4c6720521f4eb495b4dd17577dbcc7

The output of log_model.py in a terminal: MLflow creates the experiment, detects the uv project and exports 91 requirements, links the run, and the script prints the model ID m-ed4c6720521f4eb495b4dd17577dbcc7 and the model URI models:/ followed by the same ID.

  • Detected uv project … Exported 91 dependencies via uv: MLflow records the packages the model needs, read from the project's uv.lock, so the model can be rebuilt elsewhere.
  • Model ID and Model URI: the model's own identity. models:/<model ID> is how you load it later, from any script that can reach the server.

Step 2: Find the Model in the UI​

Open the experiment, log-model-demo, and its Models tab:

The log-model-demo experiment's Models tab: one model, diabetes-model, with status Ready, logged from log_model.py, from the run caring-stag-922, not registered and without a dataset.

diabetes-model is Ready, with the script that logged it and its source run. Click it, then its Artifacts tab, to see what was saved:

The model's Artifacts tab with MLmodel open: the files MLmodel, conda.yaml, model.skops, python_env.yaml and requirements.txt, and the MLmodel file listing the python_function and sklearn flavors, serialization_format skops, the model ID and the run ID.

Five files:

  • model.skops: the model itself. Outside Databricks, log_model saves scikit-learn models in the skops format unless you choose another; MLflow's reference says "The 'skops' format guarantees safe deserialization."
  • MLmodel: the description MLflow reads to load the model: its two flavors (python_function, for any tool, and sklearn), the file format and the IDs.
  • conda.yaml, python_env.yaml, requirements.txt: the environment the model needs.

Common Mistakes​

MistakeWhat happensDo this instead
Loading the model with mlflow.get_artifact_uri("model")MLflow 3 doesn't keep models with the run's files, so nothing is foundUse model_info.model_uri
Expecting the model on the run's Artifacts tabIt isn't there in MLflow 3Open the experiment's Models tab, or the run's Logged models table

Troubleshooting​

SymptomLikely causeFix
WARNING mlflow.utils.environment: Failed to resolve installed pip version...The uv project has no pipFixed: Failed to resolve installed pip version
MlflowException: The saved sklearn model references untrusted typesThe model holds a type skops doesn't trust by defaultFixed: The saved sklearn model references untrusted types

FAQ​

Do I need a run to log a model?​

Not in MLflow 3: its migration guide says you "can call log_model directly without the mlflow.start_run() context manager". Logging inside a run links the model to the run's parameters and metrics, which is usually what you want.

Where are the files stored?​

On the tracking server, under the experiment's models/<model ID>/artifacts folder, not under the run's folder.

Conclusion​

mlflow.sklearn.log_model(model, name="diabetes-model") saved the linear regression as a logged model with its own ID and models:/ URI, in the skops format, with its environment files and an MLmodel description. The experiment's Models tab lists it, ready to load.

Additional Resources​