Stop 12 · Prompt EngineeringTopics · 5/10

Structured Output (JSON Mode)

Part of the AI Engineer Roadmap

In short

Your code can't use 'Sure! Here's the info you asked for...' It needs clean JSON with the right keys, every time. Structured output turns a chatty model into something your program can trust.

Constraining a model's output to a specific schema (usually JSON) so it can be reliably parsed by downstream code — essential for building AI features, not just chat interfaces.

The blob fills a form of neat empty boxes with a pen while a clerk waits behind the window.

Think of it like this

The railway reservation form

The reservation counter doesn't accept a letter saying 'I'd like to go to Chennai sometime next week'. You fill a form: name in this box, train number in that box, date here. Fixed boxes mean the clerk can process it instantly. A JSON schema is that form.

Watch it happen

Getting JSON your code can use

Your app
Model
Validator

text + JSON schema

1/5The app sends the input text and the exact shape it wants: keys, types, required fields.

The big ideas

What you need to know

JSON mode vs schemas

JSON mode only promises valid JSON. Schema-based structured outputs, where supported, also enforce your keys and types.

Always validate

Even with strict modes, check the result in code. Types like dates and enums are easy to get subtly wrong.

Allow 'unknown'

Make fields nullable when the info might be missing, or the model may invent a value to fill the box.

Describe each field

Short descriptions like 'amount in rupees, number only' reduce mistakes more than you'd expect.

See it in code

Walk through the code

python
1# pip install pydantic2from pydantic import BaseModel, ValidationError3 4class Booking(BaseModel):5    name: str6    from_city: str7    to_city: str8    date: str          # YYYY-MM-DD9    passengers: int10 11raw = ask_llm(f"Return only JSON matching this schema: {Booking.model_json_schema()}\n"12              "Text: Book 2 seats for Ravi from Pune to Goa on 2026-12-20.")13try:14    booking = Booking.model_validate_json(raw)15except ValidationError as e:16    print("Retry with this error:", e)

1/3The schema: field names and types your app needs.

Define the shape once with Pydantic, send its schema in the prompt, and validate what comes back. ask_llm is your own helper.

Save yourself the pain

Common mistakes

Oops

Parsing the reply with json.loads and hoping keys exist.

Do this instead

Validate against a schema. Missing or wrong-typed fields should fail loudly, not crash later.

Oops

Making every field required.

Do this instead

If the source may not contain a value, allow null. Otherwise you're asking the model to make something up.

Your mission

Extract a booking

~30 min

Quick check

What does basic 'JSON mode' guarantee?

Go deeper

  1. 1Use a model's JSON mode / structured output feature to extract fields from unstructured text.
  2. 2Validate the output against a schema (Pydantic/Zod) and handle validation failures.
  3. 3Compare reliability of prompt-only JSON requests vs. native structured-output APIs.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge