aifree.dev

Why does it break my JSON?

Structured output (constrained decoding) · Builder · 5 min · sampling, llm · after Next-token prediction, Temperature and sampling

A model writes JSON one token at a time, so nothing stops a token that breaks it. Structured output modes give the API your schema and block every token that cannot fit; values still need checking.

Before you start: could you explain this to a friend?

1 = not at all · 5 = easily

  1. 1 Guess first

    You ask for JSON and the reply starts with “Sure! Here is your JSON:”. Why?
  2. 2 Look

    One token at a time

    1 / 4

    Prompt: “Return the user as JSON with name and age.”

    Sure! Here is the JSON:
    ```json
    {"name": "Ada", "age": thirty-six}
    ```

    JSON.parse → SyntaxError

    illustrative failure, not a recorded output

    Asked for JSON, it adds a friendly sentence and writes thirty-six. The parser gives up.

    Each token is a choice among likely options, some of which break JSON; with a schema, the API removes those options before the pick, so the format holds but the values still need checking.

  3. Constrained decoding means the API knows your schema and, at every step, allows only tokens that can still lead to valid output. Anthropic and Gemini describe their structured outputs this way. OpenAI separates JSON mode (valid JSON, any shape) from Structured Outputs, which follow your schema.

  4. 4 You are the model

    You are the filter. The schema is fixed; at each step, pick the one next token it allows.

    schema: {name: string, age: integer, plan: "free" | "pro"} · no extra keys

    (empty)▍

    candidate tokens illustrative

    You are the filter: which next token does the schema allow?
  5. 5 Check

    Schema says age is an integer. Which next token does constrained decoding block?
  6. 6 Say it back

  7. 7 Mission · 5 min

    With a free Gemini API key, ask Gemini 3.8 Flash to extract a recipe twice: once with a JSON schema in response_format (as in Gemini’s structured-output docs), once just asking for JSON in the prompt. Parse both. Then add maxLength to a field and check whether it is enforced.

Three quick ones

Drawn at random from this lesson's pool; different next time.

Done?

Now: could you explain this to a friend?

1 = not at all · 5 = easily

Answer the three quick ones first.

Sources

  • https://ai.google.dev/gemini-api/docs/structured-output (updated 2026-10-20, checked 2026-10-21): response_format with mime_type application/json and a schema; examples use gemini-3.8-flash; “While output is syntactically correct JSON, always validate values in your application”; minLength, maxLength, minProperties, maxProperties “not enforced during constrained decoding and are ignored by the server”
  • https://developers.openai.com/api/docs/guides/structured-outputs (checked 2026-10-21; no update date): “only Structured Outputs ensure schema adherence” (not JSON mode); text.format json_schema with strict: true; available “starting with GPT-4o”; refusals in a separate field
  • https://platform.claude.com/docs/en/build-with-claude/structured-outputs (checked 2026-10-21; no update date): output_config.format json_schema; constrained decoding on the listed models (e.g. claude-sonnet-4-6, claude-haiku-4-5); refusal and max_tokens stops may not match the schema; enum values may differ in capitalization
  • https://ai.google.dev/gemini-api/docs/pricing (updated 2026-10-19, checked 2026-10-21): Gemini 3.8 Flash input free of charge on the free tier
  • The broken answer and the token odds in the picture are illustrative, labelled as such

Published Oct 21, 2026 · updated Oct 21, 2026

Embed this

Paste this into a blog post or course page: the lesson's main picture in an iframe, with a link back to the full lesson.

<iframe src="https://aifree.dev/embed/explain/structured-output" width="100%" height="620" style="border:0;max-width:720px" title="Why does it break my JSON? (aifree.dev Explain)" loading="lazy"></iframe>
<p><a href="https://aifree.dev/explain/structured-output">Why does it break my JSON? Interactive lesson on aifree.dev</a></p>

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel
Join @aifree_dev and get new free AI offers and news right in your Telegram feed.
Browser notifications
Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.
Email newsletter
A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

+

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer