Skip to main content

How to Trace a Method in a Python Class with @mlflow.trace

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Real code often keeps its steps in a class: an agent with a reply method, a set of tools on one object. @mlflow.trace works on a method the same way it works on a plain function, with one difference: MLflow leaves out self. This guide traces a method of a small shopping-cart class, with real output from MLflow 3.18.0.

Syntax​

Syntax
import mlflow


class <Class>:
@mlflow.trace
def <method>(self, <parameters>):
...
PartWhat it means
@mlflow.traceRecords each call of the method below it, on every object of the class
selfThe object the method is called on. MLflow doesn't record it
<parameters>The method's other arguments. MLflow records them as the span's inputs

The mlflow.trace reference: "When it is applied to a method, it doesn't capture the self argument."

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​

Cart holds a list of items, and its add method puts an item in the cart a number of times, then returns how many items the cart holds:

trace_method.py
import mlflow

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


class Cart:
def __init__(self) -> None:
self.items: list[str] = []

@mlflow.trace
def add(self, item: str, quantity: int) -> int:
self.items.extend([item] * quantity)
return len(self.items)


cart = Cart()
print(cart.add("headlamp", 2))

The trace_method.py script in a macOS-style code window: the Cart class keeps a list of items, its add method is decorated with @mlflow.trace and extends the list, and the script creates a cart and prints cart.add("headlamp", 2).

  • @mlflow.trace sits right above def add, inside the class, the same place it goes on a plain function.
  • __init__ has no decorator, so creating the cart isn't traced.
  • cart.add("headlamp", 2) calls the method. Python passes the cart itself as self.

Step 4: Run It​

Terminal
uv run trace_method.py
Output
2026/12/03 08:15:41 INFO mlflow.tracking.fluent: Experiment with name 'trace-a-method' does not exist. Creating a new experiment.
2
2026/12/03 08:15:41 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-method experiment, the script prints 2, and MLflow flushes its trace logging queue.

The cart now holds two headlamps, so add returned 2.

Step 5: Open the Trace​

Open http://127.0.0.1:8080, click trace-a-method, then Traces, and click the trace:

The trace view of the add span: the span is named add, its inputs are item headlamp and quantity 2, with no self, and its output is 2.

The span is named add, after the method. Its inputs are item and quantity, the arguments the call passed, and its output is 2. There's no self: MLflow left the cart object out, as its reference says.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting the object's data, such as self.items, in the spanThe span has only the method's own arguments and return valueSet the values you need as span attributes
Expecting the class name in the span's nameThe span is named add, like the methodGive it another name with name=

Troubleshooting​

SymptomLikely causeFix
There's no self in the span's inputsMLflow leaves self out on purposeNothing to fix: record what you need as attributes
Creating the object made no trace__init__ has no @mlflow.traceDecorate only the methods you want to see

FAQ​

Does every object of the class get traced?​

Yes. The decorator is on the method in the class, so a call of add on any Cart is recorded.

Can I trace a @classmethod or a @staticmethod?​

The mlflow.trace reference lists both as supported since MLflow 3.0.0.

Can a method's span have a span type?​

Yes. Decorator parameters work the same on a method, for example @mlflow.trace(span_type=SpanType.TOOL), as in How to Set the Span Type of a Traced Function in MLflow.

Conclusion​

@mlflow.trace above the add method recorded each call as a span named add, with item and quantity as inputs and the new item count as output. MLflow left out self, so the span shows the call, not the whole cart.

Additional Resources​

  • mlflow.trace — the decorator, how it treats methods, and the function types it supports.
  • Manual Tracing — MLflow's guide to tracing your own code.