Skip to main content

How to Set the Span Type of a Traced Function in MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import mlflow
from mlflow.entities import SpanType


@mlflow.trace(span_type=SpanType.<TYPE>)
def <function>(<parameters>):
...
PartWhat it means
SpanTypeMLflow'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​

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 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.

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

The span_types.py script in a macOS-style code window: SpanType is imported from mlflow.entities, to_celsius is decorated with @mlflow.trace(span_type=SpanType.TOOL), describe with a plain @mlflow.trace, and both are called.

  • from mlflow.entities import SpanType brings in the list of types.
  • @mlflow.trace(span_type=SpanType.TOOL) records to_celsius as a tool.
  • @mlflow.trace on describe sets no type, so MLflow uses the default, UNKNOWN.

Step 4: Run It​

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

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

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:

The Traces page of the span-types experiment: one trace with the input celsius 20.0 and the output warm, and one with the input fahrenheit 68.0 and the output 20.0.

Click the row with "fahrenheit": 68.0:

The trace view of to_celsius: its span has a wrench icon, the tool icon, both in the span list and next to its name, with the input fahrenheit 68 and the output 20.

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 trace view of describe: its span has a question-mark icon, the icon of a span without a type, with the input celsius 20 and the output warm.

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​

MistakeWhat happensDo this instead
Using SpanType without importing itNameError: name 'SpanType' is not definedAdd from mlflow.entities import SpanType
Leaving the type out on an agent's toolsEvery span shows a question mark, so tools are hard to tell from other stepsGive each tool function span_type=SpanType.TOOL

Troubleshooting​

SymptomLikely causeFix
A span shows a question-mark iconNo span_type was given, so it's UNKNOWNAdd 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​