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-filewith 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_reasonor 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