Inferensys

Prompt

JSON Repair Prompt Template for API Payloads

A practical prompt playbook for using JSON Repair Prompt Template for API Payloads in production AI workflows.
Operations team reviewing AI workflow automation on laptop, workflow builder visible, casual office setup.
PROMPT PLAYBOOK

When to Use This Prompt

A programmatic repair step for backend engineers recovering broken JSON from model-generated API responses before retrying the original generation.

This prompt is for backend engineers and platform teams who receive broken JSON from model-generated API responses. It repairs the most common structural failures: missing commas between key-value pairs, trailing commas after the last element, unescaped double quotes inside string values, and mismatched bracket or brace counts. Use this prompt when a downstream JSON parser rejects the model output and you need a programmatic repair step before retrying the original generation.

This is a post-generation repair loop, not a schema enforcement tool. If the model output violates a target JSON Schema, use the Schema Validation Failure Repair Prompt instead. If the JSON is embedded in markdown fences, use the Markdown Code Block Extraction Prompt first. The repair prompt works best on structurally broken but semantically intact JSON—it cannot recover content that was never generated or fix deeply nested structural corruption where the intended hierarchy is ambiguous.

Wire this prompt into your application's error-handling path: catch JSON.parse failures, extract the raw string, inject it into the [BROKEN_JSON] placeholder, and attempt repair before falling back to a full regeneration request. Always validate the repaired output with a parse attempt and compare against your expected schema. For high-throughput pipelines, add a repair attempt counter to avoid infinite loops and log repair success rates to monitor model output quality over time.

PRACTICAL GUARDRAILS

Use Case Fit

Where the JSON Repair Prompt works, where it fails, and the operational preconditions required before you wire it into a production pipeline.

01

Good Fit: Near-Miss Syntax Errors

Use when: The model output is semantically correct JSON but fails parsing due to common tokenization artifacts—trailing commas, missing commas, unescaped quotes, or single-quote usage. Guardrail: Always run a strict parser before invoking repair; only route to the repair prompt if the parse error is structural, not semantic.

02

Bad Fit: Hallucinated or Fabricated Data

Avoid when: The model invented field values, keys, or entire objects that don't exist in the source. Risk: Repairing syntax will not fix hallucination; it will make fabricated data parseable and harder to detect downstream. Guardrail: Run a schema diff and source-grounding check before repair; if fields are fabricated, discard and re-generate with stricter constraints.

03

Required Input: Valid Schema Contract

Use when: You have a target JSON Schema, OpenAPI spec, or strict field contract to validate against. Avoid when: The expected output shape is ambiguous or defined only by examples. Guardrail: Provide [TARGET_SCHEMA] as a required input; the repair prompt should diff the repaired output against the schema and flag unresolvable mismatches instead of guessing.

04

Operational Risk: Silent Corruption in Streaming

Risk: In streaming pipelines, a repair prompt may succeed on a partial chunk but produce valid JSON that is semantically wrong when reassembled. Guardrail: Never repair streaming fragments in isolation. Buffer complete logical records before invoking repair, and validate the assembled payload end-to-end against the schema before forwarding downstream.

05

Operational Risk: Repair Loop Exhaustion

Risk: A broken output may loop through repair and re-validation multiple times without converging, burning tokens and latency budget. Guardrail: Set a hard retry limit (recommended: 2 repair attempts). After the limit, log the failure, capture the unrecoverable payload, and escalate to a dead-letter queue for human review or regeneration.

06

Bad Fit: Deeply Nested Structural Collapse

Avoid when: The JSON structure is so corrupted that bracket balancing is ambiguous—multiple equally plausible repairs exist. Risk: The model may choose a structurally valid but semantically wrong repair, introducing subtle data corruption. Guardrail: Use a bracket-balance heuristic before invoking repair; if ambiguity exceeds a threshold, flag for manual review instead of attempting automated recovery.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt with square-bracket placeholders for repairing malformed JSON API payloads. Copy, populate the variables, and wire into your validation harness.

This template is designed to be the core instruction set for a repair loop. It expects a malformed JSON string and a target JSON Schema, and it instructs the model to produce a strictly valid, schema-conforming output. The prompt uses square-bracket placeholders so you can inject the specific payload, schema, and operational constraints at runtime without modifying the core instruction text. Use this template as the system or user message in your repair harness, directly after a parser or schema validator has rejected the original model output.

text
You are a JSON repair specialist. Your task is to fix a malformed JSON payload so that it becomes valid JSON and strictly conforms to the provided JSON Schema.

# INPUT
[MALFORMED_JSON_PAYLOAD]

# TARGET SCHEMA
[TARGET_JSON_SCHEMA]

# REPAIR RULES
1. Parse the input and identify all syntax errors (e.g., missing commas, trailing commas, unescaped characters, mismatched brackets, single quotes instead of double quotes).
2. Identify all schema violations (e.g., wrong types, missing required fields, extra fields not in the schema, incorrect enum values).
3. Repair the JSON to fix all syntax errors and schema violations.
4. For ambiguous repairs (e.g., a string "123" in a number field), coerce the value to the correct type. If coercion is impossible, set the field to `null`.
5. Do not add, remove, or change any data beyond what is necessary to achieve valid, schema-conforming JSON. Preserve all recoverable information.
6. If the input is completely unrecoverable, output a valid JSON object with a single key `"repair_error"` containing a string description of the failure.

# CONSTRAINTS
[ADDITIONAL_CONSTRAINTS]

# OUTPUT FORMAT
Return ONLY the repaired JSON object. Do not include any explanatory text, markdown fences, or code blocks. The output must parse with a standard JSON parser.

To adapt this template, replace each square-bracket placeholder with the appropriate data for your current repair attempt. [MALFORMED_JSON_PAYLOAD] should be the raw string that failed validation. [TARGET_JSON_SCHEMA] should be the complete JSON Schema object the output must satisfy. [ADDITIONAL_CONSTRAINTS] is optional and can be used to inject field-specific rules, such as a map of enum values or a null-representation policy. After populating the placeholders, send this prompt to the model. The output should be a clean JSON object ready for re-validation. Always run the model's output through a JSON parser and schema validator before accepting it. If validation fails again, feed the new error and the model's output back into the repair loop with an updated [MALFORMED_JSON_PAYLOAD], up to your configured retry limit.

IMPLEMENTATION TABLE

Prompt Variables

Inputs required for the JSON repair prompt to function reliably. Validate each variable before sending to the model to prevent cascading repair failures.

PlaceholderPurposeExampleValidation Notes

[BROKEN_JSON]

The malformed JSON payload that failed parsing

{"name": "test", "value": 42,}

Must be a non-empty string. Validate that it is not already valid JSON. If already valid, skip repair and return as-is.

[TARGET_SCHEMA]

The expected JSON Schema that the output must conform to

{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}

Must be a valid JSON Schema object. Validate with ajv or jsonschema library before use. If null, repair focuses on structural validity only.

[PARSE_ERROR_MESSAGE]

The exact error message from the JSON parser that failed

Unexpected token } in JSON at position 35

Must be a non-empty string. Extract directly from JSON.parse or equivalent parser. Do not fabricate or summarize. Used to guide targeted repair.

[FIELD_TYPE_MAP]

Mapping of field names to their expected types for coercion

{"count": "number", "active": "boolean", "tags": "array"}

Optional. If provided, must be a valid object with field names as keys and JSON Schema type strings as values. Null allowed when type coercion is not needed.

[ALLOWED_ENUMS]

Map of field names to arrays of allowed values for enum repair

{"status": ["active", "inactive", "pending"], "priority": ["low", "medium", "high"]}

Optional. If provided, each key must map to a non-empty array of strings. Null allowed when enum repair is not needed. Validate no duplicate values within each array.

[NULL_REPRESENTATION]

Target representation for null or missing values in output

"null"

Must be one of: "null", "empty_string", "remove_key". Defaults to "null" if not provided. Controls how absent or N/A values are normalized in the repaired output.

[MAX_REPAIR_DEPTH]

Maximum nesting depth to attempt structural repair

10

Must be a positive integer between 1 and 50. Defaults to 20 if not provided. Prevents infinite loops on severely corrupted payloads. Higher values increase token usage and latency.

[RESOLUTION_STRATEGY]

Strategy for resolving duplicate keys in objects

"last_wins"

Must be one of: "last_wins", "first_wins", "merge_arrays", "flag_conflict". Defaults to "last_wins" if not provided. Controls how duplicate key conflicts are resolved during repair.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the JSON repair prompt into a production application with validation, retry logic, and observability.

The JSON repair prompt is not a standalone fix; it is a component inside a repair loop. The application should first attempt to parse the model's raw output. If JSON.parse() or an equivalent schema validator fails, capture the exact error message, line number, and a snippet of the malformed JSON around the failure point. Feed these diagnostics into the prompt's [MALFORMED_JSON] and [PARSE_ERROR] placeholders rather than sending the entire raw output with no context. This targeted input dramatically improves repair accuracy and reduces token cost.

Wrap the prompt call in a retry controller with a hard limit—typically 3 attempts. On each iteration, validate the repaired output against the expected [OUTPUT_SCHEMA] using a JSON Schema validator like ajv. If validation passes, break the loop and return the payload. If it fails, feed the new parse error back into the next repair attempt. After the final retry, if the output is still invalid, do not guess. Log the failure with the original input, all repair attempts, and the final error. Escalate to a dead-letter queue or a human review interface depending on the [RISK_LEVEL] configured for the workflow.

For API payload repair specifically, add a structural diff step after successful repair. Compare the repaired JSON against the original malformed input to produce a list of changes: added commas, removed trailing commas, escaped quotes, balanced brackets. This diff serves dual purpose: it provides an audit trail for debugging model behavior and can be surfaced to human reviewers when the repair confidence is borderline. Store the diff alongside the repaired payload in your observability system.

Choose your model based on the complexity of the repair, not just cost. For simple fixes like missing commas or unescaped strings, a fast, cheap model like GPT-4o-mini or Claude Haiku works well. For deeply nested structural corruption, bracket imbalances across multiple levels, or repairs where field-level type coercion is required, use a more capable model like GPT-4o or Claude Sonnet. Implement model routing in your harness: attempt the first repair with a fast model, and escalate to a stronger model only if the first attempt fails validation.

Log every repair attempt with structured metadata: original_length, repair_attempt_number, model_used, parse_error_type, validation_result, tokens_consumed, and repair_diff. This telemetry lets you measure repair success rates by error category, identify recurring failure patterns that need prompt improvements, and track cost per successful repair. If a specific error type—like trailing commas in nested arrays—shows a repair success rate below 90%, invest in improving the prompt's handling of that case or add a pre-processing regex step before the LLM call.

IMPLEMENTATION TABLE

Expected Output Contract

The repaired JSON payload must conform to this contract before it is accepted by downstream systems. Use these fields, types, and validation rules to gate the output of the JSON repair prompt.

Field or ElementType or FormatRequiredValidation Rule

repaired_json

Valid JSON object

Must pass JSON.parse without throwing. No trailing commas, unescaped characters, or single quotes as string delimiters.

repair_log

Array of objects

Each entry must contain 'field' (string), 'issue' (string), and 'action' (string). Array must not be empty if any repair was performed.

repair_log[].field

String (JSONPath)

Must be a valid JSONPath expression pointing to the repaired location. Use '$' for top-level structural repairs like bracket balancing.

repair_log[].issue

String from [ISSUE_TAXONOMY]

Must match one of the allowed issue types: 'missing_comma', 'trailing_comma', 'unescaped_character', 'bracket_mismatch', 'single_quotes', 'duplicate_key', 'type_mismatch', 'truncation'.

repair_log[].action

String

Must describe the specific corrective action taken. Examples: 'Added missing comma after key "name"', 'Converted single quotes to double quotes for entire object', 'Closed unclosed brace at depth 3'.

confidence_score

Number (0.0 to 1.0)

Must be a float between 0 and 1 inclusive. Score below [CONFIDENCE_THRESHOLD] should trigger human review or retry. Score 1.0 only if repair is trivially unambiguous.

unrecoverable_segments

Array of strings or null

If null, no unrecoverable segments exist. If present, each string must quote the exact damaged substring that could not be repaired. Must be empty array, not null, when all damage is recoverable.

schema_compliant

Boolean

Must be true if [TARGET_SCHEMA] is provided and repaired_json passes full JSON Schema validation. Must be false if validation fails. Must be null only if no [TARGET_SCHEMA] is supplied.

PRACTICAL GUARDRAILS

Common Failure Modes

What breaks first when using a JSON repair prompt in production and how to guard against it.

01

Silent Content Hallucination

What to watch: The model repairs structural JSON issues but invents plausible values for missing or truncated fields, especially near the truncation point. Guardrail: Diff the repaired output against the original malformed payload and flag any key with a value that changed without a structural reason. Require human review for net-new values in required fields.

02

Repair Loop Exhaustion

What to watch: The repair prompt produces output that still fails validation, triggering a retry loop that burns tokens without converging. Guardrail: Set a hard retry limit (max 3 attempts). After the second failure, log the validator error, save the partial payload, and escalate to a dead-letter queue instead of retrying indefinitely.

03

Schema Drift After Repair

What to watch: The repaired JSON parses successfully but violates the target schema—wrong field names, missing required keys, or incorrect types that the repair prompt didn't catch. Guardrail: Always run the repaired output through the same JSON Schema validator used for the original generation. Reject any payload that doesn't pass re-validation, even if it's valid JSON.

04

Truncation Boundary Corruption

What to watch: When the model output was cut off mid-string or mid-structure, the repair prompt guesses where to close brackets and quotes, often creating malformed string values or orphaned keys at the truncation point. Guardrail: Mark all fields within 2 levels of the detected truncation point with a _repair_confidence: low flag. Route low-confidence fields for human review before ingestion.

05

Duplicate Key Shadowing

What to watch: The original malformed JSON contains duplicate keys, and the repair prompt silently keeps one while discarding the other, potentially losing data. Guardrail: Pre-scan the input for duplicate keys before repair. If found, log both values and apply a declared resolution strategy (last-wins, merge-arrays, or flag-for-review) rather than letting the model decide.

06

Encoding Artifact Injection

What to watch: The repair prompt introduces invisible Unicode characters, smart quotes, or non-breaking spaces that pass JSON parse but break downstream systems expecting ASCII or UTF-8 clean text. Guardrail: Run repaired output through a character whitelist filter that strips or replaces characters outside the expected encoding range. Validate with a byte-level check before forwarding to downstream consumers.

IMPLEMENTATION TABLE

Evaluation Rubric

How to test output quality before shipping. Run these checks on a golden dataset of 50 known-broken JSON samples with known-good repairs.

CriterionPass StandardFailure SignalTest Method

Parse Validity

Output is valid JSON per json.loads() or equivalent parser

Parser throws exception; output is not valid JSON

Parse every output in the golden set; 100% must parse without error

Structural Fidelity

Repaired JSON matches the known-good repair for each sample

Keys missing, extra keys present, or values differ from expected repair

Deep-compare each output to the known-good repair; structural diff must be empty

Bracket and Brace Balance

All { }, [ ] pairs are correctly balanced and nested

Unmatched opening or closing bracket; nesting depth mismatch

Count bracket pairs per output; verify depth consistency with stack-based check

String Escaping Correctness

All double quotes, backslashes, and control characters are properly escaped

Unescaped quotes inside strings; invalid escape sequences; broken unicode escapes

Regex scan for unescaped quotes inside string values; validate all \ sequences

Trailing Comma Removal

No trailing commas before closing } or ]

Trailing comma detected immediately before } or ]

Regex check for ,\s*[}\]]) pattern; must return zero matches per output

Missing Comma Insertion

Commas inserted between adjacent string values or objects where missing

Two string literals or values adjacent without comma separator

Parse then re-serialize; compare token sequences for missing separator recovery

Type Preservation

Field types match the known-good repair types (string, number, boolean, null, object, array)

Number coerced to string; boolean became string "true"; null became "null" string

Type-check each field against known-good repair; flag any type mismatch

No Content Hallucination

Repair adds no new keys, values, or content beyond what was recoverable from the broken input

New field appears that was not in the broken input or known-good repair

Diff output keys against union of broken-input keys and known-good repair keys; flag additions

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Add [TARGET_SCHEMA] as a required input, implement a parse-retry loop with max 3 attempts, log every repair diff, and gate output on schema validation pass.

code
You are a JSON repair system. Your job is to fix malformed JSON so it parses and validates against a target schema.

TARGET SCHEMA:
[TARGET_SCHEMA]

BROKEN JSON:
[BROKEN_JSON]

RULES:
1. Repair structural errors: missing commas, trailing commas, unescaped quotes, bracket imbalances.
2. Do NOT add fields not present in the broken input unless required by schema.
3. If a required schema field is missing, set it to null and flag it in [REPAIR_DIFF].
4. Output valid JSON matching the target schema.
5. Return a JSON object with two keys: "repaired_json" and "repair_diff" listing every change made.

Watch for

  • Silent format drift across model versions
  • Missing human review gate for high-value payloads
  • Repair diff growing too large to debug
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.