Sub topic 13
Searching and inspecting registered models
This chapter assumes you can already register a model, set tags, and assign aliases with MlflowClient. It also assumes a tracking server with a SQL backend is running at http://127.0.0.1:5000 and holds a registered model called iris-classifier. Here you will learn to answer operational questions about the registry from code. Which models exist? Which version is champion? Which run produced it? Which versions have not been validated?
The shape of what you are querying
The registry has two kinds of records, and the search calls map onto them.
- A registered model is a named container. It has a name, a description, tags, and a map of aliases to version numbers.
- A model version belongs to one registered model. It has a version number, a source (the artifact location), usually a
run_id, tags, a description, and a list of aliases that currently point at it.
Every inspection task is a walk along these links. You start from a name or an alias, find a version, and follow run_id to the run's parameters and metrics.
Listing and filtering registered models
search_registered_models returns registered models that match a filter string. It returns a PagedList, which behaves like a list but also carries a token for fetching the next page. When token is empty, there are no more pages.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
token = None
while True:
page = client.search_registered_models(
filter_string="name LIKE 'iris%'",
order_by=["last_updated_timestamp DESC"],
max_results=50,
page_token=token,
)
for rm in page:
print(rm.name, dict(rm.aliases))
token = page.token
if not token:
breakThe filter string uses a small SQL-like language, not full SQL. The table lists what is commonly supported. Your server version decides the exact set, so check the MLflow documentation for the version you run.
| Search call | Filterable fields | Comparators |
|---|---|---|
| `search_registered_models` | `name`, `tags.<key>` | `=`, `!=`, `LIKE`, `ILIKE` |
| `search_model_versions` | `name`, `run_id`, `version_number`, `source_path` | `=`, `!=`, `LIKE`, `ILIKE` on `name`; `=`, `!=`, `IN` on `run_id`; numeric comparison on `version_number` |
Conditions are joined with AND. OR is not supported. LIKE is case-sensitive and ILIKE is not. % matches any run of characters.
Some practical points:
- With no filter, the call returns every registered model, which is slow on a large registry. Always set
max_resultsand page through the results. - If a tag key contains characters such as dots or dashes, wrap it in backticks: `
tags.team.owner= 'fraud'`. - The
order_bylist acceptsname,creation_timestamp, andlast_updated_timestamp, each withASCorDESC. Without an explicit order, do not rely on the order you get back.
Inspecting one registered model
Once you know the name, get_registered_model returns the full record.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
rm = client.get_registered_model("iris-classifier")
print("description:", rm.description)
print("tags:", rm.tags)
print("aliases:", rm.aliases) # for example {'champion': '2'}rm.aliases is the quickest answer to "what is live right now?". The map is keyed by alias and its values are version numbers as strings.
A registered model also has a latest_versions field. It belongs to the stage system that was deprecated, and it returns the newest version per stage, not the newest version overall. Do not use it to find the newest version or the live one. Use search_model_versions or an alias instead.
Searching model versions
search_model_versions is the call you will use most. It lists versions across one or many registered models.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
versions = client.search_model_versions(
filter_string="name = 'iris-classifier'",
order_by=["version_number DESC"],
max_results=20,
)
for mv in versions:
print(mv.version, mv.status, mv.run_id, mv.aliases, mv.tags)Version numbers are strings on the returned objects, such as "2". In the filter, version_number is compared as a number, so `version_number > 1 works. Convert with int(mv.version) before you compare versions in Python. Sorting the strings gives "10" before "2"`.
Here are two common queries.
- All versions produced by one run:
run_id = 'b2f0...'. This answers "did this training run ever get registered?". - All versions of every model whose name matches a pattern:
name LIKE 'iris-classifier%'. This is a convenient way to see the-stagingand-prodregistered models side by side when you promote by copying.
Build filter strings only from values you trust. If a name comes from user input, reject any value that contains a single quote before you place it inside quotes in the filter.
Resolving an alias and tracing it to a run
To check what an alias means right now, resolve it. Then follow the version's run_id to the run that produced it.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
mv = client.get_model_version_by_alias("iris-classifier", "champion")
print("champion is version", mv.version, "source:", mv.source)
if mv.run_id:
run = client.get_run(mv.run_id)
print("params:", run.data.params)
print("metrics:", run.data.metrics)
print("started:", run.info.start_time)get_model_version_by_alias raises an MlflowException if the alias does not exist. Catch it in code that runs unattended, because a missing alias is a normal state before the first promotion and after a rollback that deletes the alias.
Check mv.run_id before you use it. A version that was registered from a plain artifact path, or one that was copied from another registered model, may not carry a run you can load.
For a specific version number, use get_model_version(name, version).
Auditing with tags
Tags record validation results, so a registry query can check them. The following script reports every version that has not passed validation, and flags the one that is currently champion.
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
name = "iris-classifier"
versions = client.search_model_versions(
filter_string=f"name = '{name}'",
order_by=["version_number DESC"],
max_results=200,
)
for mv in versions:
status = mv.tags.get("validation_status", "missing")
if status != "passed":
live = " (LIVE under champion)" if "champion" in mv.aliases else ""
print(f"v{mv.version}: validation_status={status}{live}")The script filters on the tag in Python instead of in the filter string. Tag filtering in version searches depends on the MLflow release, and filtering client-side works on all of them. The cost is that you transfer every version first, which is fine for dozens or hundreds of versions. If the registry is large, check whether your server supports tag filters on versions and move the condition into the filter string.
The same pattern finds any version with a live alias and a missing or failed check. You could run it as a scheduled job to catch a promotion that skipped the gate.
Common errors
- Empty results from a filter you expect to match. The usual causes are double quotes around the value (use single quotes),
ORin the filter (unsupported), and case mismatch withLIKE(useILIKE). - Only the first page appears. You ignored
page.token. Loop until it is empty, as in the first example. mv.version == 10is always false. Versions are strings. Comparemv.version == "10"or convert withint.RESOURCE_DOES_NOT_EXISTon an alias lookup. The alias was never set or was deleted, for instance by a rollback. Treat it as "nothing promoted".- Search calls fail on a file-based tracking store. The registry needs the SQL backend you configured earlier. Point the client at the server URI, not at a local
mlrunsdirectory.
Exercise
Write a client-side check that lists the version numbers (as integers, newest first) of iris-classifier whose validation_status tag is passed, and prints the highest one. Why is the sort done on integers?
Show answer
Use versions = client.search_model_versions("name = 'iris-classifier'", max_results=200), then passed = sorted((int(mv.version) for mv in versions if mv.tags.get("validation_status") == "passed"), reverse=True), then print(passed, passed[0] if passed else None). The sort uses integers because mv.version is a string, and a string sort puts "10" before "2", which would report the wrong newest version.
Exercise
A teammate says the live model was trained in run b2f0, but your serving code loads models:/iris-classifier@champion. Which two calls confirm or refute the claim, and what must you check on the returned version before using its run?
Show answer
Call client.get_model_version_by_alias("iris-classifier", "champion") to find the version behind the alias, then compare its run_id with b2f0 and fetch the run with client.get_run(mv.run_id) for its params and metrics. Check that mv.run_id is not empty first, because versions registered from a plain path or copied from another registered model may have no usable run.