使用 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="10题中答对的数量")
passed: bool = Field(description="若得分≥7则为 True")
feedback: str = Field(
description="带具体改进建议的鼓励性反馈"
)
response = client.responses.parse(model="gpt-5.4-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="题中错误的西语单词") student_answer: str = Field(description="学生填写的答案") correct_answer: str = Field(description="正确译法")class DetailedQuizResult(BaseModel): score: int = Field(description="10题中答对的数量") passed: bool = Field(description="若得分≥7则为 True") feedback: str = Field(description="带具体建议的鼓励性反馈")mistakes: list[Mistake] = Field(description="错题列表")
response = client.responses.parse( model="gpt-5.4-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.4-mini", instructions="...", input="...",text_format=QuizResult)
使用 OpenAI Responses API