# Structured Outputs and JSON Schema

> Make models return data your code can trust: JSON mode, schema-constrained outputs, validation and repair. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/ai-engineer/structured-outputs
Last updated: 2026-11-28

Make models return data your code can trust: JSON mode, schema-constrained outputs, validation and repair.

Beginner · about 5 hours · difficulty 3/5

Learn first: [Messages APIs and Provider SDKs](https://www.internhack.in/roadmaps/tracks/ai-engineer/first-api-calls)

Most real applications need **data**, not prose: extracted fields, a classification label, a list of steps. Parsing free text with regexes is fragile. Structured outputs solve this.

### Levels of reliability

1. **Ask nicely**: "Respond only with JSON matching this shape." Works most of the time, fails in production at scale.
2. **JSON mode**: the provider guarantees syntactically valid JSON, but not your schema.
3. **Schema-constrained output**: you pass a **JSON Schema** and the provider constrains decoding so the output matches it. Most major APIs support this directly or through tool calling.
4. **Validate anyway**: parse into a Pydantic model or Zod schema. Constrained decoding guarantees shape, not truth.

### Designing good schemas

- Keep schemas flat and small; deeply nested optional structures confuse models.
- Use **enums** for categories and describe each field with a `description`.
- Add a field for uncertainty (for example `"confidence"` or `"not_found"`) so the model is not forced to invent values.
- Put a reasoning field **before** the answer field if you want the model to think first.

### Pitfalls

Schema-valid output can still be wrong: a date in the wrong format, a hallucinated invoice number. Add business-rule validation and, where needed, a retry with the validation error fed back to the model.

### Good enough to move on

You can extract typed records from messy text with a schema, validate them, and handle the cases where the data is genuinely missing.

## Key concepts

- **JSON mode vs schema-constrained output**
- **Writing JSON Schemas**
- **Pydantic / Zod validation**
- **Enums and field descriptions**
- **Handling missing data**
- **Retry with validation feedback**
- **Instructor and similar libraries**

## Mini project

Build an invoice or job-posting extractor that turns 20 messy text samples into validated JSON records using a schema, with a report of which fields were missing or failed validation.

## Check yourself

Your output always matches the schema, but a customer reports wrong prices. What does schema enforcement guarantee and what does it not?

## Free resources

- [OpenAI docs: structured outputs](https://platform.openai.com/docs/guides/structured-outputs) (OpenAI)
- [Gemini API: structured output](https://ai.google.dev/gemini-api/docs/structured-output) (Google)
- [JSON Schema: getting started](https://json-schema.org/learn) (json-schema.org)

Previous: [Streaming Responses](https://www.internhack.in/roadmaps/tracks/ai-engineer/streaming-responses) · Next: [Tool and Function Calling](https://www.internhack.in/roadmaps/tracks/ai-engineer/tool-calling)

## More in Working with Model APIs

- [Messages APIs and Provider SDKs](https://www.internhack.in/roadmaps/tracks/ai-engineer/first-api-calls)
- [Streaming Responses](https://www.internhack.in/roadmaps/tracks/ai-engineer/streaming-responses)
- [Structured Outputs and JSON Schema](https://www.internhack.in/roadmaps/tracks/ai-engineer/structured-outputs)
- [Tool and Function Calling](https://www.internhack.in/roadmaps/tracks/ai-engineer/tool-calling)
- [Prompt Caching and Batch Processing](https://www.internhack.in/roadmaps/tracks/ai-engineer/prompt-caching-batching)
- [Rate Limits, Errors and Cost Awareness](https://www.internhack.in/roadmaps/tracks/ai-engineer/rate-limits-and-cost)
- [Running Open-Weight Models Locally](https://www.internhack.in/roadmaps/tracks/ai-engineer/open-weight-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [AI Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/ai-engineer)
