Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.portkey.ai/docs/llms.txt

Use this file to discover all available pages before exploring further.

Structured Outputs ensure that models follow your supplied JSON schema. Portkey supports this for Claude models on AWS Bedrock using a unified response_format interface. Define object schemas using Pydantic (Python) or Zod (JavaScript) to extract structured information from unstructured text.
This feature is supported on Claude 3 and later models hosted on AWS Bedrock.
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",
)

# Use .parse() for automatic parsing
completion = portkey.chat.completions.parse(
    model="@bedrock/us.anthropic.claude-sonnet-4-5-20250929-v1:0",
    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"
)

completion = portkey.chat.completions.create(
    model="@bedrock/us.anthropic.claude-3-5-sonnet-20241022-v2:0",
    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 18, 2026