When it breaks

It returns JSON most of the time, then wraps it in prose and breaks my parser

Also asked as the model added text around the JSON · invalid JSON from the API · it invented a field that isn't in my schema · my parser crashes randomly

Cause documented by a vendor or a published paper Not tested by benchr First logged Last checked

What is actually happening

You asked for JSON in words. Words are a request, not a constraint, so the model complies at whatever rate it complies.

Why

  • Free-form prompting for JSON has no enforcement behind it. Retry loops around it are treating the symptom.
  • Schema-constrained decoding exists and is documented: you supply the schema and the response is constrained to it.
  • Even with the schema enforced there are documented escape hatches — a refusal, a safety rejection, or a response cut off at the token limit — so valid schema does not mean completed response.

The quick fix

Stop asking for JSON and start supplying a schema. The API feature is called structured outputs and it is a request parameter, not a prompt trick.

The real fix

Supply a flat schema, handle the three documented escape hatches explicitly, and still validate values at the boundary. The shape is guaranteed; the content is not.

Step by step

  1. Write the schema first, and keep it flat — deep optional nesting is where the unsupported features live.
  2. Confirm the model you are calling supports it before assuming the parameter is honoured.
  3. Branch on refusal, safety rejection and truncation separately from parse failure.
  4. Validate the values, not just the shape. A correctly shaped object can still be wrong.
  5. Never send user-generated text that has nothing to do with the schema — the documentation warns the model will fill it anyway, which is a hallucination path rather than a parse error.

If you are seeing an API error

Grounded in

Where this leads