strands.tools.structured_output.structured_output_tool
¶
Structured output tool implementation.
This module provides a real tool implementation for structured output that integrates with the existing tool execution and error handling infrastructure.
ToolGenerator = AsyncGenerator[Any, None]
module-attribute
¶
Generator of tool events with the last being the tool result.
_TOOL_SPEC_CACHE = {}
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
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 | |
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
227 228 229 | |
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
295 296 297 298 299 300 301 302 303 304 305 306 | |
mark_dynamic()
¶
Mark this tool as dynamically loaded.
Source code in strands/types/tools.py
291 292 293 | |
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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
StructuredOutputContext
¶
Per-invocation context for structured output execution.
Source code in strands/tools/structured_output/_structured_output_context.py
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 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 | |
is_enabled
property
¶
Check if structured output is enabled for this context.
Returns:
| Type | Description |
|---|---|
bool
|
True if a structured output model is configured, False otherwise. |
__init__(structured_output_model=None)
¶
Initialize a new structured output context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structured_output_model
|
Type[BaseModel] | None
|
Optional Pydantic model type for structured output. |
None
|
Source code in strands/tools/structured_output/_structured_output_context.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
cleanup(registry)
¶
Clean up the registered structured output tool from the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ToolRegistry
|
The tool registry to clean up the tool from. |
required |
Source code in strands/tools/structured_output/_structured_output_context.py
135 136 137 138 139 140 141 142 143 | |
extract_result(tool_uses)
¶
Extract and remove structured output result from stored results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_uses
|
list[ToolUse]
|
List of tool use dictionaries from the current execution cycle. |
required |
Returns:
| Type | Description |
|---|---|
BaseModel | None
|
The structured output result if found, or None if no result available. |
Source code in strands/tools/structured_output/_structured_output_context.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
get_result(tool_use_id)
¶
Retrieve a stored structured output result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use_id
|
str
|
Unique identifier for the tool use. |
required |
Returns:
| Type | Description |
|---|---|
BaseModel | None
|
The validated Pydantic model instance, or None if not found. |
Source code in strands/tools/structured_output/_structured_output_context.py
57 58 59 60 61 62 63 64 65 66 | |
get_tool_spec()
¶
Get the tool specification for structured output.
Returns:
| Type | Description |
|---|---|
Optional[ToolSpec]
|
Tool specification, or None if no structured output model. |
Source code in strands/tools/structured_output/_structured_output_context.py
94 95 96 97 98 99 100 101 102 | |
has_structured_output_tool(tool_uses)
¶
Check if any tool uses are for the structured output tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_uses
|
list[ToolUse]
|
List of tool use dictionaries to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any tool use matches the expected structured output tool name, |
bool
|
False if no structured output tool is present or expected. |
Source code in strands/tools/structured_output/_structured_output_context.py
80 81 82 83 84 85 86 87 88 89 90 91 92 | |
register_tool(registry)
¶
Register the structured output tool with the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ToolRegistry
|
The tool registry to register the tool with. |
required |
Source code in strands/tools/structured_output/_structured_output_context.py
125 126 127 128 129 130 131 132 133 | |
set_forced_mode(tool_choice=None)
¶
Mark this context as being in forced structured output mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_choice
|
dict | None
|
Optional tool choice configuration. |
None
|
Source code in strands/tools/structured_output/_structured_output_context.py
68 69 70 71 72 73 74 75 76 77 78 | |
store_result(tool_use_id, result)
¶
Store a validated structured output result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use_id
|
str
|
Unique identifier for the tool use. |
required |
result
|
BaseModel
|
Validated Pydantic model instance. |
required |
Source code in strands/tools/structured_output/_structured_output_context.py
48 49 50 51 52 53 54 55 | |
StructuredOutputTool
¶
Bases: AgentTool
Tool implementation for structured output validation.
Source code in strands/tools/structured_output/structured_output_tool.py
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
structured_output_model
property
¶
Get the Pydantic model type for this tool.
Returns:
| Type | Description |
|---|---|
Type[BaseModel]
|
The Pydantic model class. |
tool_name
property
¶
Get the name of the tool.
Returns:
| Type | Description |
|---|---|
str
|
The name of the tool (same as the Pydantic model class name). |
tool_spec
property
¶
Get the tool specification for this structured output tool.
Returns:
| Type | Description |
|---|---|
ToolSpec
|
The tool specification generated from the Pydantic model. |
tool_type
property
¶
Identifies this as a structured output tool implementation.
Returns:
| Type | Description |
|---|---|
str
|
"structured_output". |
__init__(structured_output_model)
¶
Initialize a structured output tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structured_output_model
|
Type[BaseModel]
|
The Pydantic model class that defines the expected output structure. |
required |
Source code in strands/tools/structured_output/structured_output_tool.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
stream(tool_use, invocation_state, **kwargs)
async
¶
Validate the structured output and return appropriate result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request containing the data to validate. |
required |
invocation_state
|
dict[str, Any]
|
Context for the tool invocation (kept for compatibility). |
required |
**kwargs
|
Any
|
Additional keyword arguments, including structured_output_context. |
{}
|
Yields:
| Type | Description |
|---|---|
ToolGenerator
|
Tool events with the last being the tool result (success or error). |
Source code in strands/tools/structured_output/structured_output_tool.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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
ToolResult
¶
Bases: TypedDict
Result of a tool execution.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
list[ToolResultContent]
|
List of result content returned by the tool. |
status |
ToolResultStatus
|
The status of the tool execution ("success" or "error"). |
toolUseId |
str
|
The unique identifier of the tool use request that produced this result. |
Source code in strands/types/tools.py
87 88 89 90 91 92 93 94 95 96 97 98 | |
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 | |
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
52 53 54 55 56 57 58 59 60 61 62 63 64 | |
convert_pydantic_to_tool_spec(model, description=None)
¶
Converts a Pydantic model to a tool description for the Amazon Bedrock Converse API.
Handles optional vs. required fields, resolves $refs, and uses docstrings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Type[BaseModel]
|
The Pydantic model class to convert |
required |
description
|
Optional[str]
|
Optional description of the tool's purpose |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ToolSpec |
ToolSpec
|
Dict containing the Bedrock tool specification |
Source code in strands/tools/structured_output/structured_output_utils.py
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 | |