Structured Outputs makes an LLM response conform to a developer-provided schema, so an application can parse fields reliably instead of interpreting free-form text. section 9.3.1
A practical implementation follows three steps:
- Define a Pydantic BaseModel with typed fields such as is_correct: bool and correct_answer: str. section 9.3.1
- Pass the model as response_format to the OpenAI parsing endpoint, which returns a parsed object. section 9.3.1
- Use the returned attributes in application logic, such as branching between a success message and an explanation. section 9.3.1
Evaluating a quiz answer
A quiz application sends the question and player answer to an LLM with an AnswerEvaluation schema. The app reads evaluation.is_correct to show success or failure and evaluation.correct_answer to reveal the answer. section 9.3.1
Listing 9.5 The AnswerEvaluation class in answer_evaluation.py
from pydantic import BaseModel
class AnswerEvaluation(BaseModel):
is_correct: bool
correct_answer: str
Inspect the minimal AnswerEvaluation schema used to validate a boolean result and a string answer. section 9.3.1























