strands.experimental.bidi.models.model
¶
Bidirectional streaming model interface.
Defines the abstract interface for models that support real-time bidirectional communication with persistent connections. Unlike traditional request-response models, bidirectional models maintain an open connection for streaming audio, text, and tool interactions.
Features:
- Persistent connection management with connect/close lifecycle
- Real-time bidirectional communication (send and receive simultaneously)
- Provider-agnostic event normalization
- Support for audio, text, image, and tool result streaming
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.
Messages = List[Message]
module-attribute
¶
A list of messages representing a conversation.
logger = logging.getLogger(__name__)
module-attribute
¶
BidiModel
¶
Bases: Protocol
Protocol for bidirectional streaming models.
This interface defines the contract for models that support persistent streaming connections with real-time audio and text communication. Implementations handle provider-specific protocols while exposing a standardized event-based API.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
dict[str, Any]
|
Configuration dictionary with provider-specific settings. |
Source code in strands/experimental/bidi/models/model.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 | |
receive()
¶
Receive streaming events from the model.
Continuously yields events from the model as they arrive over the connection. Events are normalized to a provider-agnostic format for uniform processing. This method should be called in a loop or async task to process model responses.
The stream continues until the connection is closed or an error occurs.
Yields:
| Name | Type | Description |
|---|---|---|
BidiOutputEvent |
AsyncIterable[BidiOutputEvent]
|
Standardized event objects containing audio output, transcripts, tool calls, or control signals. |
Source code in strands/experimental/bidi/models/model.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
send(content)
async
¶
Send content to the model over the active connection.
Transmits user input or tool results to the model during an active streaming session. Supports multiple content types including text, audio, images, and tool execution results. Can be called multiple times during a conversation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
BidiInputEvent | ToolResultEvent
|
The content to send. Must be one of:
|
required |
Example
await model.send(BidiTextInputEvent(text="Hello", role="user"))
await model.send(BidiAudioInputEvent(audio=bytes, format="pcm", sample_rate=16000, channels=1))
await model.send(BidiImageInputEvent(image=bytes, mime_type="image/jpeg", encoding="raw"))
await model.send(ToolResultEvent(tool_result))
Source code in strands/experimental/bidi/models/model.py
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 | |
start(system_prompt=None, tools=None, messages=None, **kwargs)
async
¶
Establish a persistent streaming connection with the model.
Opens a bidirectional connection that remains active for real-time communication. The connection supports concurrent sending and receiving of events until explicitly closed. Must be called before any send() or receive() operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str | None
|
System instructions to configure model behavior. |
None
|
tools
|
list[ToolSpec] | None
|
Tool specifications that the model can invoke during the conversation. |
None
|
messages
|
Messages | None
|
Initial conversation history to provide context. |
None
|
**kwargs
|
Any
|
Provider-specific configuration options. |
{}
|
Source code in strands/experimental/bidi/models/model.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
stop()
async
¶
Close the streaming connection and release resources.
Terminates the active bidirectional connection and cleans up any associated resources such as network connections, buffers, or background tasks. After calling close(), the model instance cannot be used until start() is called again.
Source code in strands/experimental/bidi/models/model.py
64 65 66 67 68 69 70 71 | |
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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
__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
125 126 127 128 129 130 131 132 133 134 | |
ToolResultEvent
¶
Bases: TypedEvent
Event emitted when a tool execution completes.
Source code in strands/types/_events.py
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 | |
tool_result
property
¶
Final result from the completed tool execution.
tool_use_id
property
¶
The toolUseId associated with this result.
__init__(tool_result)
¶
Initialize with the completed tool result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_result
|
ToolResult
|
Final result from the tool execution |
required |
Source code in strands/types/_events.py
278 279 280 281 282 283 284 | |
ToolSpec
¶
Bases: TypedDict
Specification for a tool that can be used by an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
description |
str
|
A human-readable description of what the tool does. |
inputSchema |
JSONSchema
|
JSON Schema defining the expected input parameters. |
name |
str
|
The unique name of the tool. |
outputSchema |
NotRequired[JSONSchema]
|
Optional JSON Schema defining the expected output format. Note: Not all model providers support this field. Providers that don't support it should filter it out before sending to their API. |
Source code in strands/types/tools.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |