Skip to main content

Fixed: An MLflow Logged Model Shows Another Model's Metric

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

I logged two models in one MLflow run and gave each its score with log_metric(..., model_id=...). The second model came out with two test_r2 values: its own, and the first model's. When log_model runs inside a run, MLflow copies the run's metrics that don't belong to a model yet onto the new model, and the first model's score looked unclaimed. Logging every model before logging any score gave each model only its own.

What I Was Doing​

I found this while writing How to Link a Metric to a Logged Model in MLflow for the MLflow series. On MLflow 3.18.0, I compared two regression models on scikit-learn's diabetes data in one run: a plain linear regression and a ridge regression. For each, the script trains it, logs it, scores it on the test set and links the score to it with model_id, then lists every model's metrics:

two_models.py
import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression, Ridge
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("two-models-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)

with mlflow.start_run():
for name, model in [("linear", LinearRegression()), ("ridge", Ridge(alpha=1.0))]:
model.fit(X_train, y_train)
model_info = mlflow.sklearn.log_model(model, name=name, input_example=X_train.head(3))
score = r2_score(y_test, model.predict(X_test))
mlflow.log_metric("test_r2", score, model_id=model_info.model_id)
print(name, model_info.model_id, round(score, 4))

for model in mlflow.search_logged_models(experiment_ids=[mlflow.get_experiment_by_name("two-models-demo").experiment_id], output_format="list"):
print(model.name, [(metric.key, round(metric.value, 4)) for metric in model.metrics])
Terminal
uv run python two_models.py

What I Expected​

One score per model: linear with 0.3322 and ridge with 0.341.

What I Got​

Output
2026/11/21 14:45:47 INFO mlflow.tracking.fluent: Experiment with name 'two-models-demo' does not exist. Creating a new experiment.
2026/11/21 14:45:47 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:45:48 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:45:48 INFO mlflow.utils.environment: Detected uv project at /Users/you/source/mlflow-demo. Attempting to export requirements via 'uv export'.
2026/11/21 14:45:48 INFO mlflow.utils.uv_utils: Exported 91 dependencies via uv
2026/11/21 14:45:48 INFO mlflow.utils.environment: Successfully exported 91 requirements from uv project. Skipping package capture based inference.
linear m-47335b0691df464db3faad42abd7e1ba 0.3322
2026/11/21 14:45:48 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:45:48 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:45:48 INFO mlflow.utils.environment: Detected uv project at /Users/you/source/mlflow-demo. Attempting to export requirements via 'uv export'.
2026/11/21 14:45:49 INFO mlflow.utils.uv_utils: Exported 91 dependencies via uv
2026/11/21 14:45:49 INFO mlflow.utils.environment: Successfully exported 91 requirements from uv project. Skipping package capture based inference.
ridge m-645caeb88a9c47538aae7af26159c0ba 0.341
🏃 View run loud-flea-53 at: http://127.0.0.1:8080/#/experiments/25/runs/4cc8f517913e4f47af7d9ad076bdd45f
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/25
ridge [('test_r2', 0.3322), ('test_r2', 0.341)]
linear [('test_r2', 0.3322)]

The output of two_models.py in a terminal: linear scores 0.3322 and ridge 0.341, but the final listing shows ridge with two test_r2 values, 0.3322 and 0.341, and linear with only 0.3322.

linear is right. ridge has its own 0.341 and also 0.3322, the linear model's score. The ridge model's page in the UI shows the same two rows:

The ridge model's Metrics (2) section in the MLflow UI: two test_r2 rows from the run loud-flea-53, one with 0.3322332173106 and one with 0.3409800318493.

What I Checked​

MLflow's reference for mlflow.log_metric says of model_id: "The ID of the model associated with the metric. If not specified, use the current active model ID set by mlflow.set_active_model(). If no active model exists, the models IDs associated with the specified or active run will be used." I passed a model_id every time, so each score was meant for one model only.

MLflow's code. When Model.log in mlflow/models/model.py logs a model inside a run, it ends by calling log_model_metrics_for_step. That reads the history of each of the run's metrics, and copies onto the new model every value at the model's step (0 by default) whose model_id is None, meaning not linked to a model yet. That's how a score logged in a run before its model still ends up on the model.

The run's metric history. So was the linear model's score unlinked? I read the run's test_r2 history:

metric_history.py
import sys

from mlflow import MlflowClient

client = MlflowClient("http://127.0.0.1:8080")
for metric in client.get_metric_history(sys.argv[1], "test_r2"):
print(round(metric.value, 4), "step", metric.step, "model_id", metric.model_id)
Terminal
uv run python metric_history.py 4cc8f517913e4f47af7d9ad076bdd45f
Output
0.3322 step 0 model_id None
0.341 step 0 model_id None

Both scores come back with model_id None, even the one logged with the linear model's ID.

Why It Happens​

Logging ridge triggered the copy. At that moment the run's test_r2 history held the linear model's score at step 0, and the history doesn't carry the model it was logged for. So the score looked unlinked, and MLflow copied it onto ridge. Then the script logged ridge's own score, linked by its model_id. That's how ridge ended up with two values. linear was logged first, when the run had no metrics, so it got only its own.

The Fix​

Log every model before logging any score, then log each score with its model's ID:

The fixed two_models_after.py in a macOS-style code window: inside one run, a loop trains and logs both models into a list, then a second loop logs each model's test_r2 with model_id, under a comment saying every model is logged before any score.

two_models_after.py
import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split

mlflow.set_tracking_uri("http://127.0.0.1:8080")
experiment = mlflow.set_experiment("two-models-after-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)

with mlflow.start_run():
logged = []
for name, model in [("linear", LinearRegression()), ("ridge", Ridge(alpha=1.0))]:
model.fit(X_train, y_train)
logged.append((model, mlflow.sklearn.log_model(model, name=name, input_example=X_train.head(3))))
# Every model is logged before any score, so each score goes to its own model only.
for model, model_info in logged:
mlflow.log_metric("test_r2", r2_score(y_test, model.predict(X_test)), model_id=model_info.model_id)

for model in mlflow.search_logged_models(experiment_ids=[experiment.experiment_id], output_format="list"):
print(model.name, [(metric.key, round(metric.value, 4)) for metric in model.metrics])

Now, when each model is logged, the run has no test_r2 yet, so there's nothing to copy.

After the Fix​

Terminal
uv run python two_models_after.py
Output
2026/11/21 14:46:23 INFO mlflow.tracking.fluent: Experiment with name 'two-models-after-demo' does not exist. Creating a new experiment.
2026/11/21 14:46:24 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:46:24 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:46:24 INFO mlflow.utils.environment: Detected uv project at /Users/you/source/mlflow-demo. Attempting to export requirements via 'uv export'.
2026/11/21 14:46:24 INFO mlflow.utils.uv_utils: Exported 91 dependencies via uv
2026/11/21 14:46:24 INFO mlflow.utils.environment: Successfully exported 91 requirements from uv project. Skipping package capture based inference.
2026/11/21 14:46:26 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:46:26 INFO mlflow.utils.uv_utils: Detected uv project: found uv.lock and pyproject.toml in /Users/you/source/mlflow-demo
2026/11/21 14:46:26 INFO mlflow.utils.environment: Detected uv project at /Users/you/source/mlflow-demo. Attempting to export requirements via 'uv export'.
2026/11/21 14:46:26 INFO mlflow.utils.uv_utils: Exported 91 dependencies via uv
2026/11/21 14:46:26 INFO mlflow.utils.environment: Successfully exported 91 requirements from uv project. Skipping package capture based inference.
🏃 View run stately-gnu-60 at: http://127.0.0.1:8080/#/experiments/26/runs/641380c8dfca461c9f86b95a83ee4954
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/26
ridge [('test_r2', 0.341)]
linear [('test_r2', 0.3322)]

The output of two_models_after.py in a terminal: the final listing shows ridge with one test_r2 of 0.341 and linear with one test_r2 of 0.3322.

One score each. The ridge model's page agrees:

The ridge model's Metrics (1) section in the MLflow UI: a single test_r2 row from the run stately-gnu-60, with 0.3409800318493.

Common Mistakes​

MistakeWhat happensDo this instead
Logging a model and its score, then the next model, in one runThe next model also gets the earlier scoreLog every model first, then the scores
Trusting model_id alone to keep the scores apartThe copy at log_model time ignores itOrder the calls as above

Troubleshooting​

SymptomLikely causeFix
A model shows two values for one metricA score logged earlier in the same run was copied onto it when it was loggedLog all the run's models before their scores
A model shows a metric you never linked to itSameSame

FAQ​

Why does MLflow copy metrics onto a model at all?​

So that a run's metrics logged before its model still belong to the model. With one model per run, that's what you want: How to Link a Metric to a Logged Model in MLflow shows a score logged before the model ending up on it.

Does one model per run avoid this?​

Yes: with one model in a run, every metric the run copies belongs to that model anyway. The order matters only when one run holds several models.

Conclusion​

The ridge model showed the linear model's score because log_model copies the run's unlinked metrics onto each new model, and the run's history didn't record that the linear score was already linked. Logging both models before either score left nothing to copy, and each model kept only its own test_r2.

Additional Resources​