strands.experimental.bidi.agent.agent
¶
Bidirectional Agent for real-time streaming conversations.
Provides real-time audio and text interaction through persistent streaming connections. Unlike traditional request-response patterns, this agent maintains long-running conversations where users can interrupt, provide additional input, and receive continuous responses including audio output.
Key capabilities:
- Persistent conversation connections with concurrent processing
- Real-time audio input/output streaming
- Automatic interruption detection and tool execution
- Event-driven communication with model providers
AgentState = JSONSerializableDict
module-attribute
¶
BidiAgentInput = str | BidiTextInputEvent | BidiAudioInputEvent | BidiImageInputEvent
module-attribute
¶
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.
_DEFAULT_AGENT_ID = 'default'
module-attribute
¶
_DEFAULT_AGENT_NAME = 'Strands Agents'
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
AgentTool
¶
Bases: ABC
Abstract base class for all SDK tools.
This class defines the interface that all tool implementations must follow. Each tool must provide its name, specification, and implement a stream method that executes the tool's functionality.
Source code in strands/types/tools.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 | |
is_dynamic
property
¶
Whether the tool was dynamically loaded during runtime.
Dynamic tools may have different lifecycle management.
Returns:
| Type | Description |
|---|---|
bool
|
True if loaded dynamically, False otherwise. |
supports_hot_reload
property
¶
Whether the tool supports automatic reloading when modified.
Returns:
| Type | Description |
|---|---|
bool
|
False by default. |
tool_name
abstractmethod
property
¶
The unique name of the tool used for identification and invocation.
tool_spec
abstractmethod
property
¶
Tool specification that describes its functionality and parameters.
tool_type
abstractmethod
property
¶
The type of the tool implementation (e.g., 'python', 'javascript', 'lambda').
Used for categorization and appropriate handling.
__init__()
¶
Initialize the base agent tool with default dynamic state.
Source code in strands/types/tools.py
219 220 221 | |
get_display_properties()
¶
Get properties to display in UI representations of this tool.
Subclasses can extend this to include additional properties.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of property names and their string values. |
Source code in strands/types/tools.py
287 288 289 290 291 292 293 294 295 296 297 298 | |
mark_dynamic()
¶
Mark this tool as dynamically loaded.
Source code in strands/types/tools.py
283 284 285 | |
stream(tool_use, invocation_state, **kwargs)
abstractmethod
¶
Stream tool events and return the final result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request containing tool ID and parameters. |
required |
invocation_state
|
dict[str, Any]
|
Caller-provided kwargs that were passed to the agent when it was invoked (agent(), agent.invoke_async(), etc.). |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Yields:
| Type | Description |
|---|---|
ToolGenerator
|
Tool events with the last being the tool result. |
Source code in strands/types/tools.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
BidiAgent
¶
Agent for bidirectional streaming conversations.
Enables real-time audio and text interaction with AI models through persistent connections. Supports concurrent tool execution and interruption handling.
Source code in strands/experimental/bidi/agent/agent.py
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 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 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 | |
tool
property
¶
Call tool as a function.
Returns:
| Type | Description |
|---|---|
_ToolCaller
|
ToolCaller for method-style tool execution. |
Example
agent = BidiAgent(model=model, tools=[calculator])
agent.tool.calculator(expression="2+2")
tool_names
property
¶
Get a list of all registered tool names.
Returns:
| Type | Description |
|---|---|
list[str]
|
Names of all tools available to this agent. |
__aenter__(invocation_state=None)
async
¶
Async context manager entry point.
Automatically starts the bidirectional connection when entering the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
invocation_state
|
dict[str, Any] | None
|
Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter. |
None
|
Returns:
| Type | Description |
|---|---|
BidiAgent
|
Self for use in the context. |
Source code in strands/experimental/bidi/agent/agent.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
__aexit__(*_)
async
¶
Async context manager exit point.
Automatically ends the connection and cleans up resources including when exiting the context, regardless of whether an exception occurred.
Source code in strands/experimental/bidi/agent/agent.py
323 324 325 326 327 328 329 330 | |
__init__(model=None, tools=None, system_prompt=None, messages=None, record_direct_tool_call=True, load_tools_from_directory=False, agent_id=None, name=None, description=None, hooks=None, state=None, session_manager=None, tool_executor=None, **kwargs)
¶
Initialize bidirectional agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BidiModel | str | None
|
BidiModel instance, string model_id, or None for default detection. |
None
|
tools
|
list[str | AgentTool | ToolProvider] | None
|
Optional list of tools with flexible format support. |
None
|
system_prompt
|
str | None
|
Optional system prompt for conversations. |
None
|
messages
|
Messages | None
|
Optional conversation history to initialize with. |
None
|
record_direct_tool_call
|
bool
|
Whether to record direct tool calls in message history. |
True
|
load_tools_from_directory
|
bool
|
Whether to load and automatically reload tools in the |
False
|
agent_id
|
str | None
|
Optional ID for the agent, useful for connection management and multi-agent scenarios. |
None
|
name
|
str | None
|
Name of the Agent. |
None
|
description
|
str | None
|
Description of what the Agent does. |
None
|
hooks
|
list[HookProvider] | None
|
Optional list of hook providers to register for lifecycle events. |
None
|
state
|
AgentState | dict | None
|
Stateful information for the agent. Can be either an AgentState object, or a json serializable dict. |
None
|
session_manager
|
SessionManager | None
|
Manager for handling agent sessions including conversation history and state. If provided, enables session-based persistence and state management. |
None
|
tool_executor
|
ToolExecutor | None
|
Definition of tool execution strategy (e.g., sequential, concurrent, etc.). |
None
|
**kwargs
|
Any
|
Additional configuration for future extensibility. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If model configuration is invalid or state is invalid type. |
TypeError
|
If model type is unsupported. |
Source code in strands/experimental/bidi/agent/agent.py
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 | |
receive()
async
¶
Receive events from the model including audio, text, and tool calls.
Yields:
| Type | Description |
|---|---|
AsyncGenerator[BidiOutputEvent, None]
|
Model output events processed by background tasks including audio output, |
AsyncGenerator[BidiOutputEvent, None]
|
text responses, tool calls, and connection updates. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If start has not been called. |
Source code in strands/experimental/bidi/agent/agent.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
run(inputs, outputs, invocation_state=None)
async
¶
Run the agent using provided IO channels for bidirectional communication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
list[BidiInput]
|
Input callables to read data from a source |
required |
outputs
|
list[BidiOutput]
|
Output callables to receive events from the agent |
required |
invocation_state
|
dict[str, Any] | None
|
Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter. |
None
|
Example
# Using model defaults:
model = BidiNovaSonicModel()
audio_io = BidiAudioIO()
text_io = BidiTextIO()
agent = BidiAgent(model=model, tools=[calculator])
await agent.run(
inputs=[audio_io.input()],
outputs=[audio_io.output(), text_io.output()],
invocation_state={"user_id": "user_123"}
)
# Using custom audio config:
model = BidiNovaSonicModel(
provider_config={"audio": {"input_rate": 48000, "output_rate": 24000}}
)
audio_io = BidiAudioIO()
agent = BidiAgent(model=model, tools=[calculator])
await agent.run(
inputs=[audio_io.input()],
outputs=[audio_io.output()],
)
Source code in strands/experimental/bidi/agent/agent.py
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
send(input_data)
async
¶
Send input to the model (text, audio, image, or event dict).
Unified method for sending text, audio, and image input to the model during an active conversation session. Accepts TypedEvent instances or plain dicts (e.g., from WebSocket clients) which are automatically reconstructed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
BidiAgentInput | dict[str, Any]
|
Can be:
|
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If start has not been called. |
ValueError
|
If invalid input type. |
Example
await agent.send("Hello") await agent.send(BidiAudioInputEvent(audio="base64...", format="pcm", ...)) await agent.send({"type": "bidirectional_text_input", "text": "Hello", "role": "user"})
Source code in strands/experimental/bidi/agent/agent.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 | |
start(invocation_state=None)
async
¶
Start a persistent bidirectional conversation connection.
Initializes the streaming connection and starts background tasks for processing model events, tool execution, and connection management.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
invocation_state
|
dict[str, Any] | None
|
Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If agent already started. |
Example
await agent.start(invocation_state={
"user_id": "user_123",
"session_id": "session_456",
"database": db_connection,
})
Source code in strands/experimental/bidi/agent/agent.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
stop()
async
¶
End the conversation connection and cleanup all resources.
Terminates the streaming connection, cancels background tasks, and closes the connection to the model provider.
Source code in strands/experimental/bidi/agent/agent.py
297 298 299 300 301 302 303 304 | |
BidiAgentInitializedEvent
dataclass
¶
Bases: BidiHookEvent
Event triggered when a BidiAgent has finished initialization.
This event is fired after the BidiAgent has been fully constructed and all built-in components have been initialized. Hook providers can use this event to perform setup tasks that require a fully initialized agent.
Source code in strands/experimental/hooks/events.py
54 55 56 57 58 59 60 61 62 63 | |
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 | |
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 | |
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 | |
BidiMessageAddedEvent
dataclass
¶
Bases: BidiHookEvent
Event triggered when BidiAgent adds a message to the conversation.
This event is fired whenever the BidiAgent adds a new message to its internal message history, including user messages (from transcripts), assistant responses, and tool results. Hook providers can use this event for logging, monitoring, or implementing custom message processing logic.
Note: This event is only triggered for messages added by the framework itself, not for messages manually added by tools or external code.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Message
|
The message that was added to the conversation history. |
Source code in strands/experimental/hooks/events.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
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 115 | |
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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
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
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 | |
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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
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
65 66 67 68 69 70 71 72 | |
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 | |
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 | |
ConcurrentToolExecutor
¶
Bases: ToolExecutor
Concurrent tool executor.
Source code in strands/tools/executors/concurrent.py
19 20 21 22 23 24 25 26 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 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 | |
HookProvider
¶
Bases: Protocol
Protocol for objects that provide hook callbacks to an agent.
Hook providers offer a composable way to extend agent functionality by subscribing to various events in the agent lifecycle. This protocol enables building reusable components that can hook into agent events.
Example
class MyHookProvider(HookProvider):
def register_hooks(self, registry: HookRegistry) -> None:
registry.add_callback(StartRequestEvent, self.on_request_start)
registry.add_callback(EndRequestEvent, self.on_request_end)
agent = Agent(hooks=[MyHookProvider()])
Source code in strands/hooks/registry.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 | |
register_hooks(registry, **kwargs)
¶
Register callback functions for specific event types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
HookRegistry
|
The hook registry to register callbacks with. |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/hooks/registry.py
107 108 109 110 111 112 113 114 | |
HookRegistry
¶
Registry for managing hook callbacks associated with event types.
The HookRegistry maintains a mapping of event types to callback functions and provides methods for registering callbacks and invoking them when events occur.
The registry handles callback ordering, including reverse ordering for cleanup events, and provides type-safe event dispatching.
Source code in strands/hooks/registry.py
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 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 | |
__init__()
¶
Initialize an empty hook registry.
Source code in strands/hooks/registry.py
156 157 158 | |
add_callback(event_type, callback)
¶
Register a callback function for a specific event type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
type[TEvent]
|
The class type of events this callback should handle. |
required |
callback
|
HookCallback[TEvent]
|
The callback function to invoke when events of this type occur. |
required |
Example
def my_handler(event: StartRequestEvent):
print("Request started")
registry.add_callback(StartRequestEvent, my_handler)
Source code in strands/hooks/registry.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
add_hook(hook)
¶
Register all callbacks from a hook provider.
This method allows bulk registration of callbacks by delegating to the hook provider's register_hooks method. This is the preferred way to register multiple related callbacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook
|
HookProvider
|
The hook provider containing callbacks to register. |
required |
Example
class MyHooks(HookProvider):
def register_hooks(self, registry: HookRegistry):
registry.add_callback(StartRequestEvent, self.on_start)
registry.add_callback(EndRequestEvent, self.on_end)
registry.add_hook(MyHooks())
Source code in strands/hooks/registry.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
get_callbacks_for(event)
¶
Get callbacks registered for the given event in the appropriate order.
This method returns callbacks in registration order for normal events, or reverse registration order for events that have should_reverse_callbacks=True. This enables proper cleanup ordering for teardown events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
TEvent
|
The event to get callbacks for. |
required |
Yields:
| Type | Description |
|---|---|
HookCallback[TEvent]
|
Callback functions registered for this event type, in the appropriate order. |
Example
event = EndRequestEvent(agent=my_agent)
for callback in registry.get_callbacks_for(event):
callback(event)
Source code in strands/hooks/registry.py
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 | |
has_callbacks()
¶
Check if the registry has any registered callbacks.
Returns:
| Type | Description |
|---|---|
bool
|
True if there are any registered callbacks, False otherwise. |
Example
if registry.has_callbacks():
print("Registry has callbacks registered")
Source code in strands/hooks/registry.py
297 298 299 300 301 302 303 304 305 306 307 308 309 | |
invoke_callbacks(event)
¶
Invoke all registered callbacks for the given event.
This method finds all callbacks registered for the event's type and invokes them in the appropriate order. For events with should_reverse_callbacks=True, callbacks are invoked in reverse registration order. Any exceptions raised by callback functions will propagate to the caller.
Additionally, this method aggregates interrupts raised by the user to instantiate human-in-the-loop workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
TInvokeEvent
|
The event to dispatch to registered callbacks. |
required |
Returns:
| Type | Description |
|---|---|
tuple[TInvokeEvent, list[Interrupt]]
|
The event dispatched to registered callbacks and any interrupts raised by the user. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If at least one callback is async. |
ValueError
|
If interrupt name is used more than once. |
Example
event = StartRequestEvent(agent=my_agent)
registry.invoke_callbacks(event)
Source code in strands/hooks/registry.py
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 | |
invoke_callbacks_async(event)
async
¶
Invoke all registered callbacks for the given event.
This method finds all callbacks registered for the event's type and invokes them in the appropriate order. For events with should_reverse_callbacks=True, callbacks are invoked in reverse registration order. Any exceptions raised by callback functions will propagate to the caller.
Additionally, this method aggregates interrupts raised by the user to instantiate human-in-the-loop workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
TInvokeEvent
|
The event to dispatch to registered callbacks. |
required |
Returns:
| Type | Description |
|---|---|
tuple[TInvokeEvent, list[Interrupt]]
|
The event dispatched to registered callbacks and any interrupts raised by the user. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If interrupt name is used more than once. |
Example
event = StartRequestEvent(agent=my_agent)
await registry.invoke_callbacks_async(event)
Source code in strands/hooks/registry.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
Message
¶
Bases: TypedDict
A message in a conversation with the agent.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
list[ContentBlock]
|
The message content. |
role |
Role
|
The role of the message sender. |
Source code in strands/types/content.py
178 179 180 181 182 183 184 185 186 187 | |
SessionManager
¶
Bases: HookProvider, ABC
Abstract interface for managing sessions.
A session manager is in charge of persisting the conversation and state of an agent across its interaction. Changes made to the agents conversation, state, or other attributes should be persisted immediately after they are changed. The different methods introduced in this class are called at important lifecycle events for an agent, and should be persisted in the session.
Source code in strands/session/session_manager.py
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 | |
append_bidi_message(message, agent, **kwargs)
¶
Append a message to the bidirectional agent's session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
Message to add to the agent in the session |
required |
agent
|
BidiAgent
|
BidiAgent to append the message to |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
145 146 147 148 149 150 151 152 153 154 155 156 157 | |
append_message(message, agent, **kwargs)
abstractmethod
¶
Append a message to the agent's session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
Message to add to the agent in the session |
required |
agent
|
Agent
|
Agent to append the message to |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
74 75 76 77 78 79 80 81 82 | |
initialize(agent, **kwargs)
abstractmethod
¶
Initialize an agent with a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent to initialize |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
93 94 95 96 97 98 99 100 | |
initialize_bidi_agent(agent, **kwargs)
¶
Initialize a bidirectional agent with a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
BidiAgent
|
BidiAgent to initialize |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
132 133 134 135 136 137 138 139 140 141 142 143 | |
initialize_multi_agent(source, **kwargs)
¶
Read multi-agent state from persistent storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
source
|
MultiAgentBase
|
Multi-agent state to initialize. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Multi-agent state dictionary or empty dict if not found. |
Source code in strands/session/session_manager.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
redact_latest_message(redact_message, agent, **kwargs)
abstractmethod
¶
Redact the message most recently appended to the agent in the session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redact_message
|
Message
|
New message to use that contains the redact content |
required |
agent
|
Agent
|
Agent to apply the message redaction to |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
64 65 66 67 68 69 70 71 72 | |
register_hooks(registry, **kwargs)
¶
Register hooks for persisting the agent to the session.
Source code in strands/session/session_manager.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
sync_agent(agent, **kwargs)
abstractmethod
¶
Serialize and sync the agent with the session storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent who should be synchronized with the session storage |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
84 85 86 87 88 89 90 91 | |
sync_bidi_agent(agent, **kwargs)
¶
Serialize and sync the bidirectional agent with the session storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
BidiAgent
|
BidiAgent who should be synchronized with the session storage |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
159 160 161 162 163 164 165 166 167 168 169 170 | |
sync_multi_agent(source, **kwargs)
¶
Serialize and sync multi-agent with the session storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
MultiAgentBase
|
Multi-agent source object to persist |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Source code in strands/session/session_manager.py
102 103 104 105 106 107 108 109 110 111 112 113 | |
ToolExecutor
¶
Bases: ABC
Abstract base class for tool executors.
Source code in strands/tools/executors/_executor.py
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 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 353 354 355 356 357 358 | |
ToolProvider
¶
Bases: ABC
Interface for providing tools with lifecycle management.
Provides a way to load a collection of tools and clean them up when done, with lifecycle managed by the agent.
Source code in strands/tools/tool_provider.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 | |
add_consumer(consumer_id, **kwargs)
abstractmethod
¶
Add a consumer to this tool provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumer_id
|
Any
|
Unique identifier for the consumer. |
required |
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Source code in strands/tools/tool_provider.py
30 31 32 33 34 35 36 37 38 | |
load_tools(**kwargs)
abstractmethod
async
¶
Load and return the tools in this provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
Sequence[AgentTool]
|
List of tools that are ready to use. |
Source code in strands/tools/tool_provider.py
18 19 20 21 22 23 24 25 26 27 28 | |
remove_consumer(consumer_id, **kwargs)
abstractmethod
¶
Remove a consumer from this tool provider.
This method must be idempotent - calling it multiple times with the same ID should have no additional effect after the first call.
Provider may clean up resources when no consumers remain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumer_id
|
Any
|
Unique identifier for the consumer. |
required |
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Source code in strands/tools/tool_provider.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
ToolRegistry
¶
Central registry for all tools available to the agent.
This class manages tool registration, validation, discovery, and invocation.
Source code in strands/tools/registry.py
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 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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 500 501 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 533 534 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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | |
NewToolDict
¶
Bases: TypedDict
Dictionary type for adding or updating a tool in the configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
spec |
ToolSpec
|
The tool specification that defines the tool's interface and behavior. |
Source code in strands/tools/registry.py
640 641 642 643 644 645 646 647 | |
__init__()
¶
Initialize the tool registry.
Source code in strands/tools/registry.py
37 38 39 40 41 42 43 | |
cleanup(**kwargs)
¶
Synchronously clean up all tool providers in this registry.
Source code in strands/tools/registry.py
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | |
discover_tool_modules()
¶
Discover available tool modules in all tools directories.
Returns:
| Type | Description |
|---|---|
dict[str, Path]
|
Dictionary mapping tool names to their full paths. |
Source code in strands/tools/registry.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
get_all_tool_specs()
¶
Get all the tool specs for all tools in this registry..
Returns:
| Type | Description |
|---|---|
list[ToolSpec]
|
A list of ToolSpecs. |
Source code in strands/tools/registry.py
565 566 567 568 569 570 571 572 573 | |
get_all_tools_config()
¶
Dynamically generate tool configuration by combining built-in and dynamic tools.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing all tool configurations. |
Source code in strands/tools/registry.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
get_tools_dirs()
¶
Get all tool directory paths.
Returns:
| Type | Description |
|---|---|
list[Path]
|
A list of Path objects for current working directory's "./tools/". |
Source code in strands/tools/registry.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
initialize_tools(load_tools_from_directory=False)
¶
Initialize all tools by discovering and loading them dynamically from all tool directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_tools_from_directory
|
bool
|
Whether to reload tools if changes are made at runtime. |
False
|
Source code in strands/tools/registry.py
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 500 501 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 533 534 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 | |
load_tool_from_filepath(tool_name, tool_path)
¶
DEPRECATED: Load a tool from a file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
tool_path
|
str
|
Path to the tool file. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the tool file is not found. |
ValueError
|
If the tool cannot be loaded. |
Source code in strands/tools/registry.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
process_tools(tools)
¶
Process tools list.
Process list of tools that can contain local file path string, module import path string, imported modules, @tool decorated functions, or instances of AgentTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tools
|
list[Any]
|
List of tool specifications. Can be:
|
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of tool names that were processed. |
Source code in strands/tools/registry.py
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 | |
register_dynamic_tool(tool)
¶
Register a tool dynamically for temporary use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
AgentTool
|
The tool to register dynamically |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a tool with this name already exists |
Source code in strands/tools/registry.py
575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
register_tool(tool)
¶
Register a tool function with the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
AgentTool
|
The tool to register. |
required |
Source code in strands/tools/registry.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 | |
reload_tool(tool_name)
¶
Reload a specific tool module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to reload. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the tool file cannot be found. |
ImportError
|
If there are issues importing the tool module. |
ValueError
|
If the tool specification is invalid or required components are missing. |
Exception
|
For other errors during tool reloading. |
Source code in strands/tools/registry.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
replace(new_tool)
¶
Replace an existing tool with a new implementation.
This performs a swap of the tool implementation in the registry. The replacement takes effect on the next agent invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_tool
|
AgentTool
|
New tool implementation. Its name must match the tool being replaced. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tool doesn't exist. |
Source code in strands/tools/registry.py
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 | |
validate_tool_spec(tool_spec)
¶
Validate tool specification against required schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_spec
|
ToolSpec
|
Tool specification to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the specification is invalid. |
Source code in strands/tools/registry.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
ToolWatcher
¶
Watches tool directories for changes and reloads tools when they are modified.
Source code in strands/tools/watcher.py
19 20 21 22 23 24 25 26 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 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 | |
MasterChangeHandler
¶
Bases: FileSystemEventHandler
Master handler that delegates to all registered handlers.
Source code in strands/tools/watcher.py
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 | |
__init__(dir_path)
¶
Initialize a master change handler for a specific directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
The directory path to watch. |
required |
Source code in strands/tools/watcher.py
72 73 74 75 76 77 78 | |
on_modified(event)
¶
Delegate file modification events to all registered handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Any
|
The file system event that triggered this handler. |
required |
Source code in strands/tools/watcher.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
ToolChangeHandler
¶
Bases: FileSystemEventHandler
Handler for tool file changes.
Source code in strands/tools/watcher.py
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 | |
__init__(tool_registry)
¶
Initialize a tool change handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_registry
|
ToolRegistry
|
The tool registry to update when tools change. |
required |
Source code in strands/tools/watcher.py
44 45 46 47 48 49 50 | |
on_modified(event)
¶
Reload tool if file modification detected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Any
|
The file system event that triggered this handler. |
required |
Source code in strands/tools/watcher.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
__init__(tool_registry)
¶
Initialize a tool watcher for the given tool registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_registry
|
ToolRegistry
|
The tool registry to report changes. |
required |
Source code in strands/tools/watcher.py
32 33 34 35 36 37 38 39 | |
start()
¶
Start watching all tools directories for changes.
Source code in strands/tools/watcher.py
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 | |
_BidiAgentLoop
¶
Agent loop.
Attributes:
| Name | Type | Description |
|---|---|---|
_agent |
BidiAgent instance to loop. |
|
_started |
Flag if agent loop has started. |
|
_task_pool |
Track active async tasks created in loop. |
|
_event_queue |
Queue
|
Queue model and tool call events for receiver. |
_invocation_state |
dict[str, Any]
|
Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter. |
_send_gate |
Gate the sending of events to the model. Blocks when agent is reseting the model connection after timeout. |
Source code in strands/experimental/bidi/agent/loop.py
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 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 | |
__init__(agent)
¶
Initialize members of the agent loop.
Note, before receiving events from the loop, the user must call start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
BidiAgent
|
Bidirectional agent to loop over. |
required |
Source code in strands/experimental/bidi/agent/loop.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
receive()
async
¶
Receive model and tool call events.
Returns:
| Type | Description |
|---|---|
AsyncGenerator[BidiOutputEvent, None]
|
Model and tool call events. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If start has not been called. |
Source code in strands/experimental/bidi/agent/loop.py
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 | |
send(event)
async
¶
Send model event.
Additionally, add text input to messages array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BidiInputEvent | ToolResultEvent
|
User input event or tool result. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If start has not been called. |
Source code in strands/experimental/bidi/agent/loop.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
start(invocation_state=None)
async
¶
Start the agent loop.
The agent model is started as part of this call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
invocation_state
|
dict[str, Any] | None
|
Optional context to pass to tools during execution. This allows passing custom data (user_id, session_id, database connections, etc.) that tools can access via their invocation_state parameter. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If loop already started. |
Source code in strands/experimental/bidi/agent/loop.py
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 | |
stop()
async
¶
Stop the agent loop.
Source code in strands/experimental/bidi/agent/loop.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
_InterruptState
dataclass
¶
Track the state of interrupt events raised by the user.
Note, interrupt state is cleared after resuming.
Attributes:
| Name | Type | Description |
|---|---|---|
interrupts |
dict[str, Interrupt]
|
Interrupts raised by the user. |
context |
dict[str, Any]
|
Additional context associated with an interrupt event. |
activated |
bool
|
True if agent is in an interrupt state, False otherwise. |
Source code in strands/interrupt.py
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 | |
activate()
¶
Activate the interrupt state.
Source code in strands/interrupt.py
56 57 58 | |
deactivate()
¶
Deacitvate the interrupt state.
Interrupts and context are cleared.
Source code in strands/interrupt.py
60 61 62 63 64 65 66 67 | |
from_dict(data)
classmethod
¶
Initiailize interrupt state from serialized interrupt state.
Interrupt state can be serialized with the to_dict method.
Source code in strands/interrupt.py
108 109 110 111 112 113 114 115 116 117 118 119 120 | |
resume(prompt)
¶
Configure the interrupt state if resuming from an interrupt event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
User responses if resuming from interrupt. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If in interrupt state but user did not provide responses. |
Source code in strands/interrupt.py
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 | |
to_dict()
¶
Serialize to dict for session management.
Source code in strands/interrupt.py
104 105 106 | |
_TaskGroup
¶
Shim of asyncio.TaskGroup for use in Python 3.10.
Attributes:
| Name | Type | Description |
|---|---|---|
_tasks |
set[Task]
|
Set of tasks in group. |
Source code in strands/experimental/bidi/_async/_task_group.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
__aenter__()
async
¶
Setup self managed task group context.
Source code in strands/experimental/bidi/_async/_task_group.py
31 32 33 34 | |
__aexit__(*_)
async
¶
Execute tasks in group.
The following execution rules are enforced: - The context stops executing all tasks if at least one task raises an Exception or the context is cancelled. - The context re-raises Exceptions to the caller. - The context re-raises CancelledErrors to the caller only if the context itself was cancelled.
Source code in strands/experimental/bidi/_async/_task_group.py
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 | |
create_task(coro)
¶
Create an async task and add to group.
Returns:
| Type | Description |
|---|---|
Task
|
The created task. |
Source code in strands/experimental/bidi/_async/_task_group.py
21 22 23 24 25 26 27 28 29 | |
_ToolCaller
¶
Call tool as a function.
Source code in strands/tools/_caller.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 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 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
__getattr__(name)
¶
Call tool as a function.
This method enables the method-style interface (e.g., agent.tool.tool_name(param="value")).
It matches underscore-separated names to hyphenated tool names (e.g., 'some_thing' matches 'some-thing').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute (tool) being accessed. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A function that when called will execute the named tool. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If no tool with the given name exists or if multiple tools match the given name. |
Source code in strands/tools/_caller.py
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 | |
__init__(agent)
¶
Initialize instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent | BidiAgent
|
Agent reference that will accept tool results. |
required |
Source code in strands/tools/_caller.py
30 31 32 33 34 35 36 37 38 | |
stop_all(*funcs)
async
¶
Call all stops in sequence and aggregate errors.
A failure in one stop call will not block subsequent stop calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
funcs
|
Callable[..., Awaitable[None]]
|
Stop functions to call in sequence. |
()
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If any stop function raises an exception. |
Source code in strands/experimental/bidi/_async/__init__.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |