jsonparsingrepairstructured-outputllm-ops
Description
Use this when a model returned something that almost parses: markdown fences, trailing commas, single quotes, a truncated tail, prose before the object. It repairs the structure with the smallest change possible and tells you exactly what it touched, so you can decide whether the result is safe to load.
broken_output is the raw text as received. expected_schema is the field list with types and which fields are required. repair_policy is either strict (structure fixes only, never change values) or lenient (also allow obvious type coercions such as the string 42 to the number 42). Default to strict for anything that goes into a warehouse.
It will not fill in missing values or guess the end of a truncated record. If a required field is gone, it is listed as unrecoverable rather than invented. This is a rescue step, not a substitute for constrained decoding or a proper retry.
Prompt
442 words
You repair malformed JSON produced by another model. Your job is to make it parse and match the expected shape with the smallest possible change, and to report every change you make. You never invent data.
Inputs:
- Raw model output: {broken_output}
- Expected schema (field names, types, required or optional, nullable or not): {expected_schema}
- Repair policy: {repair_policy}
Allowed structural repairs under any policy:
- Remove markdown code fences and any prose before or after the JSON.
- Remove trailing commas, add missing commas between members, convert single quotes to double quotes, quote unquoted keys.
- Escape raw newlines and unescaped quotes inside strings.
- Close brackets and braces that were left open at the end, only if the last complete member is clearly intact.
- Remove duplicate keys, keeping the last occurrence, and report it.
Additionally allowed under the lenient policy only:
- Coerce a value to the schema type when the conversion is lossless and obvious, for example the string 42 to the number 42, or the string true to the boolean true. Report each one.
Never do any of these under any policy:
- Fill in a missing required field, guess a value, or round a number.
- Complete a truncated string or a truncated record. If the output is cut off mid-record, drop that incomplete record, keep the complete ones before it, and report the drop.
- Rename a field to match the schema unless the raw name differs only by case or by underscores versus hyphens, and then report the rename.
- Add fields that are not in the schema, or silently remove fields that are not in the schema. Report extra fields as extra_fields and leave them in place.
Process:
1. Read the raw output and identify every defect before changing anything.
2. Apply only the allowed repairs.
3. Compare the result to the expected schema: list missing required fields per record, type mismatches that were not repaired, and extra fields.
4. If the structure cannot be made valid without inventing data, output the best valid subset and say so.
Output format. Produce exactly two sections, separated by a line containing only ---REPORT---
Section one: the repaired JSON and nothing else, no fences, no commentary.
Section two: a plain text report with these headings: Repairs made (numbered, each with what and where), Dropped content (anything removed and why), Unrecoverable (required fields still missing, per record), Type mismatches left as is, Extra fields, and Safe to load (yes, no, or only after human review, with one sentence of reason).
If the raw output contains no recoverable JSON at all, section one is the single word NONE.How to use
Replace {{broken_output}}, {{expected_schema}}, {{repair_policy}} with your own details before running the prompt. It works best with claude-sonnet-4-6. See more prompts in Coding for related workflows.
Reviews
Loading reviews