Skip to main content

How to Link a Metric to a Logged Model in MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

In MLflow 3, a logged model has its own page, and that page can show the model's scores: test accuracy, error, anything you measured. A metric gets there when it's linked to the model, and mlflow.log_metric(..., model_id=...) links it explicitly. This guide links a test score to a model and shows which metrics MLflow links by itself, with real output from MLflow 3.18.0.

Syntax​

Syntax
mlflow.log_metric(key, value, model_id=model_info.model_id)
PartWhat it means
key, valueThe metric's name and value, as with any metric
model_idThe logged model the metric belongs to. model_info is what log_model returned

The metric is still logged to the run as well. MLflow's reference on leaving model_id out: it uses "the current active model ID", and "If no active model exists, the models IDs associated with the specified or active run will be used."

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 and Its Test Score​

The example trains a linear regression on part of scikit-learn's diabetes data, logs it, scores it on the rows it didn't see, and links that score, test_r2, to the model:

The linked_metric.py script in a macOS-style code window: it splits the diabetes data, fits a LinearRegression, logs it named diabetes-model, logs test_r2 computed with r2_score with model_id set to model_info.model_id, and prints the logged model's metrics.

linked_metric.py
import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("linked-metric-demo")

X, y = load_diabetes(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
model = LinearRegression().fit(X_train, y_train)

with mlflow.start_run():
model_info = mlflow.sklearn.log_model(model, name="diabetes-model", input_example=X_train.head(3))
mlflow.log_metric("test_r2", r2_score(y_test, model.predict(X_test)), model_id=model_info.model_id)

print(mlflow.get_logged_model(model_info.model_id).metrics)
Terminal
uv run python linked_metric.py
Output (last lines)
🏃 View run delicate-flea-871 at: http://127.0.0.1:8080/#/experiments/9/runs/082b57983bed442d9eb11e65af25343a
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/9
[<Metric: dataset_digest=None, dataset_name=None, key='test_r2', model_id='m-91d9054ebc5a436f90fbf8e009f74c7c', run_id='082b57983bed442d9eb11e65af25343a', step=0, timestamp=1795250544451, value=0.33223321731061817>]

The end of the output of linked_metric.py in a terminal: the run's link, then the logged model's metrics, a single Metric test_r2 with the model's ID, the run's ID and the value 0.33223321731061817.

mlflow.get_logged_model(...).metrics lists the metric with both IDs: the model's and the run's.

Step 2: See the Score in the UI​

Open the experiment's Models tab and click the model. Its page lists the metric, with the run it came from:

The model's page: Metrics (1) lists test_r2 from the run delicate-flea-871 with the value 0.3322, and the About this logged model panel shows its status Ready, model ID and source run.

The Models tab can show it as a column too, which lets you compare models at a glance. I hid the empty Registered models and Dataset columns with the Columns button to make room:

The linked-metric-demo Models tab: diabetes-model, Ready, logged from linked_metric.py by the run delicate-flea-871, with a test_r2 column showing 0.33223321731061817.

With one model in a run, the run's metrics reach the model even without model_id. implicit_link.py logs one metric before the model and one after, neither with model_id:

implicit_link.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("linked-metric-demo")

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

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

print([metric.key for metric in mlflow.get_logged_model(model_info.model_id).metrics])
Terminal
uv run python implicit_link.py
Output (last line)
['before_model', 'after_model']

The new model's Metrics (2) section: before_model with the value 1 and after_model with the value 2, both from the run rare-deer-310.

Both are on the model:

  • after_model: no model_id, so MLflow used the run's model, as its reference says.
  • before_model: logged before the model existed. MLflow's log_model code copies the run's metrics that have no model yet onto the model it's logging.

So model_id matters most when a run holds more than one model: Fixed: An MLflow Logged Model Shows Another Model's Metric shows what goes wrong there and the order that fixes it.

Common Mistakes​

MistakeWhat happensDo this instead
Logging several models in one run, each followed by its scoreLater models also get earlier models' scoresLog every model first, then each score with its model_id
Looking for the score only on the runIt's there, but comparing models is easier on the Models tabAdd the metric's column on the Models tab

Troubleshooting​

SymptomLikely causeFix
A model shows a metric from another modelBoth were logged in one runFixed: An MLflow Logged Model Shows Another Model's Metric
The metric's column is off-screen on the Models tabOther columns take the spaceHide columns with the Columns button

FAQ​

Is the metric still on the run?​

Yes. The output above shows the run's ID on the metric, and the run's page lists it too.

Yes: model_id takes the ID of any logged model, and log_metric logs it under the active run.

Conclusion​

mlflow.log_metric("test_r2", ..., model_id=model_info.model_id) put the model's test score on its page and in a Models tab column. In a run with one model, MLflow links the run's other metrics as well; with several models in one run, log them all first and give each score its model_id.

Additional Resources​