Claude API · Tools and structured output
Get a validated Python object back
Pass a Pydantic class as output_format and read the parsed object. The response is guaranteed to match the schema.
from pydantic import BaseModel
class ContactInfo(BaseModel):
name: str
email: str
interests: list[str]
response = client.messages.parse(
model="claude-opus-5-5",
max_tokens=16000,
messages=[{"role": "user", "content": "Extract: Jane Doe ([email protected]) likes APIs and SDKs."}],
output_format=ContactInfo,
)
contact = response.parsed_output
Official Claude API documentation
More tools and structured output commands
- Let the SDK run the tool loop
- Define a tool by hand
- Return a tool result
- Guarantee valid tool input
- Constrain output with a JSON Schema
All tools and structured output commands · Claude API cheatsheet