Working with the OpenAI Responses API
James Chapman
AI Curriculum Manager, DataCamp




from pydantic import BaseModelclass QuizResult(BaseModel): score: int passed: bool feedback: str
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"
)
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)
result = response.output_parsedprint(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.
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")
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)
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'
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