Skip to main content
Structured Outputs ensure that models follow your supplied JSON schema. Portkey supports this for Anthropic models using a unified response_format interface. Define object schemas using Pydantic (Python) or Zod (JavaScript) to extract structured information from unstructured text. This approach provides type hinting and automatic validation.
from portkey_ai import Portkey
from pydantic import BaseModel

class Step(BaseModel):
    explanation: str
    output: str

class MathReasoning(BaseModel):
    steps: list[Step]
    final_answer: str

portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    provider="anthropic"
)

# Use .parse() for automatic parsing
completion = portkey.chat.completions.parse(
    model="claude-3-5-sonnet-20241022",
    messages=[
        {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},
        {"role": "user", "content": "how can I solve 8x + 7 = -23"}
    ],
    response_format=MathReasoning
)

print(completion.choices[0].message.parsed)

2. Using Raw JSON Schema

For cross-language compatibility or dynamic schemas, pass a standard JSON schema directly.
from portkey_ai import Portkey

portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    provider="anthropic"
)

completion = portkey.chat.completions.create(
    model="claude-3-5-sonnet-20241022",
    messages=[
        {"role": "system", "content": "Extract the event information."},
        {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "event_extraction",
            "schema": {
                "type": "object",
                "properties": {
                    "location": { "type": "string" },
                    "date": { "type": "string" },
                    "participants": { "type": "array", "items": { "type": "string" } }
                },
                "required": ["location", "date", "participants"],
                "additionalProperties": False
            },
            "strict": True
        }
    }
)

print(completion.choices[0].message.content)
For more, refer to Anthropic’s detailed documentation on Structured Outputs here.
Last modified on March 10, 2026