
Structured Outputs guarantees the model returns valid JSON matching your exact schema — no more parsing failures in production.
from pydantic import BaseModel
class MovieReview(BaseModel):
title: str
rating: int
summary: str
sentiment: Literal["positive", "negative", "neutral"]
response = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[{"role": "user", "content": "Review: This film was incredible..."}],
response_format=MovieReview,
)
review = response.choices[0].message.parsed
print(review.rating) # guaranteed integerReference:
TaskLoco™ — The Sticky Note GOAT