Build useful AI features
Module 6 / Lesson 3

Structured output is a contract

Validate AI output before your application uses it.

17 minutes

Ask for a known shape

A quiz generator can return a question, options, correct option index and explanation. A structured shape simplifies display, but the server still needs to validate types, lengths and relationships such as whether the index points to an existing option.

Shape does not prove truth

Valid JSON can contain a wrong explanation or an unsupported answer. Structural validation and content evaluation address different failures. Reject empty or malformed output explicitly; do not turn it into a successful blank quiz.

json
{
  "question": "Which sessions does the filter keep?",
  "options": ["All", "Assigned", "Unassigned", "None"],
  "correctIndex": 2,
  "explanation": "The predicate keeps empty facilitator values."
}