Skip to main content

How to Trace a Python Function with @mlflow.trace

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

MLflow can record any Python function you write, not only calls to a model: what went in, what came out and how long it took. You add one line, @mlflow.trace, above the function. This guide traces two small functions on your own MLflow server, one with its own name and one with a custom name, with real output from MLflow 3.18.0.

Syntax​

Syntax
import mlflow


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


@mlflow.trace(name="<span name>")
def <function>(<parameters>):
...
PartWhat it means
@mlflow.traceA decorator: from now on, each call of the function below it is recorded
<function>, <parameters>Your own function, unchanged
name="<span name>"Optional. The name the recording gets in MLflow; without it, MLflow uses the function's name

MLflow calls each recorded step a span, and a whole recorded call a trace. The mlflow.trace reference: "A decorator that creates a new span for the decorated function. [...] The span will automatically capture the input and output of the function." MLflow's manual tracing guide lists the name parameter "to override the span name from the default (the name of decorated function)".

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

uv init --bare creates a folder with only a pyproject.toml, and uv add mlflow installs MLflow 3.18.0 into the project. 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​

The example works out prices: one function adds sales tax, the other totals a basket.

add_tax.py
import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("trace-a-function")


@mlflow.trace
def add_tax(price: float, rate: float) -> float:
return round(price * (1 + rate), 2)


@mlflow.trace(name="basket_total")
def total(prices: list[float]) -> float:
return round(sum(prices), 2)


print(add_tax(19.99, 0.08))
print(total([4.5, 12.25, 3.0]))

The add_tax.py script in a macOS-style code window: the tracking URI and experiment are set, add_tax is decorated with @mlflow.trace, total is decorated with @mlflow.trace(name="basket_total"), and both are called and printed.

  • mlflow.set_tracking_uri(...) sends the traces to the server from Step 2, and mlflow.set_experiment(...) files them under an experiment named trace-a-function, which MLflow creates if it's missing.
  • @mlflow.trace on add_tax records each call under the function's own name.
  • @mlflow.trace(name="basket_total") on total records each call under the name basket_total instead.

The functions themselves don't change: they take the same arguments and return the same values as without the decorator.

Step 4: Run It​

Terminal
uv run add_tax.py
Output
2026/12/03 08:15:28 INFO mlflow.tracking.fluent: Experiment with name 'trace-a-function' does not exist. Creating a new experiment.
21.59
19.75
2026/12/03 08:15:29 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 trace-a-function experiment, the script prints 21.59 and 19.75, and MLflow flushes its trace logging queue before the program exits.

The two results print as usual: 19.99 with 8% tax is 21.59, and the basket adds up to 19.75. MLflow sends traces in the background, so its last line is it finishing that work before the program exits.

Step 5: Open the Traces​

Open http://127.0.0.1:8080 in a browser, click trace-a-function, then Traces in the left sidebar:

The Traces page of the trace-a-function experiment: two traces, one with the input prices 4.5, 12.25 and 3.0 and the output 19.75, the other with the input price 19.99 and rate 0.08 and the output 21.59, both OK.

Each call became its own trace. The Input column holds the function's arguments, by parameter name, and Output holds what it returned. Click the row with "price": 19.99 to open that trace:

The trace view of add_tax: one span named add_tax that took about 61 milliseconds, with the inputs price 19.99 and rate 0.08 and the output 21.59.

The trace holds one span, named add_tax after the function, with its inputs, its output and its duration. Now open the other trace:

The trace view of the second call: one span named basket_total, with the input prices listed as 4.5, 12.25 and 3, and the output 19.75.

The function is called total, but its span is named basket_total, the name passed to the decorator. The Pretty view shows 3.0 as 3; the trace's JSON keeps the number as it was sent.

Common Mistakes​

MistakeWhat happensDo this instead
No mlflow.set_tracking_uri(...) and no tracking server runningThe traces aren't on the server you open in the browserSet the URI to your server, such as http://127.0.0.1:8080
Writing @mlflow.trace("basket_total")Python stops at the decorator line with AttributeError: 'str' object has no attribute '__name__'Pass the name as a keyword: @mlflow.trace(name="basket_total")

Troubleshooting​

SymptomLikely causeFix
The Traces page is emptyThe page shows another experimentClick the experiment named in set_experiment
The traces appear a moment lateMLflow sends them in the backgroundRefresh the page; the "Flushing" line means they were on their way

FAQ​

Does @mlflow.trace change what my function returns?​

No. The function gets the same arguments and returns the same value; MLflow only records the call.

What if one traced function calls another?​

The inner call becomes a child span inside the outer call's trace, so you get one trace with a tree of spans instead of separate traces.

Do I need an LLM to use tracing?​

No. Any Python function can be traced, as this guide does with two price calculations. Tracing is most useful in AI apps, where it shows each step an agent takes.

Conclusion​

@mlflow.trace turned each call of add_tax and total into a trace, with the arguments as inputs, the return value as output and the time taken. The span was named after the function unless name= gave it another name, like basket_total.

Additional Resources​