Skip to content

Prompt Tutorials 3 min read

Self-Correcting JSON Repair: Prompts that Fix Truncated, Malformed, and Schema-Violating Payloads

How to build high-speed secondary prompt repairs that fix broken JSON fences, missing closing braces, and schema violations in milliseconds.

Della Reno Rinaldi Founder of renodotdev · Backend Developers, API Designers & Integration Engineers

When Structured Decoding Fails

Even with modern structured outputs, real-world edge cases break JSON parsing in production:

  • A user’s query hits the provider’s max_tokens limit mid-stream, truncating the JSON payload before the closing } or ].
  • An older open-weight model emits single quotes instead of valid double quotes ({'key': 'value'}).
  • An unescaped newline inside a multiline string crashes JSON.parse().

When JSON.parse(rawText) throws a SyntaxError, naive architectures crash and fail the user’s request.

A resilient pipeline catches the syntax exception and routes the broken payload through an Ultra-Fast LLM JSON Repair Pass.


1. The Two-Tiered Repair Pipeline

graph TD
    Raw[Raw LLM Output] --> FastRegex[Tier 1: Heuristic Regex & Partial JSON Parser]
    FastRegex -->|Success: Valid JSON| DB[Downstream Database / API]
    FastRegex -->|Fail: SyntaxError| LLMRepair[Tier 2: Fast Micro-LLM JSON Repair Prompt]
    LLMRepair --> Validate[Schema Validator (Zod)]
    Validate --> DB

Before invoking an LLM, always attempt local deterministic repair:

  1. Strip leading and trailing markdown code fences (json ... ).
  2. Use fast local libraries like jsonrepair in JavaScript/Python to close unclosed brackets.
  3. If local heuristics fail, escalate to a specialized micro-model prompt.

2. The Specialized Micro-Repair System Prompt

Because this prompt does not require high-level reasoning, run it on a sub-$0.15/M model (gpt-4o-mini, claude-3-5-haiku, or local 3B SLM) with temperature set strictly to 0.0:

<json_repair_protocol>
  <objective>
    You are an automated deterministic JSON syntax corrector.
    You will receive malformed or truncated text.
    Your sole task is to emit valid, RFC 8259 compliant JSON.
  </objective>

  <repair_rules>
    1. If the payload was abruptly truncated, close all open brackets, arrays, and quotes gracefully.
    2. Replace single quotes with standard double quotes.
    3. Escape raw newlines (\n) and tabs (\t) inside string values.
    4. Remove trailing commas before closing braces (e.g. `[1, 2,]` -> `[1, 2]`).
    5. Do NOT alter the underlying keys, numbers, or semantic facts.
  </repair_rules>

  <output_contract>
    Emit strictly the corrected JSON string.
    Do NOT preface with markdown fences or conversational apologies.
  </output_contract>
</json_repair_protocol>

3. End-to-End Implementation with TypeScript

// lib/safe-json-parse.ts
import { jsonrepair } from 'jsonrepair';
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

export async function parseOrRepairJson<T = any>(rawText: string): Promise<T> {
  // Step 1: Standard Fast Path
  try {
    return JSON.parse(rawText);
  } catch (err) {
    // Continue to Tier 1
  }

  // Step 2: Local Heuristic Repair (0ms latency, $0 cost)
  try {
    const localRepaired = jsonrepair(rawText);
    return JSON.parse(localRepaired);
  } catch (err) {
    // Continue to Tier 2
  }

  // Step 3: LLM Micro-Repair Pass (Fast fallback)
  const repairResult = await generateText({
    model: openai('gpt-4o-mini'),
    temperature: 0.0,
    system: 'Fix malformed JSON. Output strictly valid JSON without explanation or backticks.',
    prompt: rawText,
  });

  return JSON.parse(repairResult.text.trim());
}

4. Key Takeaways

  • Try Local Heuristics First: Libraries like jsonrepair resolve 80% of broken brackets with zero network latency.
  • Use Micro-Models at Temp 0.0: JSON repair requires deterministic syntax fixes, not creative reasoning.
  • Log Repair Events: Track how often your main generator emits invalid JSON to catch prompt drift early.

Written by Della Reno Rinaldi

Founder of renodotdev and Sobatoko. Building mobile and web apps since 2016, previously at Astro, Opaper and The Edge Singapore.

Have a project with similar challenges?

I build mobile apps, websites and AI features for businesses and startups. You work with me directly.