Structured Output · practice
Write a Pydantic Model
QuizQuestion describes one question. A reply holds a whole quiz: an object with a questions list, as QUIZ_SYSTEM asks. This lesson writes the model for that object, QuizSet, in the same form you read in Lesson 4.
Write QuizSet
In quiz_models.py, below QuizQuestion, is the placeholder:
# Lesson 5: replace the next line with your QuizSet model.
QuizSet = None
Replace both lines with a model. Copy the class line from QuizQuestion, change the name, and give it one field, questions, whose hint is a list of QuizQuestion:
class QuizSet(BaseModel):
questions: list[QuizQuestion]
list[QuizQuestion] uses one model inside another: a QuizSet holds a list, and every item of that list is a QuizQuestion. Put QuizSet after QuizQuestion, so a reader meets QuizQuestion before the model that uses it. On Python versions before 3.14 the order is required: the hint is evaluated when the class line runs, and QuizQuestion must already exist.
Give the field no default value. Without one, the field is required, so a reply that has no questions at all is not a valid quiz.
Nested dictionaries become models
Here is the whole file as it will look, followed by a quiz built from plain dictionaries:
from pydantic import BaseModel
class QuizQuestion(BaseModel):
question: str
choices: list[str]
answer_index: int
class QuizSet(BaseModel):
questions: list[QuizQuestion]
quiz = QuizSet(
questions=[
{"question": "What does len([]) return?", "choices": ["0", "None"], "answer_index": 0},
{"question": "Which brackets make a list?", "choices": ["()", "[]", "{}"], "answer_index": 1, "hint": "square"},
]
)
print(type(quiz.questions[1]).__name__)
print(quiz.questions[1].choices)
print(quiz.questions[1].model_dump())
QuizQuestion
['()', '[]', '{}']
{'question': 'Which brackets make a list?', 'choices': ['()', '[]', '{}'], 'answer_index': 1}
You passed dictionaries, and QuizSet turned each one into a QuizQuestion, because the field’s hint says that is what the items are. So quiz.questions[1].choices reads with dots all the way down. type(...).__name__ prints the name of a value’s type, here QuizQuestion, not dict.
Look at the second dictionary: it has a key the model does not declare, "hint". By default pydantic ignores keys it does not know, so model_dump() shows only the three fields. A reply from the language model can carry extra keys, and your program never sees them.
If one item cannot become a QuizQuestion, for example because its answer_index is "first", creating the QuizSet fails with an error that names the problem. That error is the subject of Lesson 6.
Run it
Run python structured.py. The demonstration builds a QuizSet from two dictionaries, prints it, and prints the type of its second question:
A QuizSet built from dictionaries:
QuizSet(questions=[QuizQuestion(question='What does len([]) return?', choices=['0', 'None'], answer_index=0), QuizQuestion(question='Which brackets make a list?', choices=['()', '[]', '{}'], answer_index=1)])
quiz.questions[1] is a QuizQuestion
The JSON reply, validated by parse_quiz:
Run stopped at the next function to build: parse_quiz is built in Lesson 6.
QuizSet is the shape the whole chapter is about. The reply the language model sends is JSON text, and Lesson 6 turns that text into a QuizSet in one step, or tells you exactly why it cannot.
QuizSet has the field questions: list[QuizQuestion]. You create QuizSet(questions=[{"question": "Q?", "choices": ["a", "b"], "answer_index": 0}]). What is quiz.questions[0]?
A reply's question has an extra key, "difficulty": "easy", that QuizQuestion does not declare. What does pydantic do with it by default?
Task
In quiz_models.py, replace the line QuizSet = None with a pydantic model called QuizSet:
write its class line the way
QuizQuestion’s is written, with(BaseModel);give it exactly one field,
questions, whose hint is a list ofQuizQuestion.
Leave QuizQuestion as it is, and give the field no default value. Run python structured.py to see a QuizSet built from dictionaries, then check your work.