Prompt Engineering Structured Output JSON

Structured output means asking an LLM to answer in a machine-readable format such as JSON so your code can use the result directly. It is the foundation of most AI features in real applications.

Prompting for JSON

Extract the event details from the message below.
Return only JSON with keys: title (string), date (YYYY-MM-DD), city (string or null).
No extra text.

Message: "Join our Python meetup on 14 November 2026 in Karachi!"
Sample response:
{"title": "Python meetup", "date": "2026-11-14", "city": "Karachi"}

Use Schema Features When Available

Most providers and local runtimes (including Ollama) offer a JSON mode or a structured-output setting that accepts a JSON Schema and constrains the model to follow it. Prefer these over prompt-only JSON when you can; they remove most formatting failures.

{
  "type": "object",
  "properties": {
    "title": { "type": "string" },
    "date": { "type": "string", "description": "YYYY-MM-DD" },
    "city": { "type": ["string", "null"] }
  },
  "required": ["title", "date", "city"]
}
(passed as the response format / schema parameter of your API)

Validate and Retry

Even with schemas, validate values in code. Here is a model-agnostic pattern in Python:

import json

def parse_event(raw: str) -> dict:
    data = json.loads(raw)                 # raises if not valid JSON
    assert isinstance(data["title"], str)
    assert len(data["date"]) == 10         # simple format check
    return data

raw = '{"title": "Python meetup", "date": "2026-11-14", "city": "Karachi"}'
print(parse_event(raw))
{'title': 'Python meetup', 'date': '2026-11-14', 'city': 'Karachi'}

If parsing fails, retry once with the error message added to the prompt ("Your last reply was invalid because ..."), then fall back gracefully.

Tips for Reliable JSON

  • Give field names, types and allowed values explicitly.
  • Allow null for missing data so the model is not forced to invent.
  • Include one example JSON object for complex shapes.
  • Use a low temperature.
  • Put reasoning, if needed, in a separate field before the answer field.

Frequently asked questions

Why does the model add text around the JSON?

It is trying to be helpful. Say "Return only JSON", use a JSON mode, or strip code fences before parsing.

Is JSON mode the same as structured outputs?

JSON mode guarantees valid JSON; structured outputs also enforce your schema. Use structured outputs when available.

Can LLMs output CSV or XML instead?

Yes, but JSON is easiest to validate and most providers support it natively.