How to Nest Spans in an MLflow Trace
When one traced function calls other traced functions, MLflow doesn't make a separate trace for each of them. It builds one trace with a tree: the outer function's span on top, and a child span for each call inside it. This guide traces an order total made of two smaller steps, with real output from MLflow 3.18.0.
Syntax
import mlflow
@mlflow.trace
def <child function>(<parameters>):
...
@mlflow.trace
def <parent function>(<parameters>):
return ... <child function>(...) ...
| Part | What it means |
|---|---|
<child function> | A traced function that runs as one step of another |
<parent function> | The traced function that calls it. Its span becomes the trace's top, or root, span |
There's nothing extra to write: a traced function called while another traced function is running becomes its child. MLflow's manual tracing guide: "MLflow detects the parent-child relationships between functions, making it compatible with auto-tracing integrations."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Create a Project with MLflow
uv init --bare --python 3.15 tracing-examples
cd tracing-examples
uv add mlflow
This installs MLflow 3.18.0. Don't call the project mlflow or mlflow-tracing: those are names of packages MLflow needs, and uv then installs an old MLflow or fails to build pyarrow.
Step 2: Start the Tracking Server
In a second terminal, in the same folder:
uv run mlflow server --port 8080
Leave it running. How to Start an MLflow Tracking Server on Your Computer explains each line it prints.
Step 3: Write the Script
An order's total is its price with tax plus shipping. Each part is its own traced function, and order_total calls both:
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("nested-spans")
@mlflow.trace
def price_with_tax(price: float) -> float:
return round(price * 1.08, 2)
@mlflow.trace
def shipping(weight_kg: float) -> float:
return 4.99 if weight_kg <= 2 else 9.99
@mlflow.trace
def order_total(price: float, weight_kg: float) -> float:
return round(price_with_tax(price) + shipping(weight_kg), 2)
print(order_total(49.0, 2.5))

All three functions have @mlflow.trace, but only order_total is called from the top of the script. price_with_tax and shipping run inside it.
Step 4: Run It
uv run nested_spans.py
2026/12/03 08:15:33 INFO mlflow.tracking.fluent: Experiment with name 'nested-spans' does not exist. Creating a new experiment.
62.91
2026/12/03 08:15:33 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

$49.00 with 8% tax is $52.92, and a 2.5 kg parcel ships for $9.99: $62.91 in all.
Step 5: Open the Trace
Open http://127.0.0.1:8080, click nested-spans, then Traces:

Three traced functions ran, but there's one trace. Its input and output are those of order_total, the outer function. Click the row to open it:

The span list on the left is a tree. order_total is the root span, and price_with_tax and shipping sit under it, in the order they ran. order_total took 34.66 ms, which includes both of its children, since they ran inside it. Click shipping:

Each child keeps its own inputs and output: shipping received weight_kg 2.5 and returned 9.99. That's the point of nesting: when a result looks wrong, you can open each step and see which one produced it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Calling the steps one after another at the top of the script | Each call becomes its own trace, with nothing linking them | Call them from inside one traced function |
Leaving @mlflow.trace off a helper | That helper gets no span; its work is only part of its parent's time | Decorate every step you want to see |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Three traces instead of one tree | The functions weren't called from inside a traced function | Wrap them in an outer traced function, like order_total |
| A step is missing from the tree | The function has no @mlflow.trace | Add the decorator |
FAQ
Which span's inputs does the Traces list show?
The root span's. Here that's order_total: price 49.0 and weight 2.5 in, 62.91 out.
Does a child have to be in the same file?
No. What counts is that it runs while the parent's span is open, wherever the function is defined.
Can children have children of their own?
Yes, the same rule applies at every level: any traced function called inside a traced function becomes its child.
Conclusion
Calling price_with_tax and shipping from inside the traced order_total gave one trace, with order_total as the root span and the two steps as its children, each with its own inputs, output and time.
Additional Resources
- Manual Tracing — the decorator and how it nests spans.
- Trace concepts — what a trace is made of.
- Span concepts — what each span records.
