Consistent Outputs, Every Time!
You're developing a personalized movie recommendation system for a streaming platform. To ensure the recommendations can be properly displayed in the app's UI, you need to use structured outputs with pydantic and the OpenAI client. You'll define a schema for movie recommendations and extract the structured results.
This exercise is part of the course
Working with the OpenAI Responses API
Exercise instructions
- Define a
pydanticclass calledMovieRecommendationwithtitle,genre,vibe, andwhyfields. - Generate a structured recommendation using the
MovieRecommendationclass and the prompts provided. - Extract the parsed recommendation from the response, then access its
titleandwhyinformation.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define the book recommendation schema
class ____(BaseModel):
____: str = Field(description="The book title")
____: str = Field(description="Primary genre")
____: str = Field(description="One-word vibe: cozy, thrilling, emotional, or fun")
____: str = Field(description="One sentence explaining why this matches")
# Generate structured recommendation
response = client.responses.____(
model="gpt-5-mini",
instructions="You are a knowledgeable movie recommender.",
input="Recommend a movie for someone who loved Inception and wants something mind-bending",
text_format=____,
)
# Extract the parsed output and results
recommendation = response.____
print(f"Title: {recommendation.____}")
print(f"Reason: {recommendation.____}")