Skip to main content

How to See an Exception in an MLflow Trace

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A trace is most useful when something goes wrong: it shows which step failed and why. When a function traced with @mlflow.trace raises an exception, MLflow marks its span as an error and keeps the exception's details. This guide makes one step fail twice, once inside a function that handles the error and once on its own, with real output from MLflow 3.18.0.

How MLflow Records an Exception​

There's nothing to add to your code: @mlflow.trace does it. The mlflow.trace reference: "Any exception raised within the function will set the span status to ERROR and detailed information such as exception message and stacktrace will be recorded". MLflow's manual tracing guide adds that the details "will be included within Events of the partially completed span".

Two things decide what you see:

  • The span of the function that raised the exception is marked as an error.
  • The trace as a whole is marked as an error only if the exception leaves the outermost traced function. If a traced function higher up catches it, the trace stays OK, with the failed step marked inside it.

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​

share splits a bill between people, and fails with ZeroDivisionError when there are none. split_bill calls it and turns that error into None. The script runs both cases:

trace_exception.py
import mlflow

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


@mlflow.trace
def share(total: float, people: int) -> float:
return round(total / people, 2)


@mlflow.trace
def split_bill(total: float, people: int) -> float | None:
try:
return share(total, people)
except ZeroDivisionError:
return None


print(split_bill(90.0, 0))
try:
share(90.0, 0)
except ZeroDivisionError as error:
print("Couldn't share the bill:", error)

The trace_exception.py script in a macOS-style code window: share divides the total by the number of people, split_bill calls share and returns None on ZeroDivisionError, and the script calls split_bill(90.0, 0), then share(90.0, 0) inside its own try block.

  • split_bill(90.0, 0) makes share fail inside split_bill, which catches the error.
  • share(90.0, 0) on its own fails with nothing traced around it; the script's try block, which isn't traced, catches the error.

Step 4: Run It​

Terminal
uv run trace_exception.py
Output
2026/12/03 08:15:38 INFO mlflow.tracking.fluent: Experiment with name 'trace-exceptions' does not exist. Creating a new experiment.
None
Couldn't share the bill: division by zero
2026/12/03 08:15:38 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-exceptions experiment, split_bill prints None, the second call prints Couldn't share the bill: division by zero, and MLflow flushes its trace logging queue.

Tracing didn't swallow the exception: in both cases it reached the code that catches it, as it would without MLflow.

Step 5: Compare the Two Traces​

Open http://127.0.0.1:8080, click trace-exceptions, then Traces:

The Traces page of the trace-exceptions experiment: the newer trace has the state Error and no output, the older one has the state OK and the output null; both have the input total 90.0 and people 0.

The older trace, from split_bill, is OK: its output is null, the None that split_bill returned. The newer one, from share on its own, is an Error with no output. Open the OK trace:

The trace view of split_bill, marked Success: split_bill is the root span, and its child share is shown in red with a red dot, the sign of a failed span.

The trace says Success, because split_bill handled the error, but the share span under it is red: that step failed. Click share; its Events tab opens:

The Events tab of the failed share span: an Exception event with exception.message division by zero and exception.type ZeroDivisionError.

The event holds the exception: exception.message is division by zero and exception.type is ZeroDivisionError. Below them, exception.stacktrace holds the full traceback. Now open the Error trace:

The trace view of the trace marked Error: its only span, share, is red, and its Events tab shows the same division by zero ZeroDivisionError exception.

Here share is the only span and nothing traced caught its exception, so the whole trace is marked Error, with the same exception in the span's Events.

Common Mistakes​

MistakeWhat happensDo this instead
Looking only at the trace's stateA trace can be OK while a step inside it failedLook for red spans in the tree
Catching the exception inside the traced function itselfThe span stays OK, with no exception event: the decorator never sees the errorLet the error leave the function, or catch it one traced level up, as split_bill does

Troubleshooting​

SymptomLikely causeFix
The trace is OK but a span is redA traced function higher up caught the exceptionOpen the red span's Events tab to see the error
The trace is marked ErrorThe exception left the outermost traced functionOpen the red span's Events tab, then fix or handle the error
A failed step has no exception eventThe function caught its own exceptionCheck the function's try blocks

FAQ​

Does tracing stop my exception from reaching my code?​

No. In the run above, the ZeroDivisionError reached the script's own try block and printed its message, exactly as it would without MLflow.

What output does a failed span have?​

None: the function never returned. The Error trace's output is empty, and the failed share span's output is null.

Where's the traceback?​

In the same event, under exception.stacktrace, below the message and type.

Conclusion​

When share raised ZeroDivisionError, MLflow marked its span red and recorded the message and type in its Events tab. Inside split_bill, which caught the error, the trace stayed OK with the failed step visible. On its own, the failure marked the whole trace as an Error.

Additional Resources​