Skip to content

Commit d9ac806

Browse files
authored
structured output async (#145)
1 parent 8270f51 commit d9ac806

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

docs/examples/python/structured_output.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Structured Output Example
2525
This example demonstrates how to use structured output with Strands Agents to
2626
get type-safe, validated responses using Pydantic models.
2727
"""
28+
import asyncio
2829
import tempfile
2930
from typing import List, Optional
3031
from pydantic import BaseModel, Field
@@ -153,13 +154,34 @@ def complex_nested_model_example():
153154
print(f"Skills: {result.skills}") # ["systems admin"]
154155

155156

157+
async def async_example():
158+
"""Basic example extracting structured information from text asynchronously."""
159+
print("\n--- Async Example ---")
160+
161+
class PersonInfo(BaseModel):
162+
name: str
163+
age: int
164+
occupation: str
165+
166+
agent = Agent()
167+
result = await agent.structured_output_async(
168+
PersonInfo,
169+
"John Smith is a 30-year-old software engineer"
170+
)
171+
172+
print(f"Name: {result.name}") # "John Smith"
173+
print(f"Age: {result.age}") # 30
174+
print(f"Job: {result.occupation}") # "software engineer"
175+
176+
156177
if __name__ == "__main__":
157178
print("Structured Output Examples\n")
158179

159180
basic_example()
160181
multimodal_example()
161182
conversation_history_example()
162183
complex_nested_model_example()
184+
asyncio.run(async_example())
163185

164186
print("\nExamples completed.")
165187
```
@@ -168,6 +190,7 @@ if __name__ == "__main__":
168190

169191
1. **Define a Schema**: Create a Pydantic model that defines the structure you want
170192
2. **Call structured_output()**: Pass your model and optionally a prompt to the agent
193+
- If running async, call `structured_output_async()` instead.
171194
3. **Get Validated Results**: Receive a properly typed Python object matching your schema
172195

173196
The `structured_output()` method ensures that the language model generates a response that conforms to your specified schema. It handles converting your Pydantic model into a format the model understands and validates the response.

docs/examples/python/structured_output.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
This example demonstrates how to use structured output with Strands Agents to
66
get type-safe, validated responses using Pydantic models.
77
"""
8+
import asyncio
89
import tempfile
910

1011
from typing import List, Optional
@@ -134,12 +135,33 @@ class Person(BaseModel):
134135
print(f"Skills: {result.skills}") # ["systems admin"]
135136

136137

138+
async def async_example():
139+
"""Basic example extracting structured information from text asynchronously."""
140+
print("\n--- Async Example ---")
141+
142+
class PersonInfo(BaseModel):
143+
name: str
144+
age: int
145+
occupation: str
146+
147+
agent = Agent()
148+
result = await agent.structured_output_async(
149+
PersonInfo,
150+
"John Smith is a 30-year-old software engineer"
151+
)
152+
153+
print(f"Name: {result.name}") # "John Smith"
154+
print(f"Age: {result.age}") # 30
155+
print(f"Job: {result.occupation}") # "software engineer"
156+
157+
137158
if __name__ == "__main__":
138159
print("Structured Output Examples\n")
139160

140161
basic_example()
141162
multimodal_example()
142163
conversation_history_example()
143164
complex_nested_model_example()
165+
asyncio.run(async_example())
144166

145167
print("\nExamples completed.")

docs/user-guide/concepts/agents/structured-output.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,31 @@ except ValidationError as e:
193193
# 3. Extract partial information from the error
194194
```
195195

196+
### Async
197+
198+
Strands also supports obtaining structured output asynchronously through [`structured_output_async`](../../../api-reference/agent.md#strands.agent.agent.Agent.structured_output_async):
199+
200+
```python
201+
import asyncio
202+
from pydantic import BaseModel
203+
from strands import Agent
204+
205+
class PersonInfo(BaseModel):
206+
name: str
207+
age: int
208+
occupation: str
209+
210+
async def structured_output():
211+
agent = Agent()
212+
return await agent.structured_output_async(
213+
PersonInfo,
214+
"John Smith is a 30-year-old software engineer"
215+
)
216+
217+
result = asyncio.run(structured_output())
218+
```
219+
220+
196221
## Best Practices
197222

198223
- **Keep models focused**: Define specific models for clear purposes

0 commit comments

Comments
 (0)