# JSON Mode vs Strict Structured Outputs

> The difference between JSON mode and strict schema structured outputs, why a valid format is not a correct answer, and what your code must still check, for your AI interview.

**In short:** JSON mode makes the model return valid JSON, with no set shape. Structured outputs take a JSON Schema you supply and hold completed replies to it, but your code still checks refusals, cut-off replies and business rules.

AI and LLM interview question Q086, Prompt and context engineering. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/json-mode-vs-strict-schema-structured-outputs

## Say it in 30 seconds

JSON mode makes a model return valid JSON, so the reply should parse, but the field names and types can still be wrong. Structured outputs go further: you give a JSON Schema, and a completed reply has to match it. Neither one checks your business rules. On a card dispute form, I check the amount against the transaction in code, and I retry a cut-off reply.

## Say it in 2 minutes

There are two ways to ask a model for JSON, and they promise different things. JSON mode keeps the reply to valid JSON, so it should parse, apart from edge cases your code must catch. But the field names and types can still drift from what your code expects. Structured outputs go further. You give a JSON Schema, and a completed reply has to match it, so required fields and enum values follow your schema.

Neither one checks the facts. A schema can say the amount is a number, but it cannot say the amount must stay under the original transaction. Refusals and replies cut off at the length limit also need handling in your code, and, with Anthropic's structured outputs, enum values can come back with different capitals.

On a card dispute form, I would use structured outputs where the vendor supports them, then run my own checks: the amount against the transaction, the dispute type, and a retry or a person when the reply fails. The schema keeps the shape tidy. The code decides what the card system is allowed to accept.

So JSON mode gives you data you can parse. A strict schema gives you data in the right shape. Your code still has to check it.

## What is the difference, in plain words?

JSON is a plain text format for data: keys, values and curly brackets that any program can read. Your app reads a reply with a parser, and a reply with a missing bracket makes the parser fail.

JSON mode is the basic setting. The model is kept to valid JSON, except in edge cases your code must detect and handle. A JSON Schema is a written contract for the shape: which fields exist, their types, and which are required. Structured outputs take that contract and hold completed replies to it.

Think of a bank's dispute form. JSON mode is a form the scanner can read. A strict schema is a form where each box takes only the right kind of answer. Neither form checks the figures against the account. That is the clerk's job, and in an AI system the clerk is your code.

- **JSON:** A text format for data, written as keys and values in curly brackets.
- **JSON Schema:** A written contract for a JSON reply: its fields, their types, and which are required.
- **JSON mode:** The basic setting: the reply is meant to be valid JSON, and nothing more about its shape.
- **Structured outputs:** A completed, non-refusal reply is held to a JSON Schema you supply.

## How do the two modes differ?

Both aim at the same goal: your code gets data it can read. They promise different things, so say exactly what each one covers.

- **JSON mode: Valid JSON only.** The reply should parse, but field names and types can still drift from what your code expects.
- **Structured outputs: Completed replies match.** Required fields and enum values follow the JSON Schema you supply.
- **Which to use: Structured, when you can.** OpenAI advises structured outputs over JSON mode when it is possible.

> "While both ensure valid JSON is produced, only Structured Outputs ensure schema adherence." (OpenAI)
>
> "Without structured outputs, Claude can generate malformed JSON responses or invalid tool inputs that break your applications." (Anthropic)

*Diagram, Two ways to get JSON, one check in code: JSON mode keeps the reply to valid JSON. Structured outputs also hold completed replies to your schema. Both still pass through your code, because neither knows your limits.*
1. JSON mode keeps the model to valid JSON, nothing more.
2. Structured outputs hold completed replies to the JSON Schema you wrote.
3. A JSON mode reply still needs its fields and rules checked.
4. A schema reply still needs the same checks in your code.
5. Only checked replies go on to the system.

## Where do replies still go wrong, and what does it cost?

A schema narrows the reply. It does not check the facts, and it does not cover every bad case. These are the gaps to name in an interview.

1. **The model refuses:** A refusal can come back instead of the schema, and it takes precedence over the schema. Check for it before you read any fields.
2. **The reply is cut off:** A reply that stops at the length limit may be incomplete and not match your schema. Treat it as a failure, and retry with more room.
3. **A word has the wrong capitals:** Enum values can come back with different capitals, such as Approved for approved. Anthropic's structured outputs do not guarantee capitalisation, so normalise in code or reject the reply.
4. **Your rules are not in the schema:** A schema cannot know your limits, like the original transaction amount or who may approve. Your code checks those.
5. **The first call is slower:** OpenAI and Anthropic both say the first use of a schema adds delay. Anthropic caches its compiled grammar for 24 hours from last use, so later requests are faster.

> "Your application still needs to handle refusals, incomplete responses, and API errors, and validate any business rules that the schema does not express." (OpenAI)
>
> "The output might be incomplete and not match your schema." (Anthropic)

Here is the check in plain Python. The model is not run. These are four replies a dispute bot might send, and the code decides what passes.

```python
# A card dispute bot proposes one form. The model is not run here;
# these are four replies it might send, and the code decides.
import json

TRANSACTION_AMOUNT = 120.00  # from the card system, never from the model
DISPUTE_TYPES = {"fraud", "duplicate", "not_received"}

REPLIES = [
    '{"transaction_id": "T-88121", "dispute_type": "fraud", "amount": 120.0}',
    '{"transaction_id": "T-88121", "dispute_type": "Fraud", "amount": 120.0}',
    '{"transaction_id": "T-88121", "dispute_type": "fraud", "amount": 540.0}',
    '{"transaction_id": "T-88121", "dispute_type": "fraud", "amou',
]

def check(reply):
    try:
        data = json.loads(reply)
    except json.JSONDecodeError:
        return "REJECT: cut off or not JSON, retry or ask a person"
    if not isinstance(data, dict):
        return "REJECT: not an object"
    if data.get("dispute_type") not in DISPUTE_TYPES:
        return "REJECT: unknown dispute type " + str(data.get("dispute_type"))
    amount = data.get("amount")
    if not isinstance(amount, (int, float)) or amount > TRANSACTION_AMOUNT:
        return "REJECT: amount missing or above the transaction"
    return "OK: dispute for " + str(amount)

for reply in REPLIES:
    print(check(reply))
```

Output:

```text
OK: dispute for 120.0
REJECT: unknown dispute type Fraud
REJECT: amount missing or above the transaction
REJECT: cut off or not JSON, retry or ask a person
```

*Diagram, Where a neat reply can still fail: A schema match does not finish the job. Cut-off replies, refusals and wrong capitals go back for a retry or to a person. A reply that fits the schema still gets your business rules checked before anything uses it.*
1. A reply with one of the known problems goes this way.
2. A reply with the right fields and types moves on.
3. A bad reply is retried, or passed to a person.
4. A reply that fits the schema still gets its rules checked.
5. Only a reply that passes every check is used.

### How would you choose for a card dispute form?

A card dispute bot fills in a form: the transaction, the dispute type and the amount. The card system accepts only up to the original amount, and the form is filed with it straight away.

- **Plain JSON, no checks: Parses, then breaks later.** A capital letter or a sum above the transaction reaches the card system as if it were fine.
- **Schema and code checks: Bad replies are caught.** The schema fixes the shape. Your code checks the amount against the transaction before anything is filed.

> **Tip:** Say that the schema is not the last check. The amount rule lives in your code, where the model cannot change it.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] JSON mode means valid JSON. Structured outputs also hold completed replies to a schema you supply.
- [ ] A schema fixes the shape, not whether the values are true.
- [ ] Refusals, cut-off replies and business rules still need code.
- [ ] Name the vendor feature you would use, and what it does not promise.
- [ ] Mention the cost: structured outputs add delay on a schema's first call.

**Common traps**

- Trap: "JSON mode means the data is correct." Better: It means the reply should parse. The field names and values can still be wrong.
- Trap: "A schema stops the model making things up." Better: A schema fixes the shape. A made-up number still fits a number field.
- Trap: "The schema handles every error." Better: Refusals, cut-off replies and business rules need checks in your code.

## Can you spot the gap?

**Spot the gap:** Your schema says amount is required and must be a number. Which reply can still do harm?

A. {"transaction_id": "T-88121", "dispute_type": "fraud"}
B. {"transaction_id": "T-88121", "dispute_type": "fraud", "amount": 540.0}
C. {"transaction_id": "T-88121", "dispute_type": "fraud", "amount": "one hundred twenty"}
D. {"transaction_id": "T-88121", "dispute_type": "fraud", "amount": 120.0}

**Answer:** B. It fits the schema, since amount is a number. But the original transaction was 120.00, so your code must reject it. A schema checks the shape, not your limits.

**Quick check:** Your dispute bot is moving to structured outputs. What else does it need to be safe?

A. A longer system prompt
B. A bigger model
C. Code checks for amounts, refusals and cut-off replies
D. A lower temperature

**Answer:** C. The schema fixes the shape, and code checks what the schema cannot see. A longer prompt, a bigger model or a lower temperature does not change what the card system will accept.

## Can you answer these from memory?

**Flash cards**

- **What does JSON mode promise?** Valid JSON. It does not check your field names, types or business rules.
- **What does a strict schema add?** Completed, non-refusal replies follow your JSON Schema, so required fields and enum values match it.
- **What does OpenAI advise?** Use structured outputs instead of JSON mode when possible.
- **Is a schema match enough?** No. Refusals, cut-off replies, wrong capitals and business rules still need code.
- **Why check the amount in code?** A schema sees a number. It cannot see the transaction the amount must stay under.

## Likely follow-up questions

- Q085 How do you get reliable JSON output? https://designervenkat.online/interview/ai/how-to-get-reliable-json-from-an-llm
- Q234 How should an app handle refusals? (full answer coming)
- Q478 Parse and validate LLM JSON, retrying on failure. (full answer coming)
- Q448 How do you extract structured data from documents and images? (full answer coming)
- Q176 How should an agent handle tool errors and retries? https://designervenkat.online/interview/ai/how-should-an-agent-handle-tool-errors-and-retries

## Learn it in depth

- [Structured Output](https://designervenkat.online/learn/ai-engineering/structured-output)
