How to Set the Span Type of a Traced Function in MLflow
When MLflow traces a function, it doesn't know what the function is for: a tool an agent calls, a model call, a search. The span type tells it. This guide gives one traced function the type TOOL and leaves another without a type, so you can see the difference in the MLflow UI, with real output from MLflow 3.18.0.
Syntax
import mlflow
from mlflow.entities import SpanType
@mlflow.trace(span_type=SpanType.<TYPE>)
def <function>(<parameters>):
...
| Part | What it means |
|---|---|
SpanType | MLflow's list of built-in span types, imported from mlflow.entities |
<TYPE> | One of the built-in types, such as TOOL, AGENT, LLM or RETRIEVER |
span_type= | The decorator parameter that sets the type. Without it, the type is UNKNOWN |
MLflow's span concepts page: "Span types are a way to categorize spans within a trace. MLflow provides a set of predefined span types for common use cases, while also allowing you to set custom span types." In MLflow 3.18.0, SpanType holds AGENT, CHAIN, CHAT_MODEL, EMBEDDING, EVALUATOR, GUARDRAIL, LLM, MEMORY, PARSER, RERANKER, RETRIEVER, TASK, TOOL, UNKNOWN and WORKFLOW.
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 has two small functions: to_celsius converts a temperature, the kind of helper an AI agent could call as a tool, and describe turns a temperature into a word.
import mlflow
from mlflow.entities import SpanType
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("span-types")
@mlflow.trace(span_type=SpanType.TOOL)
def to_celsius(fahrenheit: float) -> float:
return round((fahrenheit - 32) * 5 / 9, 1)
@mlflow.trace
def describe(celsius: float) -> str:
return "warm" if celsius >= 20 else "cool"
print(to_celsius(68.0))
print(describe(20.0))

from mlflow.entities import SpanTypebrings in the list of types.@mlflow.trace(span_type=SpanType.TOOL)recordsto_celsiusas a tool.@mlflow.traceondescribesets no type, so MLflow uses the default,UNKNOWN.
Step 4: Run It
uv run span_types.py
2026/12/03 08:15:31 INFO mlflow.tracking.fluent: Experiment with name 'span-types' does not exist. Creating a new experiment.
20.0
warm
2026/12/03 08:15:31 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

68 °F is 20.0 °C, and 20 °C counts as warm. Each call became a trace.
Step 5: Compare the Two Spans
Open http://127.0.0.1:8080, click span-types, then Traces:

Click the row with "fahrenheit": 68.0:

The to_celsius span has a wrench icon: that's how the trace view shows a TOOL span. Go back and open the row with "celsius": 20.0:

The describe span has a question mark: its type is UNKNOWN. Both spans record their inputs and outputs in the same way; the type only says what kind of step each one is. In a trace with many spans, such as an agent that calls several tools, the icons let you pick out each kind of step at a glance.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using SpanType without importing it | NameError: name 'SpanType' is not defined | Add from mlflow.entities import SpanType |
| Leaving the type out on an agent's tools | Every span shows a question mark, so tools are hard to tell from other steps | Give each tool function span_type=SpanType.TOOL |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A span shows a question-mark icon | No span_type was given, so it's UNKNOWN | Add span_type= to its decorator |
FAQ
Can I make up my own span type?
Yes. MLflow's manual tracing guide says to "set either one of built-in Span Types or a string". A built-in type is the better choice when one fits, because MLflow knows what it means.
Does the type change what MLflow records?
For TOOL and UNKNOWN, no: both spans above kept their inputs and outputs the same way. Some types come with expectations, though. MLflow's span concepts page explains that a RETRIEVER span's output is expected to be a list of documents.
Which type should an agent's main function have?
AGENT, which MLflow describes as "an autonomous agent operation". Its tool functions are TOOL spans inside it.
Conclusion
@mlflow.trace(span_type=SpanType.TOOL) recorded to_celsius as a tool, with a wrench icon in the trace view, while describe, traced without a type, got UNKNOWN and a question mark.
Additional Resources
- Span concepts — span types and what each one means.
- mlflow.entities.SpanType — the built-in types.
- mlflow.trace — the
span_typeparameter.
