You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For more details, see the [Tool Response Format](#tool-response-format) section below.
90
90
91
+
### Async Invocation
92
+
93
+
Decorated tools may also be defined async. Strands will invoke all async tools concurrently.
94
+
95
+
```Python
96
+
import asyncio
97
+
from strands import Agent, tool
98
+
99
+
100
+
@tool
101
+
asyncdefcall_api() -> str:
102
+
"""Call API asynchronously."""
103
+
104
+
await asyncio.sleep(5) # simulated api call
105
+
return"API result"
106
+
107
+
108
+
asyncdefasync_example():
109
+
agent = Agent(tools=[call_api])
110
+
await agent.invoke_async("Can you call my API?")
111
+
112
+
113
+
asyncio.run(async_example())
114
+
```
115
+
91
116
## Python Modules as Tools
92
117
93
118
An alternative approach is to define a tool as a Python module with a specific structure. This enables creating tools that don't depend on the SDK directly.
result =f"Weather forecast for {city} for the next {days} days..."
142
-
167
+
143
168
# Return structured response
144
169
return {
145
170
"toolUseId": tool_use_id,
@@ -171,6 +196,34 @@ agent = Agent(
171
196
)
172
197
```
173
198
199
+
### Async Invocation
200
+
201
+
Similar to decorated tools, users may define their module tools async.
202
+
203
+
```Python
204
+
TOOL_SPEC= {
205
+
"name": "call_api",
206
+
"description": "Call my API asynchronously.",
207
+
"inputSchema": {
208
+
"json": {
209
+
"type": "object",
210
+
"properties": {},
211
+
"required": []
212
+
}
213
+
}
214
+
}
215
+
216
+
asyncdefcall_api(tool, **kwargs):
217
+
await asyncio.sleep(5) # simulated api call
218
+
result ="API result"
219
+
220
+
return {
221
+
"toolUseId": tool["toolUseId"],
222
+
"status": "success",
223
+
"content": [{"text": result}],
224
+
}
225
+
```
226
+
174
227
### Tool Response Format
175
228
176
229
Tools can return responses in various formats using the [`ToolResult`](../../../api-reference/types.md#strands.types.tools.ToolResult) structure. This structure provides flexibility for returning different types of content while maintaining a consistent interface.
@@ -227,4 +280,4 @@ When using the [`@tool`](../../../api-reference/tools.md#strands.tools.decorator
227
280
228
281
1. If you return a string or other simple value, it's wrapped as `{"text": str(result)}`
229
282
2. If you return a dictionary with the proper [`ToolResult`](../../../api-reference/types.md#strands.types.tools.ToolResult) structure, it's used directly
230
-
3. If an exception occurs, it's converted to an error response
283
+
3. If an exception occurs, it's converted to an error response
Copy file name to clipboardExpand all lines: docs/user-guide/concepts/tools/tools_overview.md
+26-18Lines changed: 26 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,19 +94,21 @@ Build your own Python tools using the Strands SDK's tool interfaces.
94
94
Function decorated tools can be placed anywhere in your codebase and imported in to your agent's list of tools. Define any Python function as a tool by using the [`@tool`](../../../api-reference/tools.md#strands.tools.decorator.tool) decorator.
Tool modules can contain function decorated tools, in this example `get_user_location.py`:
128
+
await asyncio.sleep(5) # simulated api call
129
+
return"API result"
127
130
128
-
```python
129
-
# get_user_location.py
130
131
131
-
from strands import tool
132
+
defbasic_example():
133
+
agent = Agent(tools=[get_user_location, weather])
134
+
agent("What is the weather like in my location?")
132
135
133
-
@tool
134
-
defget_user_location() -> str:
135
-
"""Get the user's location
136
-
"""
137
136
138
-
# Implement user location lookup logic here
139
-
return"Seattle, USA"
137
+
asyncdefasync_example():
138
+
agent = Agent(tools=[call_api])
139
+
await agent.invoke_async("Can you call my API?")
140
+
141
+
142
+
defmain():
143
+
basic_example()
144
+
asyncio.run(async_example())
140
145
```
141
146
147
+
#### Module-Based Approach
148
+
142
149
Tool modules can also provide single tools that don't use the decorator pattern, instead they define the `TOOL_SPEC` variable and a function matching the tool's name. In this example `weather.py`:
143
150
144
151
```python
@@ -165,6 +172,7 @@ TOOL_SPEC = {
165
172
}
166
173
167
174
# Function name must match tool name
175
+
# May also be defined async similar to decorated tools
0 commit comments