Skip to main content

How to Add Attributes to a Span in MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

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>, ...})
...
PartWhat 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​

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​

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.

span_attributes.py
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))

The span_attributes.py script in a macOS-style code window: pick_shipping is decorated with mlflow.trace and a fixed carrier attribute of Parcelway, and inside it gets the current span and, if there is one, sets the method and heavy attributes.

  • attributes={"carrier": "Parcelway"} puts carrier on the span of every call.
  • mlflow.get_current_active_span() returns the span the decorator opened for this call.
  • span.set_attributes(...) adds method, a text value, and heavy, a True or False value, worked out from the weight.

Step 4: Run It​

Terminal
uv run span_attributes.py
Output
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...

The output in a terminal: MLflow creates the span-attributes experiment, the script prints express, and MLflow flushes its trace logging queue.

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:

The Attributes tab of the pick_shipping span: carrier is Parcelway, method is express and heavy is false.

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:

tracing_off.py
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))
Terminal
uv run tracing_off.py
Output
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​

MistakeWhat happensDo this instead
Calling span.set_attributes(...) without checking for NoneAttributeError: 'NoneType' object has no attribute 'set_attributes' whenever tracing is offCheck if span is not None: first
Calling get_current_active_span() outside any traced functionIt returns None: no span is recording thereCall it inside a function that has @mlflow.trace
Using mlflow.log_metric(...) inside a traced functionMLflow's guide warns that it writes to an MLflow run, not to the spanUse set_attributes

Troubleshooting​

SymptomLikely causeFix
The Attributes tab says "No attributes found"That span never got any attributesCheck that you opened the span that sets them
The function fails only with tracing turned offA set_attributes call without the None checkAdd 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​