DEVSAIO

Getting reliable JSON from AI models

Techniques for structured output: JSON mode, schemas, validation and retries, so your code never chokes on a malformed answer.

DEVSAIO team · 2 min read
Share on X

When AI output feeds into code (saving to a database, filling a form, calling another API), you need valid JSON every time. Here's how to get there.

1. Ask clearly, with an example

text
Extract the order details. Reply with JSON only, no other text, in this shape:
{"customer": string, "items": [{"name": string, "qty": number}], "total": number}

Showing the exact shape works better than describing it.

2. Use JSON mode where supported

Many models accept response_format:

python
r = client.chat.completions.create(
    model="gpt-5.6-luna",
    response_format={"type": "json_object"},
    messages=[{"role": "system", "content": "Reply with JSON only."}, {"role": "user", "content": text}],
)

Support varies by model and provider. If a model ignores or rejects response_format, rely on the prompt plus validation.

3. Use tool calling as a schema

A trick that works across most models: define a tool whose parameters are the schema you want, and ask the model to call it. The arguments come back as JSON matching your schema.

4. Always validate

python
import json
from pydantic import BaseModel, ValidationError

class Order(BaseModel):
    customer: str
    items: list[dict]
    total: float

def parse(text: str) -> Order | None:
    text = text.strip().removeprefix("```json").removesuffix("```").strip()
    try:
        return Order.model_validate(json.loads(text))
    except (json.JSONDecodeError, ValidationError):
        return None

5. Retry once with the error

If parsing fails, send the model its own output and the validation error, and ask it to fix the JSON. One retry fixes almost every case.

6. Keep temperature low

For extraction, use temperature: 0 or close to it. Creativity is the enemy of consistent structure.

Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading