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.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
🚀 Introducing Agent Gateway — governance, observability, and control for your AI agents.  Register for live webinar ↗
Direct models to return data that conforms to a specific JSON schema.
response_format interface.
Define object schemas using Pydantic (Python) or Zod (JavaScript) to extract structured information from unstructured text.
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)
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)
Was this page helpful?
