New Tool Radar

AI Concepts

How to Mark Optional Fields Nullable in a Structured Outputs Schema

Mark optional fields nullable in your schema so the model returns null instead of inventing a value.

Schematic illustration of mark optional fields nullable in a structured outputs schema: step one find the uncertain fields, step two make them nullable, step three handle null in code.

A required field forces the model to supply a value, and if the information is missing the model may invent one. The fix is to make optional fields nullable: the schema allows null, so the model can say it does not know instead of guessing. Use this for any field your source may not contain.

Prerequisites

  • A JSON Schema with at least one field the source may not contain.
  • Code that handles a null value for that field.
  • The Structured Outputs guide open for the nullable form in your API.

Steps

Step 1: Find the uncertain fields

Go through the schema and mark every field the source might not contain: a middle name, a second phone number, a discount code. If the source always has it, leave it required.

Step 2: Make them nullable

Change the field’s type to allow null alongside its normal type. In a strict schema this is how an optional field is expressed; a missing value becomes an explicit null, not an invented string.

Step 3: Handle null in code

Write the code that reads the reply to accept null for those fields: skip the field, show ‘not given’, or leave it blank. Test with a reply that contains a null before you trust it.

Worked example: Bluebell Bakery customer record

The fictional Bluebell Bakery extracts customer records. The phone number is often missing from the order form.

Field Type Value when missing Code handles it
name string, required Always present Yes
phone string or null null Shows ‘not given’
email string or null null Shows ‘not given’
loyalty_card string or null null Leaves it blank

Three of the four fields are nullable, and the code shows ‘not given’ or leaves the field blank when the value is null, instead of receiving an invented number.

Expected result. Fields the source may lack come back as null, and your code handles null without crashing or storing a guess. 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

  • Nullable is for missing information, not for wrong information; a wrong value still passes the schema.
  • The exact nullable syntax differs between the Chat Completions and Responses APIs; check the guide for your API.
  • Do not make every field nullable: then the schema guarantees almost nothing.

Sources

See all