strands.tools.tools
¶
Core tool implementations.
This module provides the base classes for all tool implementations in the SDK, including function-based tools and Python module-based tools, as well as utilities for validating tool uses and normalizing tool schemas.
ToolGenerator = AsyncGenerator[Any, None]
module-attribute
¶
Generator of tool events with the last being the tool result.
_COMPOSITION_KEYWORDS = ('anyOf', 'oneOf', 'allOf', 'not')
module-attribute
¶
JSON Schema composition keywords that define type constraints.
Properties with these should not get a default type: "string" added.
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 | |
InvalidToolUseNameException
¶
Bases: Exception
Exception raised when a tool use has an invalid name.
Source code in strands/tools/tools.py
27 28 29 30 | |
PythonAgentTool
¶
Bases: AgentTool
Tool implementation for Python-based tools.
This class handles tools implemented as Python functions, providing a simple interface for executing Python code as SDK tools.
Source code in strands/tools/tools.py
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 | |
supports_hot_reload
property
¶
Check if this tool supports automatic reloading when modified.
Returns:
| Type | Description |
|---|---|
bool
|
Always true for function-based tools. |
tool_name
property
¶
Get the name of the tool.
Returns:
| Type | Description |
|---|---|
str
|
The name of the tool. |
tool_spec
property
¶
Get the tool specification for this Python-based tool.
Returns:
| Type | Description |
|---|---|
ToolSpec
|
The tool specification. |
tool_type
property
¶
Identifies this as a Python-based tool implementation.
Returns:
| Type | Description |
|---|---|
str
|
"python". |
__init__(tool_name, tool_spec, tool_func)
¶
Initialize a Python-based tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Unique identifier for the tool. |
required |
tool_spec
|
ToolSpec
|
Tool specification defining parameters and behavior. |
required |
tool_func
|
ToolFunc
|
Python function to execute when the tool is invoked. |
required |
Source code in strands/tools/tools.py
168 169 170 171 172 173 174 175 176 177 178 179 180 | |
stream(tool_use, invocation_state, **kwargs)
async
¶
Stream the Python function with the given tool use request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request. |
required |
invocation_state
|
dict[str, Any]
|
Context for the tool invocation, including agent state. |
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/tools/tools.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
ToolFunc
¶
Bases: Protocol
Function signature for Python decorated and module based tools.
Source code in strands/types/tools.py
196 197 198 199 200 201 202 203 204 205 206 207 | |
__call__(*args, **kwargs)
¶
Function signature for Python decorated and module based tools.
Returns:
| Type | Description |
|---|---|
ToolResult | Awaitable[ToolResult]
|
Tool result or awaitable tool result. |
Source code in strands/types/tools.py
201 202 203 204 205 206 207 | |
ToolResultEvent
¶
Bases: TypedEvent
Event emitted when a tool execution completes.
Source code in strands/types/_events.py
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 | |
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
279 280 281 282 283 284 285 | |
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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
ToolUse
¶
Bases: TypedDict
A request from the model to use a specific tool with the provided input.
Attributes:
| Name | Type | Description |
|---|---|---|
input |
Any
|
The input parameters for the tool. Can be any JSON-serializable type. |
name |
str
|
The name of the tool to invoke. |
toolUseId |
str
|
A unique identifier for this specific tool use request. |
Source code in strands/types/tools.py
53 54 55 56 57 58 59 60 61 62 63 64 65 | |
_normalize_property(prop_name, prop_def)
¶
Normalize a single property definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_name
|
str
|
The name of the property. |
required |
prop_def
|
Any
|
The property definition to normalize. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The normalized property definition. |
Source code in strands/tools/tools.py
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 | |
normalize_schema(schema)
¶
Normalize a JSON schema to match expectations.
This function recursively processes nested objects to preserve the complete schema structure. Uses a copy-then-normalize approach to preserve all original schema properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
The schema to normalize. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The normalized schema. |
Source code in strands/tools/tools.py
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 | |
normalize_tool_spec(tool_spec)
¶
Normalize a complete tool specification by transforming its inputSchema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_spec
|
ToolSpec
|
The tool specification to normalize. |
required |
Returns:
| Type | Description |
|---|---|
ToolSpec
|
The normalized tool specification. |
Source code in strands/tools/tools.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
validate_tool_use(tool)
¶
Validate a tool use request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolUse
|
The tool use to validate. |
required |
Source code in strands/tools/tools.py
33 34 35 36 37 38 39 | |
validate_tool_use_name(tool)
¶
Validate the name of a tool use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolUse
|
The tool use to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidToolUseNameException
|
If the tool name is invalid. |
Source code in strands/tools/tools.py
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 | |