JSON that parses on Monday and breaks on Friday

L2 · PractitionerDebug~7 minLLM foundations

Your agent asks the model for a JSON object with action, args and reason. It worked in testing. In production, about 1 in 200 responses fails json.loads: a trailing comment, a missing brace, or prose before the JSON. Another 1 in 100 parses but has args as a string instead of an object. How do you make this reliable?

Answer it out loud or on paper first. Then open the short answer.

Short answer

Treat model output as untrusted input. Use the provider's structured output or native tool calling with a JSON schema so the format is constrained at decoding time. Then validate against the schema anyway (types, required fields, enums), because valid JSON isn't valid data. When validation fails, send the specific error back to the model and retry a bounded number of times; after that, fail the step cleanly. Measure the failure rate per model and prompt version.

Full answer

Layer the defences

LayerCatchesDoesn't catch
Prompting ("respond only with JSON")Most formatting driftRare failures; it's a request
JSON modeInvalid JSONWrong shape: missing fields, wrong types
Structured outputs / tool calling with a schemaShape violations, at decoding timeWrong values (an id that doesn't exist)
Schema validation in your code (Pydantic, JSON Schema)Anything the provider let through; version driftSemantic errors
Semantic checksUnknown tool names, ids not in the DB, amounts over limitsNothing left at this layer, if you wrote the checks

The retry loop

On failure, append an observation like error: args must be an object; got string and let the model try again. Errors as observations work because the model can usually fix a specific complaint. Cap retries (2–3) so a confused model can't loop.

Lenient parsing, carefully

Stripping code fences or text before the first { is fine. Repairing arbitrary broken JSON is risky: you may "fix" it into something the model didn't mean. Prefer constrained decoding plus a retry.

Measure

Track parse-failure and validation-failure rates by model, prompt version and schema. A jump after a model update is a regression you want to see in a dashboard, not in a support ticket.

Follow-ups

  1. L2. Why does args come back as a string sometimes?

    Expected answer

    The model serialised the object into a string (double-encoding). The schema should require an object; a validator catches it and the error message tells the model exactly what to change.

  2. L3. Structured output guarantees the schema. Why still validate?

    Expected answer

    Provider features have limits (unsupported schema keywords, fallback paths), versions drift, and you may switch providers. Validation is cheap and catches it at your boundary.

  3. L3. When would you not retry?

    Expected answer

    When the call has side effects that may have happened, when the error is semantic and repeated, or when latency budgets are tight; then fail the step with a clear error.

Wrong answers interviewers hear
  • "Add 'ONLY output valid JSON' in capitals." Prompts reduce the rate; they don't make it zero.
  • "Use a regex to extract the JSON and fix it up." Repair can silently change meaning; constrain and validate instead.
  • "JSON mode solves it." JSON mode guarantees syntax, not your schema.

Taught in

Improve this answer