Search traces programmatically
Search and analyze traces programmatically using mlflow.search_traces(). This function can query traces stored in the MLflow tracking server, inference tables, or Unity Catalog tables. You can select subsets of traces to analyze or to create evaluation datasets.
mlflow.search_traces() API
def mlflow.search_traces(
experiment_ids: list[str] | None = None,
filter_string: str | None = None,
max_results: int | None = None,
order_by: list[str] | None = None,
extract_fields: list[str] | None = None,
run_id: str | None = None,
return_type: Literal['pandas', 'list'] | None = None,
model_id: str | None = None,
sql_warehouse_id: str | None = None,
include_spans: bool = True,
locations: list[str] | None = None,
) -> pandas.DataFrame | list[Trace]
mlflow.search_traces() lets you filter and select data along a few dimensions:
- Filter by a query string
- Filter by locations: experiment, run, model, or Unity Catalog schema
- Limit data: max results, include or exclude spans
- Adjust return value format: data format, data order
search_traces() returns either a pandas DataFrame or a list of Trace objects, which can then be analyzed further or reshaped into evaluation datasets. See the schema details of these return types.
See the mlflow.search_traces() API docs for full details.
Databricks-managed MLflow and OSS (open source software) MLflow share most search query syntax but have a few field-level differences. See Differences from OSS MLflow for details.
mlflow.search_traces() parameters
Category |
| Description | Example |
|---|---|---|---|
Filter by query string |
| See Search query syntax for supported filters and comparators. |
|
Filter by locations |
| This argument can be list of experiment IDs or Unity Catalog |
|
| MLflow run ID |
| |
| MLflow model ID |
| |
Limit data |
| Max number of traces (rows) to return |
|
| Include or exclude spans from the results. Spans include trace details and can make result sizes much larger. |
| |
Return value format |
| See the syntax and supported keys. |
|
| This function can return either a pandas DataFrame or a list of |
| |
Deprecated |
| Use | |
| Select fields in the returned DataFrame or trace objects instead. | ||
| Use the |
Search query syntax
The filter_string argument uses a SQL-like query language to filter traces. String values must be wrapped in single quotes (for example, trace.status = 'OK'), and numeric values must not be quoted (for example, trace.execution_time_ms > 1000). Combine conditions with AND. The OR operator is not supported.
Supported filters and comparators
The following fields and comparators are supported on Databricks-managed MLflow.
Filters marked (UC only) are supported only for MLflow traces stored in Unity Catalog. See Store OpenTelemetry traces in Unity Catalog.
Field type | Fields | Comparators | Example |
|---|---|---|---|
Trace status |
|
|
|
Trace timestamps |
|
|
|
Trace IDs |
|
|
|
String fields |
|
|
|
Request and response content (UC only) |
|
|
|
Token count (UC only) |
|
|
|
Linked prompts |
|
|
|
Span name, type, and status (UC only) |
|
|
|
OTel span attributes (UC only) |
|
|
|
Tags |
|
For MLflow traces stored in an experiment (not in Unity Catalog), only |
|
Metadata |
|
For MLflow traces stored in an experiment (not in Unity Catalog), only |
|
Feedback (UC only) |
|
|
|
Expectations (UC only) |
|
|
|
Differences from OSS MLflow
The search query syntax on Databricks-managed MLflow closely tracks OSS MLflow, with the following differences:
Field | Databricks-managed MLflow | OSS MLflow | Notes |
|---|---|---|---|
| Supported (UC only) | Not supported | Use these fields to filter on serialized request and response content. |
| Supported (UC only) | Not supported | Filter traces by total token count. |
| Supported (UC only) | Not supported | Filter traces by OpenTelemetry span attributes. |
| Not supported | Supported (SQLAlchemy store only) | OSS exposes |
| Not supported | Supported (mapped to linked prompts tag) | On Databricks, use the top-level |
| Not supported | Supported | On Databricks, use |
| Not supported | Supported | Filter traces linked to a specific issue ID. |
Search for third-party OpenTelemetry spans
To search traces ingested from third-party OpenTelemetry tools such as Langfuse, use the span.attributes.* prefix instead. See Search for traces by OTel span attributes.
Best practices
Keyword arguments
Always use keyword (named) arguments with mlflow.search_traces(). It allows positional arguments, but the function arguments are evolving.
Good practice: mlflow.search_traces(filter_string="trace.status = 'OK'")
Bad practice: mlflow.search_traces([], "trace.status = 'OK'")
filter_string gotchas
When searching using the filter_string argument to mlflow.search_traces(), remember to:
- Use prefixes:
trace.,tag., ormetadata. - Use backticks if tag or attribute names have dots:
tag.`mlflow.traceName` - Use single quotes only:
'value'not"value" - Use Unix timestamp (milliseconds) for time:
1749006880539not dates - Use AND only: No OR support
See Search query syntax for the full list of supported fields and operators.
SQL warehouse integration
mlflow.search_traces() can optionally use a Databricks SQL warehouse to improve performance on large trace datasets in inference tables or Unity Catalog tables. Specify your SQL warehouse ID using the MLFLOW_TRACING_SQL_WAREHOUSE_ID environment variable.
Execute trace queries using a Databricks SQL warehouse for improved performance on large trace datasets:
import os
os.environ['MLFLOW_TRACING_SQL_WAREHOUSE_ID'] = 'fa92bea7022e81fb'
# Use SQL warehouse for better performance
traces = mlflow.search_traces(
filter_string="trace.status = 'OK'",
locations=['my_catalog.my_schema'],
)
Pagination
mlflow.search_traces() returns results in memory, which works well for smaller result sets. To handle large result sets, use MlflowClient.search_traces() since it supports pagination.
Next steps
- Tutorial: Search traces programmatically - Run a set of simple examples of
mlflow.search_traces() - Tutorial: Trace and analyze users and environments - Run an example of adding context metadata to traces and analyzing the results
- Examples: Analyzing traces - See a variety of examples of trace analysis
- Build evaluation datasets - Convert queried traces into test datasets