Prerequisites

Please complete the Quickstart and make sure you set up custom tracing using the @trace Python decorator or traceFunction JavaScript method. Here’s the guide for custom spans in Python and custom spans in JavaScript.

Session & Span Level Enrichments

Custom Spans allow you to trace any function in your code and see its inputs, outputs, errors, duration, etc. However, for certain use cases, you might want to add additional context into your trace such as feedback or metrics. We call this context injection enrichments.

You can enrich both at the root session level and individual event spans:

  • To log additional context to your root session, you can use the enrich_session() function.
  • To log additional context to a specific child event span, you can use the enrich_span() function. This is useful if you want to add context within a traced function during runtime.

Enrichment Schema

Enrichments are key-value pairs that you can add to your traces to provide additional context. You can add the following enrichment attributes to your traces:

Attribute KeyValue TypeDescriptionSession-levelSpan-level
configObjectConfiguration details
feedbackObjectUser feedback or annotations
metricObjectMetrics, scores, or evaluations
metadataObjectCatch-all for arbitrary metadata or JSON
outputsObjectOutput data from the function or event
user_propertiesObjectUser-specific attributes
event_typeStringType of event (chain, model, tool)
inputsObjectInput data for the function or event
errorStringError information if applicable

Note: For the Object type enrichments (config, metadata, etc.), enrichments are always additive for unique keys. This means that enriching the same field twice with different keys will result in both keys being included in the trace. However, if you enrich the same field with the same key twice, the value will be overwritten.

Examples

Here are some ways in which you can enrich your traces: