-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathworkflow.py
More file actions
38 lines (28 loc) · 1.22 KB
/
Copy pathworkflow.py
File metadata and controls
38 lines (28 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
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
"""Structured output: agent returns a typed Pydantic model.
``TemporalAgent(structured_output_model=PersonInfo)`` makes Strands coerce the
model's final response into an instance of ``PersonInfo``. The plugin installs
``pydantic_data_converter`` by default, so the typed value flows back across
the activity/workflow boundary without extra wiring.
"""
# @@@SNIPSTART python-strands-structured-output-workflow
from datetime import timedelta
from pydantic import BaseModel, Field
from temporalio import workflow
from temporalio.contrib.strands import TemporalAgent
class PersonInfo(BaseModel):
name: str = Field(description="Name of the person")
age: int = Field(description="Age of the person")
occupation: str = Field(description="Occupation of the person")
@workflow.defn
class StructuredOutputWorkflow:
def __init__(self) -> None:
self.agent = TemporalAgent(
start_to_close_timeout=timedelta(seconds=60),
structured_output_model=PersonInfo,
)
@workflow.run
async def run(self, prompt: str) -> PersonInfo:
result = await self.agent.invoke_async(prompt)
assert isinstance(result.structured_output, PersonInfo)
return result.structured_output
# @@@SNIPEND