Search⌘ K
AI Features

Repair Invalid JSON with a Targeted Re-Ask

Explore techniques to repair invalid JSON output in AI workflows by constructing focused re-ask prompts that fix schema violations without altering underlying meanings. Learn to enforce minimal changes and handle missing data safely to improve automation reliability.

When a model returns structurally valid JSON with a type mismatch, for example, "needs_human": "false" instead of the boolean value false, a strict validator will reject the output. Re-running the full triage prompt on the original email consumes additional tokens and may introduce new classification errors. A better option is often a targeted repair step: a focused correction prompt that treats the invalid JSON as input and fixes only the schema violations without changing the underlying classification. In this lesson, we will explore how to construct minimal-change correction prompts that repair schema-invalid output while enforcing explicit stop conditions when required data is missing.

The table below shows four common ways model output breaks, with an example of each and how a strict parser or schema validator responds. The first two fail at parsing, and the last two parse but fail schema validation:

Common JSON Output Failures
Failure classExampleWhat a strict validator does

Trailing commentary

{...} // extra note

Parse fails, extra data after the JSON object

Unquoted keys

{category: "bug", ...}

Parse fails, keys must be quoted strings

Wrong boolean type

"needs_human": "false"

Parses fine, schema check fails: expected boolean, got string

Enum violation

"priority": "urgent"

Parses fine, schema check fails: "urgent" is not an allowed value

Do you find this helpful?

The pattern to keep in mind is simple. When validation fails, a targeted re-ask treats the invalid output as data, requests a corrected version, and forbids extra prose so the next system step can parse it.

Build a correction re-ask prompt

With the symptom identified, the next move is to write a re-ask that ...