trace.Span

An operation whose lifetime the code that starts it owns, instead of one function call's: for work that begins in one call and ends in another, such as a stream that is pulled step by step, or an agent run spanning many model turns. It is a span like a traced function call's, nested under the span active where it starts (or `parent`).

Reference version

Signature

class trace.Span

An operation whose lifetime the code that starts it owns, instead of one function call's: for work that begins in one call and ends in another, such as a stream that is pulled step by step, or an agent run spanning many model turns. It is a span like a traced function call's, nested under the span active where it starts (or parent).

Start it with trace.start_span, run each piece of its work inside it with run, and end it once with finish, fail, fail_with_context or close. Inputs are captured at creation. The first ending wins; close makes no claim about the outcome.

Source:<builtin>/trace/trace.bamlbytes 6092–7615

Fields

_handle

$rust_type

Instance methods

function

_activate

(self) -> null throws trace.SpanError

(internal) Make this operation the active span for the rest of the calling frame, which run scopes to its body.

function

close

(self) -> null throws never

End without asserting success, failure, or cancellation.

function

fail

<E>(self, error: E) -> null throws never

Record an error value and end the operation. Does not throw that value.

function

fail_with_context

<E>(self, error: E, context: baml.errors.Context) -> null throws never

Record an error with its existing baml.errors.Context and end the operation.

function

finish

<T>(self, value: T) -> null throws never

Record a successful output and end the operation.

function

is_closed

(self) -> bool throws never
function

run

<T, E>(self, body: () -> T throws E) -> T throws E | trace.SpanError

Execute with this operation as the parent, using its context without completing it.