AI & automation

Handling Missing Data in Structured Outputs

Learn how to handle missing data in LLM JSON schemas by setting explicit null values and default values to prevent parser errors.

To handle missing info in a schema, explicitly define optional fields using union types with null or set clear default values in your properties object. Models often invent placeholder text or omit fields entirely when data is missing. Declaring explicit nullability ensures your downstream application safely parses every API payload without unexpected execution failures.

Designing Schemas to Handle Missing Fields Explicitly

When configuring structured outputs for an API call, specify optional properties directly inside your JSON schema object. The standard JSON schema format map key string properties to defined type blueprints. If a field might lack data in the source context, mark it as a nullable type rather than leaving the field implicit JSON Schema object reference.

Leaving missing properties completely unmanaged causes LLMs to emit invalid keys or random string tokens like N/A. By adding explicit null types or default string values to your property definitions, you enforce consistent key presence across all responses. Downstream parsers can then reliably unpack payload objects without key error exceptions.

Processing Sample Customer Records with Missing Values

Consider an extraction script parsing incoming web forms into a strict schema. The input prompt contains a customer named Jane Doe with no middle name on record.

Your defined JSON schema requires middle_name to accept either a string or a null value.

{
  "type": "object",
  "properties": {
    "first_name": {"type": "string"},
    "middle_name": {"type": ["string", "null"]},
    "last_name": {"type": "string"}
  },
  "required": ["first_name", "middle_name", "last_name"]
}

The model processes the prompt and outputs a valid JSON object containing middle_name set directly to null. Your code parses the object smoothly, assigning the null value safely without raising missing key runtime exceptions.

Diagnosing Implicit Omissions and Parser Errors

A common failure occurs when developers remove optional fields from the required properties list without adding null to the field type schema. The model might omit the key entirely on one request, then generate empty string values on the next request.

When your Python application expects explicit object properties, omitted keys trigger KeyErrors during data mapping. Downstream validation scripts fail because the object structure changes dynamically based on source document completeness. Fixing this requires updating the explicit JSON schema to declare every expected property while granting explicit permission for null values on fields that lack source data.

Validating Schema Implementations with Automated Guardrails

Verify your schema design by passing incomplete test datasets through your integration workflow. Ensure your application includes a human review step when critical required fields return null unexpectedly.

Build automated unit tests that pass the generated payload directly into a standard JSON schema validator before executing business logic.

Step 1: Send raw text with missing fields to the API.

Step 2: Validate the returned JSON payload against your schema.

Step 3: Confirm that omitted data resolves directly to explicit null values instead of missing object keys. This pipeline check guarantees that missing data never breaks downstream storage tasks.