Vibe Coder · Unit 10: APIs · Lesson 8 of 13

Structured outputs

Engineers: get responses that match a schema, validate them, and decide what happens when they do not.

Read the short notes and pictures.Do everything inside a blue frame.

Schemas and validation

Providers offer ways to constrain output to a JSON schema. Use them, and still validate on your side: a schema library gives you one definition for the prompt, the runtime check and your TypeScript types.

A 3D scene of coloured blocks and cards. A schema: theme: "billing" | "bug" | "idea" | "other", urgency: 1 | 2 | 3, summary: string.
Enumerations and types for each field.

Design choices

Enumerations

A fixed list beats free text for anything you group by.

An escape value

"other" or null, so the model is not forced to guess.

Small schemas

Fewer fields, fewer failures.

Descriptions

A line per field improves accuracy.

A 3D scene of coloured blocks and cards. On failure: retry once, then fall back, log it.
Retry once, fall back, log.

Handling failure

  1. Parse and validate

    Reject anything that does not conform.

  2. Retry with the error

    Once.

  3. Fall back

    A safe default, or a manual path.

  4. Measure

    Track the failure rate.

ActivityBuild stepOne AI feature

Add a schema to your feature

Define a schema for your feature’s output, request structured output, validate it, and implement retry and fallback. Add tests for a valid, an invalid and a refused response.

Done when

Your database types will be generated from the same shapes in Unit 12.

Keep these

  • Constrain output with a schema.
  • Validate on your side as well.
  • Plan for failure.