# Mission 12.2: We need guaranteed JSON

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/einsatz-12-2

Belongs to [Chapter 12: Integrations](https://llm.yoga/en/12-integrationen.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Paul**, developer:

> “We need to turn customer emails into guaranteed JSON for our calendar. The tutorial says: create a tool ‘Save appointment’ and force Claude to call it. Claude Opus 5.5 rejects that with error 400. Locally with Ollama and a schema, I did get valid JSON, but the date in it was ‘2023-12-03’. What’s broken here?”

JSON is a fixed text format of fields and values that programs can read directly. A schema defines which fields must be there and what they can contain, such as text or a whole number. Error 400 means the API rejects the request. Ollama is a program Paul uses to run models on his own computer. Two problems show up together here: the first is about the way to the JSON, the second about what’s in it.

**Question 1 (decision):** How does Paul reliably get JSON with Claude Opus 5.5?

- A) Force “any tool” instead of the one specific tool.
- B) Structured output: the response follows a fixed schema.
- C) Write in the prompt: “Answer only with JSON.”

## What’s behind it

Think of a form with fixed boxes. It makes sure that the box “Date” contains something that looks like a date. The form doesn’t know whether it’s the right date.

That’s how **structured output** works. Claude gets a schema, and the whole response follows it; for tool calls, the same thing exists as a strict setting on the tool. The technique behind it is called constrained decoding. A model writes token by token, that is, in word pieces. Before the next token is drawn, the software removes every token that would violate the schema at this point. Where a number has to go, the word “two” has a probability of zero.

The old trick of forcing Claude to use a specific tool no longer works: the new Claude models reject it with error 400. The docs don’t give an explicit reason. But a forced call starts without a thinking block, the part where the model thinks before answering, and Opus 5.5 always thinks.

For Paul, this means he fixes error 400 with structured output. The year 2023, on the other hand, isn’t a schema error. The schema guarantees the form, not the content. The model didn’t know what year it is and guessed; on the test Mac, the MacBook I use for every measurement in this course, a small and a medium model both did the same. The fix works on two levels: today’s date in the prompt, and a check in the program that the date is valid and not in the past before it goes into the calendar.

### In more detail: the names in the API and what came out on the test Mac

In the Claude API, structured output is called `output_config.format`, with a JSON Schema. For tools, there’s `strict: true` on the tool. `tool_choice` controls whether Claude has to use a tool: `auto` lets Claude decide; `any` (any tool) and `tool` with a name force the call and result in error 400 with Opus 5.5, Sonnet 5.5 and Fable 5.2. In the schema, `required` sets the required fields, `"type": "integer"` whole numbers and `enum` the allowed values.

Schema with the required fields titel, datum, uhrzeit and ort (title, date, time, place), request „Lege einen Termin an: Zahnarzt am 3. Dezember um 9:30 Uhr in Hamburg.“ (German for “Create an appointment: dentist on December 3 at 9:30 a.m. in Hamburg.”). Without today’s date, the result was `2023-12-03` three times, with three different seeds: a stable guess.

With „Heute ist der 25.11.2026.“ (“Today is November 25, 2026.”) at the start of the request, one run returned `2026-12-03T09:30:00`: the right year, but with a time, even though the field had the description „Format JJJJ-MM-TT“ (format YYYY-MM-DD). In another attempt, the field held `25.11.2026`, today’s date instead of the appointment. `2026-12-03` came out only when the format hint was at the end of the request. Each time, the check in the program caught the wrong value.

The Ollama docs recommend also putting the schema into the prompt as text. Whatever is only in the schema forces the form but doesn’t necessarily explain to the model what is meant.

## Try it

**Sorting: who takes care of what?**

**Task:** Paul has noted eight wishes and ideas for his appointment import. Sort each one: does the schema guarantee it, does his program have to check it, or does Opus 5.5 reject it with error 400?

Categories: the schema guarantees it · the program checks it · error 400

1. The field “datum” (date) is always there and contains text.
2. The year in the date is right.
3. Force Claude to call exactly the tool “Save appointment”.
4. The type is „termin“, „erinnerung“ or „aufgabe“ (appointment, reminder or task), nothing else.
5. Locally with Ollama: the date appears as YYYY-MM-DD, even though the schema only specifies “text” and a description.
6. The duration in minutes is a whole number, never the word “two”.
7. Force Claude to call some tool, no matter which.
8. The appointment isn’t in the past.

## Check

**Question 2 (check question):** Paul switches to structured output. Does he still have to check the values?

- A) No, with structured output the result is guaranteed to be correct.
- B) Yes, because the schema guarantees the form, not the values.
- C) Only with small local models; Claude doesn’t guess years.

**Question 3 (check question):** Claude should decide on its own whether an email contains an appointment at all, and only save it then. What does Paul use?

- A) Force Claude to use the tool “Save appointment” so the call is sure to come.
- B) Offer the tool with a strict schema; Claude decides whether to save.
- C) Structured output for the whole response with the appointment schema.

**Question 4 (check question):** What helps most against the wrong year?

- A) Set the temperature to zero, then the model stops guessing.
- B) Write today’s date in the prompt and check in the program.
- C) Use a bigger model; it knows the current year.

## Done

**What you can do now**

- With the new Claude models, you choose the way to JSON: structured output or a tool with a strict schema, not a forced tool.
- You separate form and content: what the schema describes is guaranteed; whether the values are right is not.
- If a customer asks for “guaranteed correct JSON” from their emails, you ask who checks the values before they go into their system, and whether today’s date is in the prompt.

### Two cards for review

**Card 1:** True or false? “With structured output, the values are guaranteed to be right.”

**Card 2:** When do you use a tool with a strict schema for JSON from Claude instead of structured output for the whole response?

- [Next mission: MCP, n8n or a direct call for the CRM note?](https://llm.yoga/en/einsatz-12-3.md)
- [Try it on your Mac](https://llm.yoga/en/mac-12-2.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [Tool choice (tool_choice)](https://llm.yoga/en/12-integrationen-2.md)
- [Structured output](https://llm.yoga/en/12-integrationen-3.md)
- [Exercise: a tool call with a local model](https://llm.yoga/en/12-integrationen-6.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is B.
- A: Opus 5.5, Sonnet 5.5 and Fable 5.2 reject that too, also with error 400. Forced is forced.
- B, correct: Then the whole response follows the appointment schema. If you want Claude to decide on its own whether to save anything at all, a tool with a strict schema that Claude calls voluntarily also works. The docs recommend both instead of forcing.
- C: A request is not a guarantee. The model can still add a sentence before the JSON, forget a field or deliver “two” instead of 2.

- Card 1: **the schema guarantees it:** If the field is required in the schema and defined as text, constrained decoding allows nothing else.
- Card 2: **the program checks it:** Good form, guessed content: on the test Mac, the result was 2023-12-03 for an appointment in December 2026. Only the program can check that.
- Card 3: **error 400:** Opus 5.5, Sonnet 5.5 and Fable 5.2 reject a forced call. Older tutorials are out of date here.
- Card 4: **the schema guarantees it:** If the schema defines exactly these three values, only they are allowed. That takes the guessing away from the model.
- Card 5: **the program checks it:** A description is a hint, not a rule. On the test Mac, the results were “25.11.2026” and “2026-12-03T09:30:00”, both valid text.
- Card 6: **the schema guarantees it:** If the field is defined as a whole number, only digits are allowed at this point. Anthropic’s docs give a booking example where, without the strict setting, you might get “two” instead of 2.
- Card 7: **error 400:** “Any tool, but one of them” is forced too, and with the new models it results in error 400.
- Card 8: **the program checks it:** That’s a question of content. A schema can define types and allowed values, but it can’t know what day it is today.

**Question 2:** The correct answer is B.
- A: The form is guaranteed. Nobody guarantees that the date, time and place are right.
- B, correct: The program checks the date, time and place. If an appointment has consequences, for example it goes to customers or books a room, a human should confirm it as well.
- C: Through the API, Claude also only sees what’s in the request. If today’s date matters, it belongs in the prompt.

**Question 3:** The correct answer is B.
- A: That gives error 400 with Opus 5.5. And forced would mean a call even for emails without an appointment.
- B, correct: Claude only calls it when it finds an appointment, and with the strict setting the inputs are guaranteed to match the schema. A clear instruction in the prompt helps on top of that.
- C: Then every response is an appointment, even for an email without one. Claude would no longer have the “save or not” decision.

**Question 4:** The correct answer is B.
- A: Less randomness doesn’t change the fact that the model doesn’t know the year. On the test Mac, three runs gave 2023 three times: it guesses wrong consistently.
- B, correct: The model only knows what’s in the request. With the date in the prompt, the year was right, and the check in the program catches whatever still goes wrong.
- C: A bigger model also only sees what’s in the request. On the test Mac, the medium model got the year just as wrong.

**Card 1:** **False.** What’s guaranteed is the form: fields, types, allowed values. In my test, a small local model dutifully wrote “2023-12-03” for an appointment on December 3: perfect format, wrong year. The fix: today’s date in the prompt, and checking the values in the program.

**Card 2:** When Claude should decide on its own whether to save anything at all, for example only for emails that contain an appointment. Then it calls the tool only when needed, and the strict setting guarantees the form of the inputs. Structured output, by contrast, turns every response into an appointment. With the new Claude models, you can no longer force a tool (error 400).
