How to Trace a Method in a Python Class with @mlflow.trace
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
import mlflow
class <Class>:
@mlflow.trace
def <method>(self, <parameters>):
...
| Part | What it means |
|---|---|
@mlflow.trace | Records each call of the method below it, on every object of the class |
self | The 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
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
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:
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))

@mlflow.tracesits right abovedef 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 asself.
Step 4: Run It
uv run trace_method.py
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 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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting the object's data, such as self.items, in the span | The span has only the method's own arguments and return value | Set the values you need as span attributes |
| Expecting the class name in the span's name | The span is named add, like the method | Give it another name with name= |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
There's no self in the span's inputs | MLflow leaves self out on purpose | Nothing to fix: record what you need as attributes |
| Creating the object made no trace | __init__ has no @mlflow.trace | Decorate 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.
