Spans
Span
Bases:ABC,Generic[TSpanData]
Base class for representing traceable operations with timing and context.
A span represents a single operation within a trace (e.g., an LLM call, tool execution,or agent run). Spans track timing, relationships between operations, and operation-specificdata.
Example
# Creating a custom spanwithcustom_span("database_query",{"operation":"SELECT","table":"users"})asspan:results=awaitdb.query("SELECT * FROM users")span.set_output({"count":len(results)})# Handling errors in spanswithcustom_span("risky_operation")asspan:try:result=perform_risky_operation()exceptExceptionase:span.set_error({"message":str(e),"data":{"operation":"risky_operation"}})raiseNotes:- Spans automatically nest under the current trace- Use context managers for reliable start/finish- Include relevant data but avoid sensitive information- Handle errors properly using set_error()
Source code insrc/agents/tracing/spans.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | |
trace_idabstractmethodproperty
The ID of the trace this span belongs to.
Returns:
| Name | Type | Description |
|---|---|---|
str | str | Unique identifier of the parent trace. |
span_idabstractmethodproperty
Unique identifier for this span.
Returns:
| Name | Type | Description |
|---|---|---|
str | str | The span's unique ID within its trace. |
span_dataabstractmethodproperty
Operation-specific data for this span.
Returns:
| Name | Type | Description |
|---|---|---|
TSpanData | TSpanData | Data specific to this type of span (e.g., LLM generation data). |
parent_idabstractmethodproperty
ID of the parent span, if any.
Returns:
| Type | Description |
|---|---|
str | None | str | None: The parent span's ID, or None if this is a root span. |
errorabstractmethodproperty
error:SpanError|NoneAny error that occurred during span execution.
Returns:
| Type | Description |
|---|---|
SpanError | None | SpanError | None: Error details if an error occurred, None otherwise. |
started_atabstractmethodproperty
When the span started execution.
Returns:
| Type | Description |
|---|---|
str | None | str | None: ISO format timestamp of span start, None if not started. |
ended_atabstractmethodproperty
When the span finished execution.
Returns:
| Type | Description |
|---|---|
str | None | str | None: ISO format timestamp of span end, None if not finished. |
startabstractmethod
Start the span.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mark_as_current | bool | If true, the span will be marked as the current span. | False |
finishabstractmethod
Finish the span.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reset_current | bool | If true, the span will be reset as the current span. | False |
NoOpSpan
Bases:Span[TSpanData]
A no-op implementation of Span that doesn't record any data.
Used when tracing is disabled but span operations still need to work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span_data | TSpanData | The operation-specific data for this span. | required |
Source code insrc/agents/tracing/spans.py
SpanImpl
Bases:Span[TSpanData]
Source code insrc/agents/tracing/spans.py
247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 | |