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-specific data.
Example
# Creating a custom span
with custom_span("database_query", {
"operation": "SELECT",
"table": "users"
}) as span:
results = await db.query("SELECT * FROM users")
span.set_output({"count": len(results)})
# Handling errors in spans
with custom_span("risky_operation") as span:
try:
result = perform_risky_operation()
except Exception as e:
span.set_error({
"message": str(e),
"data": {"operation": "risky_operation"}
})
raise
Notes: - 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 in src/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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
trace_id
abstractmethod
property
The ID of the trace this span belongs to.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Unique identifier of the parent trace. |
span_id
abstractmethod
property
Unique identifier for this span.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The span's unique ID within its trace. |
span_data
abstractmethod
property
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_id
abstractmethod
property
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. |
error
abstractmethod
property
error: SpanError | None
Any error that occurred during span execution.
Returns:
Type | Description |
---|---|
SpanError | None
|
SpanError | None: Error details if an error occurred, None otherwise. |
started_at
abstractmethod
property
When the span started execution.
Returns:
Type | Description |
---|---|
str | None
|
str | None: ISO format timestamp of span start, None if not started. |
ended_at
abstractmethod
property
When the span finished execution.
Returns:
Type | Description |
---|---|
str | None
|
str | None: ISO format timestamp of span end, None if not finished. |
start
abstractmethod
Start the span.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mark_as_current
|
bool
|
If true, the span will be marked as the current span. |
False
|
finish
abstractmethod
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 in src/agents/tracing/spans.py
SpanImpl
Bases: Span[TSpanData]
Source code in src/agents/tracing/spans.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|