New Tool Radar

AI Concepts

How to Write a JSON Schema with Required Fields and an Enum

Write a JSON Schema that forces the fields you need and limits a choice to the values you allow.

Schematic illustration of write a json schema with required fields and an enum: step one list the fields, step two mark the required ones, step three add the enum.

A JSON Schema for Structured Outputs names the fields, their types, which ones are required, and, for fixed choices, the allowed values as an enum. Required fields and enum values follow the supported schema you supply. Writing the schema carefully is the whole job: the model fills the shape you define, so a vague schema gets vague replies.

Prerequisites

  • The list of fields your code reads.
  • The fixed set of allowed values for any choice field.
  • The Structured Outputs guide open for the supported schema subset.

Steps

Step 1: List the fields

Write every field with its type: string, integer, number, boolean, array or object. Give each field a plain description of what it means, so the model fills it with the right value.

Step 2: Mark the required ones

Put the fields your code cannot do without in the required list. Leave a field out of required only if your code handles it being absent.

Step 3: Add the enum

For any field with a fixed set of values, list them as an enum. The model must then pick one of your values instead of inventing its own.

Worked example: Harbor Light library loan record

The fictional Harbor Light library defines a loan record schema with four fields, three required, and a status enum.

Field Type Required Enum or rule
book_title string Yes None
borrower string Yes None
due_date string Yes Date form YYYY-MM-DD
status string No on-loan, returned, overdue

The schema has 4 fields, 3 required, and the status field allows only on-loan, returned or overdue, so the model cannot invent a fourth status.

Expected result. The schema lists every field your code reads, marks the essential ones required, and limits fixed choices to an enum. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • Only a subset of JSON Schema is supported in Structured Outputs; check the guide before using advanced keywords.
  • A required field forces the model to supply a value, which can be a guess if the information is missing; prefer nullable for uncertain fields.
  • The schema controls shape, not truth.

Sources

See all