Span

Contents

Span#

A span is a building block of the trace. It typically represents the input and output of a given phase of the pipeline. For example, it can contain the inputs and ouputs of the LLM call.

Mesages#

The typical output of the LLM-based systems is a sequence of messages. MLFlow uses a special UI representation if the data has been prepared in a format compatible with OpenAI.


The following cell illustrates the correct format for publishing the sequence in the mlflow span.

with mlflow.start_span(
    name="my_span",
    span_type=SpanType.LLM
) as span:
    span.set_inputs([
        {"role": "system", "content": "System prompt"},
        {"role": "user", "content": "Hello how are you"},
    ])
    span.set_outputs([
        {"role": "assistant", "content": "Answer of the model"},
        {"role": "tool", "content": "The tool call"}
    ])

Now check the UI of the MLFlow server. Each message will be presented according to its type.

Nested#

Some spans can be nested within other spans.


The following code shows how an inner_span can be placed inside an outer_span.

with mlflow.start_span(
    name="outer_span",
) as outer_span:
    outer_span.set_inputs("data of outer span")
    with mlflow.start_span(
        name="innder_span"
    ) as inner_span:
        inner_span.set_inputs("data of inner span")

Only the outer span would be represented in the list of traces. Go to the Details & Timeline section of the outer span find more about the inner span.