strands.experimental.bidi.types
¶
Type definitions for bidirectional streaming.
strands.experimental.bidi.types.agent
¶
Agent-related type definitions for bidirectional streaming.
This module defines the types used for BidiAgent.
strands.experimental.bidi.types.model
¶
Model-related type definitions for bidirectional streaming.
Defines types and configurations that are central to model providers, including audio configuration that models use to specify their audio processing requirements.
AudioConfig
¶
Bases: TypedDict
Audio configuration for bidirectional streaming models.
Defines standard audio parameters that model providers use to specify their audio processing requirements. All fields are optional to support models that may not use audio or only need specific parameters.
Model providers build this configuration by merging user-provided values with their own defaults. The resulting configuration is then used by audio I/O implementations to configure hardware appropriately.
Attributes:
| Name | Type | Description |
|---|---|---|
input_rate |
AudioSampleRate
|
Input sample rate in Hz (e.g., 16000, 24000, 48000) |
output_rate |
AudioSampleRate
|
Output sample rate in Hz (e.g., 16000, 24000, 48000) |
channels |
AudioChannel
|
Number of audio channels (1=mono, 2=stereo) |
format |
AudioFormat
|
Audio encoding format |
voice |
str
|
Voice identifier for text-to-speech (e.g., "alloy", "matthew") |
Source code in strands/experimental/bidi/types/model.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
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 | |
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 | |
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 | |
strands.experimental.bidi.types.io
¶
Protocol for bidirectional streaming IO channels.
Defines callable protocols for input and output channels that can be used with BidiAgent. This approach provides better typing and flexibility by separating input and output concerns into independent callables.
BidiInput
¶
Bases: Protocol
Protocol for bidirectional input callables.
Input callables read data from a source (microphone, camera, websocket, etc.) and return events to be sent to the agent.
Source code in strands/experimental/bidi/types/io.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
__call__()
¶
Read input data from the source.
Returns:
| Type | Description |
|---|---|
Awaitable[BidiInputEvent]
|
Awaitable that resolves to an input event (audio, text, image, etc.) |
Source code in strands/experimental/bidi/types/io.py
32 33 34 35 36 37 38 | |
start(agent)
async
¶
Start input.
Source code in strands/experimental/bidi/types/io.py
24 25 26 | |
stop()
async
¶
Stop input.
Source code in strands/experimental/bidi/types/io.py
28 29 30 | |
BidiOutput
¶
Bases: Protocol
Protocol for bidirectional output callables.
Output callables receive events from the agent and handle them appropriately (play audio, display text, send over websocket, etc.).
Source code in strands/experimental/bidi/types/io.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
__call__(event)
¶
Process output events from the agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BidiOutputEvent
|
Output event from the agent (audio, text, tool calls, etc.) |
required |
Source code in strands/experimental/bidi/types/io.py
57 58 59 60 61 62 63 | |
start(agent)
async
¶
Start output.
Source code in strands/experimental/bidi/types/io.py
49 50 51 | |
stop()
async
¶
Stop output.
Source code in strands/experimental/bidi/types/io.py
53 54 55 | |