Skip to main content

API Reference

Complete reference for all observability APIs in the Basalt Python SDK.

Core Decorators and Context Managers

start_observe

Creates a root span to start a new trace. Every trace must have exactly one root span. As Decorator:
As Context Manager:
Parameters: Identity Structure:
Experiment Structure:
Examples:

observe

Creates a child span within an existing trace. Requires a parent span from start_observe. If no parent span exists, silently creates a no-op span (no errors, just no telemetry). As Decorator:
As Context Manager:
Parameters: ObserveKind Values:
Examples:

async_start_observe / async_observe

Explicit async variants of start_observe and observe. Also works with auto-detection using @start_observe and @observe on async functions. Signatures: Same as sync versions Examples:

Span Handle Methods

When using context managers, you get a span handle with these methods:

Data Methods

set_input(data)

Set the input data for the span.
Example:

set_output(data)

Set the output data for the span.
Example:

set_attribute(key, value)

Set a single attribute on the span.
Example:

set_metadata(metadata)

Set multiple attributes at once.
Example:

Identity Methods

set_identity(identity)

Set user and organization identity.
Structure:
Example:

set_user(user_id, name=None)

Set user identity only.
Example:

set_organization(org_id, name=None)

Set organization identity only.
Example:

Status and Error Methods

set_status(status, message=None)

Set the span status.
Status values: "ok", "error", "unset" Example:

record_exception(exception)

Record an exception with full traceback.
Example:

Event Methods

add_event(name, attributes=None)

Add a timestamped event to the span.
Example:

Evaluator Methods

add_evaluator(slug, metadata=None)

Add a single evaluator to this span only (non-propagating).
Example:

add_evaluators(*slugs)

Add multiple evaluators at once (non-propagating).
Example:

set_evaluator_config(config)

Set evaluation configuration (e.g., sample rate).
Example:

set_evaluator_metadata(metadata)

Set metadata for evaluators to use.
Example:

LLM-Specific Methods

For spans with kind=ObserveKind.GENERATION:

set_model(model)

Set the LLM model name.
Example:

set_prompt(prompt)

Set the prompt text.
Example:

set_completion(completion)

Set the LLM completion/response text.
Example:

set_tokens(input=None, output=None, total=None)

Set token counts.
Example:

Static Methods on observe Class

For use within decorator-traced functions (when you don’t have span handle access):

observe.metadata(metadata)

Set metadata on the current active span.
Example:

observe.update_metadata(metadata)

Update existing metadata on the current active span.
Example:

observe.set_input(data)

Set input on the current active span.
Example:

observe.set_output(data)

Set output on the current active span.
Example:

observe.set_identity(identity)

Set identity on the current active span.
Example:

observe.evaluate(slug)

Add evaluator to the current active span.
Example:

Evaluator Decorators and Context Managers

@evaluator

Attach evaluators in propagating mode (affects all child spans).
Parameters: Example:

with_evaluators

Context manager for propagating evaluators.
Example:

attach_evaluator

Context manager for attaching a single propagating evaluator.
Example:

Global Configuration

configure_trace_defaults

Set global defaults for all traces.
Parameters: Example:

current_trace_defaults

Get current global trace defaults.
Returns: Dictionary with experiment, metadata, and evaluators keys. Example:

Trace Context Helpers

set_trace_user

Set user identity in current context (propagates to all spans).
Example:

set_trace_organization

Set organization identity in current context (propagates to all spans).
Example:

Data Classes

EvaluationConfig

Configuration for evaluator sampling and behavior.
Example:

TraceExperiment

Experiment information for A/B testing.
Example:

Auto-Instrumentation

Basalt Initialization

Enable auto-instrumentation when initializing Basalt.
Example:

TelemetryConfig

Advanced telemetry configuration.

Context Keys (Advanced)

For advanced use cases, these context keys are available:
These are used internally for context propagation and are rarely needed in application code.

See Also