How to Add Attributes to a Span in MLflow
A span records a function's inputs and output, but sometimes you want more on it: which model answered, which carrier was chosen, whether an order was heavy. MLflow calls these extra details attributes. This guide adds a fixed attribute in the decorator and two more from inside the function, with real output from MLflow 3.18.0.
Syntax
import mlflow
@mlflow.trace(attributes={"<key>": <value>})
def <function>(<parameters>):
span = mlflow.get_current_active_span()
if span is not None:
span.set_attributes({"<key>": <value>, ...})
...
| Part | What it means |
|---|---|
attributes={...} | Attributes known when you write the code, set on every call |
mlflow.get_current_active_span() | The span that's recording right now, or None when nothing is |
span.set_attributes({...}) | Adds attributes whose values are only known while the function runs |
if span is not None: | Skips the attributes when the call isn't being traced |
MLflow's manual tracing guide lists the decorator's "attributes parameter to add custom attributes to the span", and adds: "Alternatively, you can update the span dynamically inside the function." The get_current_active_span reference says it returns "The current active span if exists, otherwise None."
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
The example picks a shipping method from a parcel's weight. The carrier is always the same, so it goes in the decorator. The method and whether the parcel is heavy depend on the weight, so the function sets them itself.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("span-attributes")
@mlflow.trace(attributes={"carrier": "Parcelway"})
def pick_shipping(weight_kg: float) -> str:
method = "express" if weight_kg <= 1 else "ground"
span = mlflow.get_current_active_span()
if span is not None:
span.set_attributes({"method": method, "heavy": weight_kg > 20})
return method
print(pick_shipping(0.8))

attributes={"carrier": "Parcelway"}putscarrieron the span of every call.mlflow.get_current_active_span()returns the span the decorator opened for this call.span.set_attributes(...)addsmethod, a text value, andheavy, aTrueorFalsevalue, worked out from the weight.
Step 4: Run It
uv run span_attributes.py
2026/12/03 08:15:36 INFO mlflow.tracking.fluent: Experiment with name 'span-attributes' does not exist. Creating a new experiment.
express
2026/12/03 08:15:36 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

A 0.8 kg parcel goes by express.
Step 5: See the Attributes
Open http://127.0.0.1:8080, click span-attributes, then Traces, and click the trace. In the trace view, open the Attributes tab:

All three attributes are on the span: carrier from the decorator, method and heavy from set_attributes. heavy kept its type and shows as false.
When the Call Isn't Traced
get_current_active_span() returns None when no span is recording, for example after mlflow.tracing.disable(), which switches tracing off. That's why the script checks for None:
import mlflow
mlflow.tracing.disable()
@mlflow.trace
def pick_shipping(weight_kg: float) -> str:
method = "express" if weight_kg <= 1 else "ground"
span = mlflow.get_current_active_span()
print("Current span:", span)
if span is not None:
span.set_attributes({"method": method})
return method
print(pick_shipping(25.0))
uv run tracing_off.py
Current span: None
ground
With tracing off, there was no span, and the function still did its job. Without the if span is not None: check, the same call stops with AttributeError: 'NoneType' object has no attribute 'set_attributes'.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling span.set_attributes(...) without checking for None | AttributeError: 'NoneType' object has no attribute 'set_attributes' whenever tracing is off | Check if span is not None: first |
Calling get_current_active_span() outside any traced function | It returns None: no span is recording there | Call it inside a function that has @mlflow.trace |
Using mlflow.log_metric(...) inside a traced function | MLflow's guide warns that it writes to an MLflow run, not to the span | Use set_attributes |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The Attributes tab says "No attributes found" | That span never got any attributes | Check that you opened the span that sets them |
| The function fails only with tracing turned off | A set_attributes call without the None check | Add the check |
FAQ
When do I use attributes= and when set_attributes?
Use attributes= for values you know when you write the code, like a carrier name or a model name. Use set_attributes for values that depend on the call, like the method picked for this parcel.
What happens if I set the same key twice?
The later value wins. The set_attributes reference says the method "is additive", and "If an attribute with the same key already exists, it will be overwritten."
Can the value be something other than text?
Yes. heavy was a Python True/False value and stayed one: the Attributes tab shows false, not the text "False".
Conclusion
@mlflow.trace(attributes={"carrier": "Parcelway"}) set a fixed attribute on every call, and mlflow.get_current_active_span().set_attributes(...) added method and heavy from inside the function. Checking the span for None kept the function working with tracing turned off.
Additional Resources
- Manual Tracing — attributes in the decorator and inside the function.
- mlflow.get_current_active_span — the span that's recording now.
- LiveSpan.set_attributes — adding attributes to a span.
- mlflow.tracing.disable — switching tracing off.
