Generating Structured Outputs

Working with the OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

Coming up...

ch3_overview.jpg

Working with the OpenAI Responses API

The Need for Structure

language_app1.jpg

Working with the OpenAI Responses API

The Need for Structure

language_app2.jpg

Working with the OpenAI Responses API

The Need for Structure

language_app3.jpg

Working with the OpenAI Responses API

Part 1: Defining the Output Schema

from pydantic import BaseModel


class QuizResult(BaseModel): score: int passed: bool feedback: str
Working with the OpenAI Responses API

Part 1: Defining the Output Schema

from pydantic import BaseModel, Field

class QuizResult(BaseModel):
    score: int = Field(description="Number of correct answers out of 10")
    passed: bool = Field(description="True if score is 7 or higher")
    feedback: str = Field(
        description="Encouraging message with specific tips for improvement"
    )
Working with the OpenAI Responses API

Part 2: Sending the Request

response = client.responses.parse(

model="gpt-5-mini",
instructions="You are a Spanish vocabulary tutor. Grade the student's quiz answers. Grade the quiz with 2 points per correct answer.", input="""1. casa = house 2. perro = dog 3. gato = car 4. libro = book 5. agua = water""",
text_format=QuizResult
)
Working with the OpenAI Responses API

Part 3: Extracting the Results

result = response.output_parsed

print(f"Score: {result.score}/10") print(f"Passed: {result.passed}") print(f"Feedback: {result.feedback}")
Score: 8/10
Passed: True
Feedback: Great job - you scored 8/10 (4/5 correct). The only mistake was #3: 'gato'
means 'cat', not 'car' (Spanish for 'car' is 'coche' or 'carro'). Tip: review common
animal vocabulary with flashcards and short quizzes to reinforce recall.
Working with the OpenAI Responses API

More Complex Data Structures

class Mistake(BaseModel):
    word: str = Field(description="The Spanish word that was incorrect")
    student_answer: str = Field(description="What the student wrote")
    correct_answer: str = Field(description="The correct translation")

class DetailedQuizResult(BaseModel): score: int = Field(description="Number of correct answers out of 10") passed: bool = Field(description="True if score is 7 or higher") feedback: str = Field(description="Encouraging message with specific tips")
mistakes: list[Mistake] = Field(description="List of incorrect answers")
Working with the OpenAI Responses API
response = client.responses.parse(
    model="gpt-5-mini",
    instructions="You are a Spanish vocabulary tutor. Grade the student's quiz
    answers. Grade the quiz with 2 points per correct answer.",
    input="""1. casa = house
             2. perro = dog
             3. gato = car
             4. libro = library
             5. agua = water""",

text_format=DetailedQuizResult
)
Working with the OpenAI Responses API
result = response.output_parsed
print(f"Score: {result.score}/10")
print(f"Passed: {result.passed}")

for mistake in result.mistakes: print(f"{mistake.word}: '{mistake.student_answer}' -> '{mistake.correct_answer}'")
Score: 6/10
Passed: False
gato: 'car' -> 'cat'
libro: 'library' -> 'book'
Working with the OpenAI Responses API

Summary

from pydantic import BaseModel, Field

class QuizResult(BaseModel):
    score: int = Field(...)
    passed: bool = Field(...)
    feedback: str = Field(...)
result = response.output_parsed

print(f"Score: {result.score}/10")
print(f"Passed: {result.passed}")
print(f"Feedback: {result.feedback}")
response = client.responses.parse(
    model="gpt-5-mini",
    instructions="...",
    input="...",

text_format=QuizResult
)
Working with the OpenAI Responses API

Let's practice!

Working with the OpenAI Responses API

Preparing Video For Download...