Skip to main content

Spans

What is a Span?

The Span object is a fundamental building block in the Trace data model. It is a container for the information about the individual steps of the trace, such as LLM calls, tool execution, retrieval, etc. Spans form a hierarchical tree structure within a single trace, which represents the execution flow of the trace.

Example of a Span for a Tool Calling Agent

Example of a Span for a Tool Calling Agent

For example, the above picture illustrates a set of spans that are organized in a tree structure in a trace. Each line represents a span, where the tree-structure is formed by the curly edges between lines.

Span Object Schema

MLflow's Span object is designed to be compatible with the OpenTelemetry Span spec. It is a dataclass object that is mostly same as the OpenTelemetry span object, but with some additional convenience accessors and methods to support GenAI use cases. When exported to OpenTelemetry-compatible backend, the Span object is serialized into the strict OpenTelemetry export format (OTLP).

FieldTypeDescription
span_idstrA unique identifier that is generated for each span within a trace.
trace_idstrThe unique identifier that links this span to its parent trace.
parent_idOptional[str]The identifier that establishes the hierarchical association of a given span with its parent span. If the span is the root span, this field is None.
namestrThe name of the span, either user-defined or automatically generated based on the function or method being instrumented.
start_time_nsintThe unix timestamp (in nanoseconds) when the span was started.
end_time_nsintThe unix timestamp (in nanoseconds) when the span was ended.
statusSpanStatusThe status of a span with values of OK, UNSET, or ERROR. The span status object contains an optional description if the status_code is reflecting an error that occurred.
inputsOptional[Any]The input data that is passed into the particular stage of your application.
outputsOptional[Any]The output data that is passed out of the particular stage of your application.
attributesDict[str, Any]Attributes are metadata that are associated with a given step within your application. These are key-value pairs that provide insight into behavioral modifications for function and method calls.
eventsList[SpanEvent]Events are a system-level property that is optionally applied to a span only if there was an issue during the execution of the span. These events contain information about exceptions that were thrown in the instrumented call, as well as the stack trace.

Span Attributes

Span attributes are key-value pairs that provide insight into behavioral modifications for function and method calls.

span.set_attributes(
{
"ai.model.name": "o3-mini",
"ai.model.version": "2024-01-01",
"ai.model.provider": "openai",
"ai.model.temperature": 0.7,
"ai.model.max_tokens": 1000,
"infrastructure.gpu.type": "A100",
"infrastructure.memory.used_mb": 2048,
}
)

Span Types

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.

Span TypeDescription
"CHAT_MODEL"Represents a query to a chat model. This is a special case of an LLM interaction.
"CHAIN"Represents a chain of operations.
"AGENT"Represents an autonomous agent operation.
"TOOL"Represents a tool execution (typically by an agent), such as querying a search engine.
"EMBEDDING"Represents a text embedding operation.
"RETRIEVER"Represents a context retrieval operation, such as querying a vector database.
"PARSER"Represents a parsing operation, transforming text into a structured format.
"RERANKER"Represents a re-ranking operation, ordering the retrieved contexts based on relevance.
"UNKNOWN"A default span type that is used when no other span type is specified.

Specialized Span Schemas

MLflow has predefined types of spans, and certain span types have properties that are required in order to enable additional functionality within the UI and downstream tasks such as evaluation.

Retriever Spans

The RETRIEVER span type is used for operations involving retrieving data from a data store (for example, querying documents from a vector store). The output of a RETRIEVER span is expected to be a list of documents.

Each document in the list should be a dictionary with the following structure:

page_content (str): The text content of the retrieved document chunk.

metadata (Optional[Dict[str, Any]]): A dictionary of additional metadata associated with the document. MLflow UI and evaluation metrics may specifically look for:

  • doc_uri (str): A string URI for the document source
  • chunk_id (str): A string identifier if the document is part of a larger chunked document

id (Optional[str]): An optional unique identifier for the document chunk itself.

Example Usage

import mlflow
from mlflow.entities import SpanType, Document


def search_store(query: str) -> list[tuple[str, str]]:
# Simulate retrieving documents (e.g., from a vector database)
return [
(
"MLflow Tracing helps debug GenAI applications...",
"docs/mlflow/tracing_intro.md",
),
(
"Key components of a trace include spans...",
"docs/mlflow/tracing_datamodel.md",
),
("MLflow provides automatic instrumentation...", "docs/mlflow/auto_trace.md"),
]


@mlflow.trace(span_type=SpanType.RETRIEVER)
def retrieve_relevant_documents(query: str):
# Get documents from the search store
docs = search_store(query)

# Get the current active span (created by @mlflow.trace)
span = mlflow.get_current_active_span()

# Set the outputs of the span in accordance with the tracing schema
outputs = [
Document(page_content=doc, metadata={"doc_uri": uri}) for doc, uri in docs
]
span.set_outputs(outputs)

# Return the original format for downstream usage
return docs


# Example usage
user_query = "MLflow Tracing benefits"
retrieved_docs = retrieve_relevant_documents(user_query)