strands.experimental.bidi.types.events
¶
Bidirectional streaming types for real-time audio/text conversations.
Type definitions for bidirectional streaming that extends Strands' existing streaming capabilities with real-time audio and persistent connection support.
Key features:
- Audio input/output events with standardized formats
- Interruption detection and handling
- Connection lifecycle management
- Provider-agnostic event types
- Type-safe discriminated unions with TypedEvent
- JSON-serializable events (audio/images stored as base64 strings)
Audio format normalization:
- Supports PCM, WAV, Opus, and MP3 formats
- Standardizes sample rates (16kHz, 24kHz, 48kHz)
- Normalizes channel configurations (mono/stereo)
- Abstracts provider-specific encodings
- Audio data stored as base64-encoded strings for JSON compatibility
AudioChannel = Literal[1, 2]
module-attribute
¶
Number of audio channels.
- Mono: 1
- Stereo: 2
AudioFormat = Literal['pcm', 'wav', 'opus', 'mp3']
module-attribute
¶
Audio encoding format.
AudioSampleRate = Literal[16000, 24000, 48000]
module-attribute
¶
Audio sample rate in Hz.
BidiInputEvent = BidiTextInputEvent | BidiAudioInputEvent | BidiImageInputEvent
module-attribute
¶
Union of different bidi input event types.
BidiOutputEvent = BidiConnectionStartEvent | BidiConnectionRestartEvent | BidiResponseStartEvent | BidiAudioStreamEvent | BidiTranscriptStreamEvent | BidiInterruptionEvent | BidiResponseCompleteEvent | BidiUsageEvent | BidiConnectionCloseEvent | BidiErrorEvent | ToolUseStreamEvent
module-attribute
¶
Union of different bidi output event types.
Role = Literal['user', 'assistant']
module-attribute
¶
Role of a message sender.
- "user": Messages from the user to the assistant.
- "assistant": Messages from the assistant to the user.
StopReason = Literal['complete', 'error', 'interrupted', 'tool_use']
module-attribute
¶
Reason for the model ending its response generation.
- "complete": Model completed its response.
- "error": Model encountered an error.
- "interrupted": Model was interrupted by the user.
- "tool_use": Model is requesting a tool use.
BidiAudioInputEvent
¶
Bases: TypedEvent
Audio input event for sending audio to the model.
Used for sending audio data through the send() method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
str
|
Base64-encoded audio string to send to model. |
required |
format
|
AudioFormat | str
|
Audio format from SUPPORTED_AUDIO_FORMATS. |
required |
sample_rate
|
AudioSampleRate
|
Sample rate from SUPPORTED_SAMPLE_RATES. |
required |
channels
|
AudioChannel
|
Channel count from SUPPORTED_CHANNELS. |
required |
Source code in strands/experimental/bidi/types/events.py
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 | |
audio
property
¶
Base64-encoded audio string.
channels
property
¶
Number of audio channels (1=mono, 2=stereo).
format
property
¶
Audio encoding format.
sample_rate
property
¶
Number of audio samples per second in Hz.
__init__(audio, format, sample_rate, channels)
¶
Initialize audio input event.
Source code in strands/experimental/bidi/types/events.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
BidiAudioStreamEvent
¶
Bases: TypedEvent
Streaming audio output from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
str
|
Base64-encoded audio string. |
required |
format
|
AudioFormat
|
Audio encoding format. |
required |
sample_rate
|
AudioSampleRate
|
Number of audio samples per second in Hz. |
required |
channels
|
AudioChannel
|
Number of audio channels (1=mono, 2=stereo). |
required |
Source code in strands/experimental/bidi/types/events.py
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 | |
audio
property
¶
Base64-encoded audio string.
channels
property
¶
Number of audio channels (1=mono, 2=stereo).
format
property
¶
Audio encoding format.
sample_rate
property
¶
Number of audio samples per second in Hz.
__init__(audio, format, sample_rate, channels)
¶
Initialize audio stream event.
Source code in strands/experimental/bidi/types/events.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
BidiConnectionCloseEvent
¶
Bases: TypedEvent
Streaming connection closed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id
|
str
|
Unique identifier for this streaming connection (matches BidiConnectionStartEvent). |
required |
reason
|
Literal['client_disconnect', 'timeout', 'error', 'complete', 'user_request']
|
Why the connection was closed. |
required |
Source code in strands/experimental/bidi/types/events.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
connection_id
property
¶
Unique identifier for this streaming connection.
reason
property
¶
Why the interruption occurred.
__init__(connection_id, reason)
¶
Initialize connection close event.
Source code in strands/experimental/bidi/types/events.py
510 511 512 513 514 515 516 517 518 519 520 521 522 | |
BidiConnectionRestartEvent
¶
Bases: TypedEvent
Agent is restarting the model connection after timeout.
Source code in strands/experimental/bidi/types/events.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
timeout_error
property
¶
Model timeout error.
__init__(timeout_error)
¶
Initialize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout_error
|
BidiModelTimeoutError
|
Timeout error reported by the model. |
required |
Source code in strands/experimental/bidi/types/events.py
218 219 220 221 222 223 224 225 226 227 228 229 | |
BidiConnectionStartEvent
¶
Bases: TypedEvent
Streaming connection established and ready for interaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id
|
str
|
Unique identifier for this streaming connection. |
required |
model
|
str
|
Model identifier (e.g., "gpt-realtime", "gemini-2.0-flash-live"). |
required |
Source code in strands/experimental/bidi/types/events.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
connection_id
property
¶
Unique identifier for this streaming connection.
model
property
¶
Model identifier (e.g., 'gpt-realtime', 'gemini-2.0-flash-live').
__init__(connection_id, model)
¶
Initialize connection start event.
Source code in strands/experimental/bidi/types/events.py
194 195 196 197 198 199 200 201 202 | |
BidiErrorEvent
¶
Bases: TypedEvent
Error occurred during the session.
Stores the full Exception object as an instance attribute for debugging while
keeping the event dict JSON-serializable. The exception can be accessed via
the error property for re-raising or type-based error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The exception that occurred. |
required |
details
|
dict[str, Any] | None
|
Optional additional error information. |
None
|
Source code in strands/experimental/bidi/types/events.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
code
property
¶
Error code derived from exception class name.
details
property
¶
Additional error context beyond the exception itself.
error
property
¶
The original exception that occurred.
Can be used for re-raising or type-based error handling.
message
property
¶
Human-readable error message from the exception.
__init__(error, details=None)
¶
Initialize error event.
Source code in strands/experimental/bidi/types/events.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | |
BidiImageInputEvent
¶
Bases: TypedEvent
Image input event for sending images/video frames to the model.
Used for sending image data through the send() method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
str
|
Base64-encoded image string. |
required |
mime_type
|
str
|
MIME type (e.g., "image/jpeg", "image/png"). |
required |
Source code in strands/experimental/bidi/types/events.py
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 174 175 176 177 178 | |
image
property
¶
Base64-encoded image string.
mime_type
property
¶
MIME type of the image (e.g., "image/jpeg", "image/png").
__init__(image, mime_type)
¶
Initialize image input event.
Source code in strands/experimental/bidi/types/events.py
156 157 158 159 160 161 162 163 164 165 166 167 168 | |
BidiInterruptionEvent
¶
Bases: TypedEvent
Model generation was interrupted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
Literal['user_speech', 'error']
|
Why the interruption occurred. |
required |
Source code in strands/experimental/bidi/types/events.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
reason
property
¶
Why the interruption occurred.
__init__(reason)
¶
Initialize interruption event.
Source code in strands/experimental/bidi/types/events.py
370 371 372 373 374 375 376 377 | |
BidiModelTimeoutError
¶
Bases: Exception
Model timeout error.
Bidirectional models are often configured with a connection time limit. Nova sonic for example keeps the connection open for 8 minutes max. Upon receiving a timeout, the agent loop is configured to restart the model connection so as to create a seamless, uninterrupted experience for the user.
Source code in strands/experimental/bidi/models/model.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
__init__(message, **restart_config)
¶
Initialize error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Timeout message from model. |
required |
**restart_config
|
Any
|
Configure restart specific behaviors in the call to model start. |
{}
|
Source code in strands/experimental/bidi/models/model.py
126 127 128 129 130 131 132 133 134 135 | |
BidiResponseCompleteEvent
¶
Bases: TypedEvent
Model finished generating response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_id
|
str
|
ID of the response that completed (matches response.start). |
required |
stop_reason
|
StopReason
|
Why the response ended. |
required |
Source code in strands/experimental/bidi/types/events.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
response_id
property
¶
Unique identifier for this response.
stop_reason
property
¶
Why the response ended.
__init__(response_id, stop_reason)
¶
Initialize response complete event.
Source code in strands/experimental/bidi/types/events.py
393 394 395 396 397 398 399 400 401 402 403 404 405 | |
BidiResponseStartEvent
¶
Bases: TypedEvent
Model starts generating a response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_id
|
str
|
Unique identifier for this response (used in response.complete). |
required |
Source code in strands/experimental/bidi/types/events.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
response_id
property
¶
Unique identifier for this response.
__init__(response_id)
¶
Initialize response start event.
Source code in strands/experimental/bidi/types/events.py
244 245 246 | |
BidiTextInputEvent
¶
Bases: TypedEvent
Text input event for sending text to the model.
Used for sending text content through the send() method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text content to send to the model. |
required |
role
|
Role
|
The role of the message sender (default: "user"). |
'user'
|
Source code in strands/experimental/bidi/types/events.py
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 | |
role
property
¶
The role of the message sender.
text
property
¶
The text content to send to the model.
__init__(text, role='user')
¶
Initialize text input event.
Source code in strands/experimental/bidi/types/events.py
74 75 76 77 78 79 80 81 82 | |
BidiTranscriptStreamEvent
¶
Bases: ModelStreamEvent
Audio transcription streaming (user or assistant speech).
Supports incremental transcript updates for providers that send partial transcripts before the final version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta
|
ContentBlockDelta
|
The incremental transcript change (ContentBlockDelta). |
required |
text
|
str
|
The delta text (same as delta content for convenience). |
required |
role
|
Role
|
Who is speaking ("user" or "assistant"). |
required |
is_final
|
bool
|
Whether this is the final/complete transcript. |
required |
current_transcript
|
str | None
|
The accumulated transcript text so far (None for first delta). |
None
|
Source code in strands/experimental/bidi/types/events.py
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 353 354 355 356 357 358 359 360 | |
current_transcript
property
¶
The accumulated transcript text so far.
delta
property
¶
The incremental transcript change.
is_final
property
¶
Whether this is the final/complete transcript.
role
property
¶
The role of the message sender.
text
property
¶
The text content to send to the model.
__init__(delta, text, role, is_final, current_transcript=None)
¶
Initialize transcript stream event.
Source code in strands/experimental/bidi/types/events.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
BidiUsageEvent
¶
Bases: TypedEvent
Token usage event with modality breakdown for bidirectional streaming.
Tracks token consumption across different modalities (audio, text, images) during bidirectional streaming sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_tokens
|
int
|
Total tokens used for all input modalities. |
required |
output_tokens
|
int
|
Total tokens used for all output modalities. |
required |
total_tokens
|
int
|
Sum of input and output tokens. |
required |
modality_details
|
list[ModalityUsage] | None
|
Optional list of token usage per modality. |
None
|
cache_read_input_tokens
|
int | None
|
Optional tokens read from cache. |
None
|
cache_write_input_tokens
|
int | None
|
Optional tokens written to cache. |
None
|
Source code in strands/experimental/bidi/types/events.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
cache_read_input_tokens
property
¶
Optional tokens read from cache.
cache_write_input_tokens
property
¶
Optional tokens written to cache.
input_tokens
property
¶
Total tokens used for all input modalities.
modality_details
property
¶
Optional list of token usage per modality.
output_tokens
property
¶
Total tokens used for all output modalities.
total_tokens
property
¶
Sum of input and output tokens.
__init__(input_tokens, output_tokens, total_tokens, modality_details=None, cache_read_input_tokens=None, cache_write_input_tokens=None)
¶
Initialize usage event.
Source code in strands/experimental/bidi/types/events.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
ContentBlockDelta
¶
Bases: TypedDict
A block of content in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
reasoningContent |
ReasoningContentBlockDelta
|
Contains content regarding the reasoning that is carried out by the model. |
text |
str
|
Text fragment being streamed. |
toolUse |
ContentBlockDeltaToolUse
|
Tool use input fragment being streamed. |
Source code in strands/types/streaming.py
108 109 110 111 112 113 114 115 116 117 118 119 120 | |
ModalityUsage
¶
Bases: dict
Token usage for a specific modality.
Attributes:
| Name | Type | Description |
|---|---|---|
modality |
Literal['text', 'audio', 'image', 'cached']
|
Type of content. |
input_tokens |
int
|
Tokens used for this modality's input. |
output_tokens |
int
|
Tokens used for this modality's output. |
Source code in strands/experimental/bidi/types/events.py
418 419 420 421 422 423 424 425 426 427 428 429 | |
ModelStreamEvent
¶
Bases: TypedEvent
Event emitted during model response streaming.
This event is fired when the model produces streaming output during response generation.
Source code in strands/types/_events.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
__init__(delta_data)
¶
Initialize with streaming delta data from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta_data
|
dict[str, Any]
|
Incremental streaming data from the model response |
required |
Source code in strands/types/_events.py
125 126 127 128 129 130 131 | |
ToolUseStreamEvent
¶
Bases: ModelStreamEvent
Event emitted during tool use input streaming.
Source code in strands/types/_events.py
144 145 146 147 148 149 | |
__init__(delta, current_tool_use)
¶
Initialize with delta and current tool use state.
Source code in strands/types/_events.py
147 148 149 | |
TypedEvent
¶
Bases: dict
Base class for all typed events in the agent system.
Source code in strands/types/_events.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
is_callback_event
property
¶
True if this event should trigger the callback_handler to fire.
__init__(data=None)
¶
Initialize the typed event with optional data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | None
|
Optional dictionary of event data to initialize with |
None
|
Source code in strands/types/_events.py
30 31 32 33 34 35 36 | |
as_dict()
¶
Convert this event to a raw dictionary for emitting purposes.
Source code in strands/types/_events.py
43 44 45 | |
prepare(invocation_state)
¶
Prepare the event for emission by adding invocation state.
This allows a subset of events to merge with the invocation_state without needing to pass around the invocation_state throughout the system.
Source code in strands/types/_events.py
47 48 49 50 51 52 53 | |