Sometimes you have some context prior to running a function and you want to label it at creation time. For example, you may want to tag a span with the model provider endpoint that you use, or the test dataset that you used to run the request.

1. Create a label class

First, you need to create a label class. This is required to define the label name and possible values. Label classes are created in the Laminar UI.

If you do not create a label class, the labels sent from code will not be saved.

2. Label spans from code

This functionality is available from v0.4.35.

Example 1. Labeling a span created with automatic instrumentation

from lmnr import Laminar

with Laminar.with_labels({"my_label": "my_value", "another_label": "value"}):
    openai_client.chat.completions.create(...)

Note that any spans created within the with_labels block will inherit the labels, including auto-instrumented spans or any calls to observed. functions.

Example 2. Labeling a span created manually

with Laminar.start_as_current_span(name="foo", labels={"my_label": "value"}):
    # your code here
    pass

Note that only the span created by this Laminar.start_as_current_span call will have the labels.

Every label must have a string key and a string value. They must exactly match the name and possible values of the label class.

3. Viewing labels

The labels will be visible in the Laminar UI, in the “Labels” view of the span.

The only difference for these labels is that they will have CODE as their source.