Sub topic 8
Searching Runs with `search_runs`
This chapter assumes you know basic Python, pandas DataFrames, MLflow experiments and runs, and common metric names such as accuracy. You should also have runs stored in an MLflow tracking server or local tracking database.
When an experiment contains many runs, clicking through the UI is useful for exploration but inefficient for repeatable analysis. MLflow’s search_runs() function lets you filter, sort, and limit runs in Python. It returns a pandas DataFrame, so you can use familiar DataFrame operations afterward.
What `search_runs()` returns
Each row represents one MLflow run. The columns include run metadata and flattened values for parameters, metrics, and tags.
| Column pattern | Meaning | Example |
|---|---|---|
| `run_id` | Unique identifier for the run | `a13f...` |
| `params.<name>` | A logged parameter | `params.C` |
| `metrics.<name>` | The latest value of a metric | `metrics.test_accuracy` |
| `tags.<name>` | A logged tag | `tags.stage` |
| `status` | Run state | `FINISHED` |
A metric can have a history, such as one value per training epoch. In the search result, metrics.loss normally represents the latest logged value. Use the metric-history API when you need every step.
Filtering and sorting runs
The filter_string argument uses MLflow’s search language. Compare numeric metrics with operators such as >, <, and =, and compare strings with quoted values. Use and or or to combine conditions.
The order_by argument accepts a list of expressions. Add DESC for descending order or ASC for ascending order.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
runs = mlflow.search_runs(
experiment_names=["iris-baseline"],
filter_string=(
"metrics.test_accuracy > 0.90 "
"and tags.stage = 'baseline' "
"and attributes.status = 'FINISHED'"
),
order_by=["metrics.test_accuracy DESC"],
max_results=5,
)
columns = [
"run_id",
"params.C",
"metrics.test_accuracy",
"tags.stage",
]
print(runs[columns].to_string(index=False))max_results limits the number of returned rows. This is useful when the experiment contains thousands of runs. If you need a stable tie-breaker, add another ordering expression, such as attributes.start_time DESC.
You can search by experiment ID instead of name:
runs = mlflow.search_runs(
experiment_ids=["123456789"],
order_by=["attributes.start_time DESC"],
max_results=20,
)Use either experiment_names or experiment_ids to make the search scope explicit. Searching across all experiments can accidentally mix incompatible metrics, datasets, or model versions.
Understanding filter values
Parameters and tags are stored as strings. Therefore, a parameter filter compares text, even when the parameter represents a number. Metric filters use numeric values.
For example, this filters a metric numerically:
metrics.test_accuracy >= 0.90This filters a parameter by its recorded text:
params.solver = 'lbfgs'A parameter filter such as params.C > 1 is not a reliable numeric comparison. If numeric analysis of parameters is important, convert the selected column after searching:
runs["C_number"] = runs["params.C"].astype(float)
large_C = runs[runs["C_number"] > 1.0]Missing values are another common source of surprises. A run that did not log test_accuracy will not satisfy a comparison involving that metric. Log a consistent set of metrics across comparable runs.
From search results to a decision
A search should reflect the decision you are making. If you want the most accurate finished baseline, filter for finished runs and sort by accuracy. If you want a model that balances accuracy and latency, search for both metrics and apply the final trade-off in pandas.
candidates = mlflow.search_runs(
experiment_names=["iris-baseline"],
filter_string="attributes.status = 'FINISHED'",
order_by=["metrics.test_accuracy DESC"],
max_results=50,
)
candidates = candidates.dropna(
subset=["metrics.test_accuracy", "metrics.prediction_ms"]
)
acceptable = candidates[candidates["metrics.prediction_ms"] < 10]
print(acceptable.head(3)[
["run_id", "metrics.test_accuracy", "metrics.prediction_ms"]
])Do not treat the first row as automatically being the best production choice. Check the evaluation dataset, code version, data version, and other tags before selecting a run.
Common errors
- Wrong experiment: Runs from separate experiments may use different datasets or metric definitions.
- Wrong metric direction: Accuracy is usually maximized, while loss and latency are usually minimized.
- Unlogged values:
search_runs()cannot filter on information that was never logged. - Parameter type assumptions: Parameters are strings in search results.
- Unstable selection: Tied metrics can return an arbitrary order unless you add a tie-breaker.
- Incomplete filtering: Filtering only on a high score can select a failed, exploratory, or outdated run.
Use tags such as dataset_version, git_commit, stage, and owner to make searches more meaningful. Consistent tag names matter more than elaborate queries.
Exercise
Write a search that returns at most three finished runs from the iris-baseline experiment, with test_accuracy above 0.90, sorted from highest to lowest accuracy.
Show answer
Use mlflow.search_runs(experiment_names=["iris-baseline"], filter_string="metrics.test_accuracy > 0.90 and attributes.status = 'FINISHED'", order_by=["metrics.test_accuracy DESC"], max_results=3).