Structured Output (JSON)

Gen AI · Prompt Engineering

Theory

Structured output means asking a model to reply in a fixed, machine-readable shape, usually JSON with named fields, instead of free text, so your code can parse the answer and load it into a table. A typical prompt describes the exact fields, their types and allowed values, for example sentiment as one of "positive", "negative" or "neutral" and issue as a short string or null, and gives one example of valid output.

Many model APIs also offer a JSON or schema mode that constrains the reply to match a schema you supply, which is more reliable than wording alone. Either way, always validate in code: parse the JSON, check required fields and allowed values, and retry or flag the row when validation fails. This pattern turns messy text such as reviews, emails or tickets into clean columns you can query with SQL.

Why we use it

Free-text answers are hard to use in pipelines: formats drift and parsing breaks. A fixed schema lets you treat the model like a function that returns typed fields you can store, join and aggregate.

When to use it

Use it whenever model output feeds code, a database or a dashboard: extraction, classification, tagging and tool arguments. For answers that a person reads directly, natural prose is usually fine.

How it works

  1. Define the fields, types and allowed values
  2. Put the schema and one valid example in the prompt
  3. Send each text record with low temperature
  4. Parse the reply as JSON in code
  5. Does it parse and validate? Yes: Load the fields into the table. No: Retry once, then flag for review.
  6. Query the new columns with SQL

Example

import json

ALLOWED = {"positive", "negative", "neutral"}

prompt = (
    "Return only JSON with keys sentiment (positive|negative|neutral) "
    "and issue (short string or null).\n"
    "Review: Late, box damaged"
)

reply = call_llm(prompt, temperature=0)   # pseudo-call
# reply -> '{"sentiment": "negative", "issue": "delivery"}'

data = json.loads(reply)                 # fails loudly if not JSON
assert data["sentiment"] in ALLOWED      # validate before loading
print(data["issue"])                     # delivery

Key points

Gen AI topics

Foundations of Generative AI

Prompt Engineering

Retrieval & Embeddings

Customizing Model Behavior

AI Agents & Tool Use

Multimodal & Applied AI

Ethics, Cost & Careers

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Generative AI for the Data Stack

LLMs, prompt engineering, RAG, embeddings, agents and the tools shaping modern AI products — explained practically. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Section {{ sec.num }}

{{ sec.label }}

Gen AI  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

💡

{{ current.tldr }}

Explained

{{ para }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
See It In Action
{{ current.example }}
Key Takeaways — click to check off
{{ pt.mark }} {{ pt.text }}

Share this topic