Skip to main content

How to Find the Run and Logged Model Behind a Model Version in MLflow

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

When a registered version misbehaves, the first questions are what trained it and with which settings. Every version records the run that trained it and the logged model it holds, so you can follow it back to the run's parameters and the model's own page. This guide does that from Python, with real output from MLflow 3.18.0.

Syntax​

Syntax
client = MlflowClient()
version = client.get_model_version(name, version_number)
run = client.get_run(version.run_id)
logged_model = mlflow.get_logged_model(version.source.removeprefix("models:/"))
PartWhat it means
version.run_idThe run that trained the model
version.sourceWhere the version's model comes from: models:/ followed by the logged model's ID
.removeprefix("models:/")The logged model's ID, without the address in front
runThe run, with its info (name, status) and data (parameters, metrics, tags)
logged_modelThe logged model, with its name, model_id, source_run_id and more

The ModelVersion reference describes run_id as "MLflow run ID that generated this model" and source as "Source path for the model". removeprefix is plain Python: How to Remove a Prefix from a String in Python with removeprefix.

Prerequisites​

  • An MLflow tracking server with a database backend at http://127.0.0.1:8080, such as mlflow server --port 8080 with its default SQLite database. The registry needs one.
  • mlflow and scikit-learn in your project, for example with uv add mlflow scikit-learn pandas.

Step 1: Follow a Version Back​

The example trains a ridge regression on scikit-learn's diabetes data in a run named ridge-alpha-1, logs its alpha, and registers it as version 1 of diabetes-lineage. Then it starts from the version alone: it prints what the version records, opens the run, and opens the logged model:

The version_lineage.py script in a macOS-style code window: a ridge regression registered as diabetes-lineage from run ridge-alpha-1, then get_model_version and prints of its source, run_id and model_id, get_run with the run's name and parameters, and get_logged_model from the source with its name, ID and whether its run matches.

version_lineage.py
import mlflow
from mlflow import MlflowClient
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("version-lineage-example")
client = MlflowClient()
name = "diabetes-lineage"

X, y = load_diabetes(return_X_y=True, as_frame=True)
with mlflow.start_run(run_name="ridge-alpha-1"):
mlflow.log_param("alpha", 1.0)
mlflow.sklearn.log_model(
Ridge(alpha=1.0).fit(X, y), name="model", registered_model_name=name, input_example=X.head(3)
)

version = client.get_model_version(name, "1")
print("source: ", version.source)
print("run_id: ", version.run_id)
print("model_id:", version.model_id)

run = client.get_run(version.run_id)
print("The run: ", run.info.run_name, run.data.params)

model_id = version.source.removeprefix("models:/")
logged_model = mlflow.get_logged_model(model_id)
print("The logged model:", logged_model.name, logged_model.model_id, logged_model.source_run_id == run.info.run_id)
Terminal
uv run python version_lineage.py
Output (last lines)
source: models:/m-b65b4ec8b80a4026a17fedc3bb307de5
run_id: 13f86eb5fb0a4986a2b34a8dbf6bbf19
model_id: None
The run: ridge-alpha-1 {'alpha': '1.0'}
The logged model: model m-b65b4ec8b80a4026a17fedc3bb307de5 True

The end of the output in a terminal: source models:/m-b65b4ec8b80a4026a17fedc3bb307de5, run_id 13f86eb5fb0a4986a2b34a8dbf6bbf19, model_id None, the run ridge-alpha-1 with alpha 1.0, and the logged model named model with the same ID and True.

  • source: the logged model's address, models:/m-b65b....
  • run_id: the run that trained it, which get_run opened: ridge-alpha-1, with alpha 1.0.
  • model_id: None: the version has a model_id field, but an open-source MLflow server doesn't store it, so it's always empty. The ID is in source instead. I reported it to MLflow as issue #28119.
  • The logged model: get_logged_model found it by that ID, named model as it was logged, and True confirms it came from the same run.

That empty model_id has a side effect when you evaluate a version: Fixed: MLflow Doesn't Link Evaluation Metrics to the Model When You Evaluate a Registered Model Version.

Common Mistakes​

MistakeWhat happensDo this instead
Reading version.model_id for the logged modelIt's None on an open-source MLflow serverTake the ID from version.source
Passing version.source to get_logged_model as it isThe call gets an address, not an IDStrip models:/ first

Troubleshooting​

SymptomLikely causeFix
API request to endpoint /api/2.0/mlflow/logged-models/models:/m-... failed with error code 404 != 200get_logged_model got the whole addressStrip models:/ first
The same 404 for /logged-models/tumour-staging/1The version was copied from another registered model, so its source is that version's address, models:/tumour-staging/1Read the source of tumour-staging version 1 instead

FAQ​

Can I see the same thing in the UI?​

Yes. A version's page in the Model Registry shows its Source Run and Source Model.

Does the logged model know its versions?​

Yes. The logged model's page lists Model versions, the registered versions made from it.

Conclusion​

Starting from version 1 of diabetes-lineage alone, version.run_id led to the run ridge-alpha-1 and its alpha, and version.source led to the logged model, while version.model_id stayed None.

Additional Resources​