Debian Codegraph @

examples/structured_outputs.py

raw
1 import pydantic
2
3 import anthropic
4
5
6 class Order(pydantic.BaseModel):
7 product_name: str
8 price: float
9 quantity: int
10
11
12 client = anthropic.Anthropic()
13
14 prompt = """
15 Extract the product name, price, and quantity from this customer message:
16 "Hi, I’d like to order 2 packs of Green Tea for 5.50 dollars each."
17 """
18
19 parsed_message = client.messages.parse(
20 model="claude-sonnet-5-5",
21 messages=[{"role": "user", "content": prompt}],
22 max_tokens=1024,
23 output_format=Order,
24 )
25
26 print(parsed_message.parsed_output) # Order(product_name='Green Tea', price=5.5, quantity=2)