Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""Structured outputs with the Python SDK: client.messages.parse() + a Pydantic model (SDK transforms the schema,2validates the response, returns parsed_output). Also shows the raw output_config.format form and an unsupported3schema keyword (`minimum`) returning 400.4STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): parsed Person(name='Ada', age=36, city='Lyon');5raw call OK; `minimum` → 400 "For 'integer' type, property 'minimum' is not supported". ≈$0.0006.6Run: .venv/bin/python examples/anthropic/structured-output/json_schema_pydantic.py7"""8from __future__ import annotations910import json11import os12import sys13from pathlib import Path1415sys.path.insert(0, str(Path(__file__).resolve().parents[3]))16from scripts import live # noqa: E4021718import anthropic # noqa: E40219from pydantic import BaseModel # noqa: E4022021MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-haiku-4-5-20251001")22client = anthropic.Anthropic(max_retries=1)23PROMPT = "Ada is 36 and lives in Lyon. Extract the person."242526class Person(BaseModel):27 name: str28 age: int29 city: str303132parsed = client.messages.parse(model=MODEL, max_tokens=100, output_format=Person, messages=[{"role": "user", "content": PROMPT}])33print("parse():", parsed.parsed_output)34live.log_request("anthropic", "POST", "/v1/messages", 200, 0.0003, f"example structured-output/json_schema_pydantic.py parse {MODEL}")3536schema = {"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}, "city": {"type": "string"}},37 "required": ["name", "age", "city"], "additionalProperties": False}38raw = client.messages.create(model=MODEL, max_tokens=100, output_config={"format": {"type": "json_schema", "schema": schema}},39 messages=[{"role": "user", "content": PROMPT}])40print("raw:", json.loads(raw.content[0].text))41live.log_request("anthropic", "POST", "/v1/messages", 200, 0.0003, f"example structured-output/json_schema_pydantic.py raw {MODEL}")4243bad = {"type": "object", "properties": {"age": {"type": "integer", "minimum": 0}}, "required": ["age"], "additionalProperties": False}44try:45 client.messages.create(model=MODEL, max_tokens=50, output_config={"format": {"type": "json_schema", "schema": bad}},46 messages=[{"role": "user", "content": PROMPT}])47except anthropic.BadRequestError as e:48 print("unsupported keyword ->", e.status_code, e.body["error"]["message"])49 live.log_request("anthropic", "POST", "/v1/messages", e.status_code, 0, "example structured-output/json_schema_pydantic.py minimum -> 400")50