interaction() function wraps your AI functions with OpenTelemetry tracing to track interactions within a Gentrace pipeline. It creates spans for function execution, records arguments and outputs, and automatically manages OpenTelemetry baggage to ensure proper sampling and tracing context.
Basic usage
GentraceSpanProcessor when you call init(), which simplifies the process of converting baggage values to span attributes, ensuring proper trace routing to Gentrace. For manual configuration details, see the OpenTelemetry setup guide.
Overview
An interaction in Gentrace represents a single AI function call or operation within your pipeline. Theinteraction() function:
- Creates OpenTelemetry spans for function execution with detailed tracing
- Records function arguments and outputs as span events for debugging
- Manages OpenTelemetry baggage by setting
gentrace.sample="true"for proper sampling - Associates with pipelines by adding the
gentrace.pipeline_idattribute - Handles errors gracefully by recording exceptions and setting span status
Parameters
Additional attributes
You can add custom attributes to the OpenTelemetry span using theattributes option.
OpenTelemetry integration
Theinteraction() function provides deep integration with OpenTelemetry:
Span creation and attributes
- Span Name: Uses the provided name parameter
- Pipeline ID: Automatically adds
gentrace.pipeline_idattribute - Custom Attributes: Merges any additional attributes you provide
- Function Metadata: Records function name and execution context
Baggage management
The function automatically manages OpenTelemetry baggage:- All nested spans are properly sampled
- Gentrace can identify and process the traces
- Context is preserved across async boundaries
Event recording
Function arguments and outputs are recorded as span events:- Arguments Event:
gentrace.fn.argswith serialized function arguments - Output Event:
gentrace.fn.outputwith serialized return value - Exception Events: Automatic exception recording with stack traces
Error handling
Theinteraction() function handles errors gracefully and automatically associates all errors and exceptions with the OpenTelemetry span:
OTEL span error integration
When errors occur within interactions:- Automatic Capture: All
Errorobjects (TypeScript) and exceptions (Python) are automatically captured as span events - Stack Traces: Full stack traces are preserved in the span for debugging
- Error Attributes: Error messages, types, and metadata are recorded as span attributes
- Span Status: The span status is automatically set to
ERRORwhen unhandled exceptions occur
Usage in experiments
Theinteraction() function works seamlessly with Gentrace experiments:
Requirements
Must call
init() with a valid
API key. The SDK automatically configures OpenTelemetry for you. For
custom OpenTelemetry setups, see the manual setup
guide.When provided, must be a valid UUID for an existing Gentrace
pipeline. If omitted, uses the default pipeline
Works with both synchronous and asynchronous functions
Related functions
init()- Initialize the Gentrace SDKtraced()- Lower-level function tracing without pipeline associationexperiment()- Create testing contexts for grouping related evaluationseval()/evalOnce()- Run individual test cases within experimentsevalDataset()/eval_dataset()- Run tests against a dataset