-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_structured_outputs.py
More file actions
57 lines (47 loc) · 2.1 KB
/
Copy path06_structured_outputs.py
File metadata and controls
57 lines (47 loc) · 2.1 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Pattern: Prompt Chaining
Agents are called sequentially in code — each Runner.run() receives the previous agent's output as input.
Instead of returning free text, the Code Reviewer is configured with output_type=CodeReview — a Pydantic
model — so the SDK deserializes its response into a typed Python object automatically.
That object is then serialized to JSON and passed as input to the next agent in the chain.
Chain: Code Reviewer (CodeReview) → Frontend Developer (HTML) → Email Sender.
"""
import asyncio
from agents import Runner, trace
from my_agents import code_reviewer_agent, frontend_developer_agent, email_sender_agent
from my_agents.code_review.code_reviewer import CodeReview
from utils import display_token_usage, generate_trace_id, get_source_code
async def review_file(file_name: str) -> None:
trace_id = generate_trace_id()
code = get_source_code(file_name)
with trace(workflow_name="Code Review Demo", trace_id=trace_id):
# Review agent call
review_agent_response = await Runner.run(
starting_agent=code_reviewer_agent,
input=code
)
code_review: CodeReview = review_agent_response.final_output
print(f"Code Review Response type: {type(code_review)}")
display_token_usage(review_agent_response)
# Frontend agent call
code_review_json = code_review.model_dump_json()
frontend_agent_response = await Runner.run(
starting_agent=frontend_developer_agent,
input=code_review_json
)
display_token_usage(frontend_agent_response)
# Send Email Agent call
html_content = frontend_agent_response.final_output
email_agent_response = await Runner.run(
starting_agent=email_sender_agent,
input=html_content
)
display_token_usage(email_agent_response)
print("=" * 100)
print(email_agent_response.final_output)
async def main():
# Switch files to demo both cases:
await review_file("buggy_python.py")
# await review_file("CleanBankAccount.java")
if __name__ == "__main__":
asyncio.run(main())