Werken met de 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="Aantal goede antwoorden van de 10")
passed: bool = Field(description="Waar als score 7 of hoger is")
feedback: str = Field(
description="Bemoedigend bericht met concrete verbetertips"
)
response = client.responses.parse(model="gpt-5-mini",instructions="Je bent een Spaanse woordenschat-tutor. Beoordeel de antwoorden van de student. Geef 2 punten per goed antwoord.", 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"Geslaagd: {result.passed}") print(f"Feedback: {result.feedback}")
Score: 8/10
Geslaagd: True
Feedback: Goed gedaan - je scoorde 8/10 (4/5 goed). De enige fout was #3: 'gato'
betekent 'cat', niet 'car' (Spaans voor 'car' is 'coche' of 'carro'). Tip: herhaal veelvoorkomende
dierwoorden met flashcards en korte quizzen om het geheugen te versterken.
class Mistake(BaseModel): word: str = Field(description="Het Spaanse woord dat fout was") student_answer: str = Field(description="Wat de student invulde") correct_answer: str = Field(description="De juiste vertaling")class DetailedQuizResult(BaseModel): score: int = Field(description="Aantal goede antwoorden van de 10") passed: bool = Field(description="Waar als score 7 of hoger is") feedback: str = Field(description="Bemoedigend bericht met specifieke tips")mistakes: list[Mistake] = Field(description="Lijst met foute antwoorden")
response = client.responses.parse( model="gpt-5-mini", instructions="Je bent een Spaanse woordenschat-tutor. Beoordeel de antwoorden van de student. Geef 2 punten per goed antwoord.", 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"Geslaagd: {result.passed}")for mistake in result.mistakes: print(f"{mistake.word}: '{mistake.student_answer}' -> '{mistake.correct_answer}'")
Score: 6/10
Geslaagd: 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"Geslaagd: {result.passed}")
print(f"Feedback: {result.feedback}")
response = client.responses.parse( model="gpt-5-mini", instructions="...", input="...",text_format=QuizResult)
Werken met de OpenAI Responses API