Skip to main content

How to Nest Spans in an MLflow Trace

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import mlflow


@mlflow.trace
def <child function>(<parameters>):
...


@mlflow.trace
def <parent function>(<parameters>):
return ... <child function>(...) ...
PartWhat 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​

Terminal
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:

Terminal 2
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:

nested_spans.py
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))

The nested_spans.py script in a macOS-style code window: price_with_tax and shipping are traced functions, and order_total, also traced, returns the sum of the two; the script prints 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​

Terminal
uv run nested_spans.py
Output
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...

The output in a terminal: MLflow creates the nested-spans experiment, the script prints 62.91, and MLflow flushes its trace logging queue.

$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:

The Traces page of the nested-spans experiment: a single trace with the input price 49.0 and weight_kg 2.5, the output 62.91 and a duration of 34 milliseconds.

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 trace view: order_total at the top of the span tree, with price_with_tax and shipping indented below it as its children; order_total's inputs are price 49 and weight_kg 2.5 and its output is 62.91.

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:

The shipping span selected in the tree, with its own input weight_kg 2.5 and output 9.99.

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​

MistakeWhat happensDo this instead
Calling the steps one after another at the top of the scriptEach call becomes its own trace, with nothing linking themCall them from inside one traced function
Leaving @mlflow.trace off a helperThat helper gets no span; its work is only part of its parent's timeDecorate every step you want to see

Troubleshooting​

SymptomLikely causeFix
Three traces instead of one treeThe functions weren't called from inside a traced functionWrap them in an outer traced function, like order_total
A step is missing from the treeThe function has no @mlflow.traceAdd 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​