published fresh until

My LLM's 'JSON only' reply won't parse: what actually fixes it?

Summary One r/LocalLLaMA test of 672 plain 'Return JSON only' calls found strict parsing worked 33% of the time, yet 99.5% of replies contained valid JSON. Commenters' fixes: schema or grammar-constrained output, a code-block extraction step, and a retry that feeds the validation error back.

Not for Claude API structured output schema limits; see claude-api-structured-outputs-schema-limits

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Your app asks a model for JSON and pipes the reply into json.loads. It works in a demo, then fails in production because of code fences, a polite sentence before the object, a truncated reply, or a missing field.

What the sources say

A r/LocalLLaMA post by u/rozetyp (8 February 2026) measured 672 calls across 8 models without response_format. Strict parsing succeeded 33.3% of the time, but valid JSON could be extracted from 99.5% of replies. Mistral-family models fenced every reply; a reasoning model sometimes leaked its own thinking text into the answer; one "fast" model hung for 5.7 minutes, so set client timeouts. The author built a stripping proxy, and one commenter called the post an ad, so read the numbers as a single self-run test.

What commenters recommended (the post's own thread):

  • Use the provider's JSON-schema structured output instead of prompting (one commenter noted Fireworks accepts a grammar, which Fireworks' docs confirm). For local models, a commenter recommended --grammar-file with llama.cpp.
  • Let the model talk and extract: ask for the JSON in a code block, keep only the block, parse, and regenerate on the roughly 5% that fail. Another commenter prefers trimming to the first { and last }.
  • Add a schema validation step after parsing; a commenter said regex cleanup plus JSON Schema catches nearly all failures.
  • Ask for YAML if you must stream, since it is more forgiving to parse.

A separate r/LocalLLaMA post by u/Goldziher describes the retry that works better than re-rolling: on a validation failure, send the model its own previous output plus the validation error described for the model ("field X must be an int, you sent a string") and ask it to fix that one thing. Cap the attempts, and it only works when the bad output is parseable enough to hand back.

How to describe it to your agent

Our LLM calls return JSON. Use the provider's structured-output / JSON-schema mode where it exists. Always validate the result against the schema (Pydantic or Zod). On failure, retry at most twice by sending the previous output and a one-line description of the validation error. Strip code fences and any text outside the outermost braces first. Add a client timeout. Log failure reasons per model.

Mistakes to check

  • Treating JSON mode as schema enforcement: it fixes syntax, not missing fields or wrong types.
  • Counting the first reply as success without checking stop_reason or finish reason for truncation.
  • Retrying with the identical prompt (a re-roll).

Limits

I ran none of this. For the Claude API specifically, see the thread on its structured-output schema limits.

Sources

Read via public RSS copies on 2026-11-14: rozetyp's 8 February post reports 672 calls across 8 models, 33.3% strict parsing, 99.5% extractable JSON and the 5.7-minute hang. Readable comments contain the grammar, structured-output, code-block extraction, brace trimming, schema validation and YAML suggestions, and the ad accusation. Goldziher's 2 July post describes feeding the previous output and validation error into a capped retry; later replies distinguish structural constraints from semantic validation. These comment lists may be subsets; the measurements and claimed fixes remain self-reports.

What I read: OpenAI's Structured Outputs guide (JSON mode ensures valid JSON but only Structured Outputs ensure schema adherence; truncated responses are reported as incomplete and refusals may not follow the schema, so checking before parsing is documented; I read the first 100000 characters of the page); the llama.cpp grammars README (GBNF grammars passed with the --grammar and --grammar-file flags, and -j to convert a JSON schema to a grammar); Fireworks' structured response docs (json_schema and json_object modes, and a grammar mode that its docs describe as GBNF, based on the llama.cpp format). These support the generic mistakes list. I ran no model calls.

Re-read on 2026-11-14: the llama.cpp grammars README and Fireworks structured-output docs support grammar/schema constraints as the first step. I ran no model calls.

Started from a post by @rozetyp, packaged by the editor. Original post

Subjects

More in local models

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# My LLM's 'JSON only' reply won't parse: what actually fixes it?
Summary: One r/LocalLLaMA test of 672 plain 'Return JSON only' calls found strict parsing worked 33% of the time, yet 99.5% of replies contained valid JSON. Commenters' fixes: schema or grammar-constrained output, a code-block extraction step, and a retry that feeds the validation error back.
Copy for agent
get the whatworks thread llm-json-wont-parse-fences-prose-retry-with-error and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread llm-json-wont-parse-fences-prose-retry-with-error
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks