This prompt is a production recovery tool for AI engineers whose structured output pipelines have already failed. It is designed for a single, narrow job: taking a malformed, truncated, or structurally invalid JSON string and repairing it into a valid, schema-compliant payload without data loss. The ideal user is a backend engineer or platform developer who has already implemented a primary generation step with schema instructions, received a 4xx or 5xx error from a downstream parser, and needs a targeted repair step before falling back to a human operator or discarding the response. You should use this prompt when the cost of failure is high—such as in payment processing, user-facing API responses, or data ingestion pipelines—and when the original model output contains recoverable data that is too valuable to discard.
Prompt
Output Repair Prompt for Malformed JSON

When to Use This Prompt
Define the exact production failure this prompt repairs and the conditions under which it is safe to deploy.
This prompt is not a substitute for proper schema-first prompting, structured output modes, or function-calling APIs. Do not use it as your primary generation strategy. It is a safety net, not a foundation. The prompt assumes you have already captured the raw, broken output and the exact validation error or structural defect. It works best when the damage is syntactic (unescaped quotes, trailing commas, missing brackets) or structural (truncation, incorrect nesting) rather than semantic (the model generated the wrong keys or invented fields). If the model hallucinated entirely incorrect data, no repair prompt will recover it—escalate to a full retry or human review instead. For high-risk domains such as finance, healthcare, or legal, always log the original broken output and the repaired version, and require human review before the repaired payload is acted upon.
Before wiring this into your application, define clear circuit-breaker conditions. If the repair prompt itself fails validation after a single attempt, stop retrying and escalate. Do not loop repair attempts indefinitely—each cycle risks compounding errors. Pair this prompt with a strict JSON schema validator in your application layer, and only pass the repaired output downstream if it passes validation. For additional safety, compare the key set of the repaired output against the expected schema to detect dropped or invented fields. The next section provides the copy-ready prompt template you can adapt to your specific schema and error-handling workflow.
Use Case Fit
Where the Output Repair Prompt for Malformed JSON works and where it does not. Use these cards to decide if this prompt fits your production pipeline before you integrate it.
Good Fit: Production API Gateways
Use when: your application receives model-generated JSON that downstream services must consume without manual intervention. Guardrail: deploy the repair prompt as a middleware step with strict schema validation after repair to catch any remaining structural issues before they reach your database or API response.
Bad Fit: Streaming or Partial Responses
Avoid when: you need to repair JSON from streaming responses where the payload is intentionally incomplete. Guardrail: use a buffering strategy to accumulate the full response before invoking repair, or implement a streaming parser that validates chunks independently rather than attempting to fix partial JSON mid-stream.
Required Input: The Broken Payload and Expected Schema
What to watch: the repair prompt needs both the malformed JSON string and the target schema to produce a valid output. Guardrail: always pass the expected schema alongside the broken payload, and include explicit instructions to preserve all recoverable data rather than silently dropping fields that don't match.
Operational Risk: Silent Data Loss
What to watch: the model may quietly omit fields it cannot parse rather than flagging them as unrecoverable. Guardrail: implement a field-level diff between the original malformed input and the repaired output, and log any dropped or altered fields for human review before the repaired payload enters your data pipeline.
Operational Risk: Repair Loop Exhaustion
What to watch: a broken payload may fail repair, trigger a retry, and fail again in an infinite loop that burns tokens and latency budget. Guardrail: set a hard maximum of 2 repair attempts, and after exhaustion return a structured error with the original payload and failure reason rather than retrying indefinitely.
Good Fit: Batch Processing with Human Review
Use when: you process large volumes of model outputs and can queue failed repairs for human inspection. Guardrail: route any payload that fails repair after max retries to a review queue with the original output, repair attempt logs, and schema violations highlighted so an operator can decide whether to fix or discard.
Copy-Ready Prompt Template
A reusable prompt template that repairs malformed JSON outputs by feeding the model its own invalid payload and a structured error description.
This prompt template is designed to be used inside a repair loop: when your application's JSON parser rejects a model output, you feed the invalid string and the parser's error message back to the model with a strict instruction to return only a corrected, valid JSON object. The template uses square-bracket placeholders so you can wire it directly into your production retry or post-processing pipeline without modification.
textYou are a JSON repair agent. Your only job is to fix malformed JSON. You will receive: - A malformed JSON string inside <MALFORMED_JSON> tags. - A parser error message inside <ERROR_MESSAGE> tags. - An expected JSON schema inside <EXPECTED_SCHEMA> tags. Rules: 1. Return ONLY the corrected JSON object. No markdown fences, no commentary, no explanation. 2. Preserve all data values from the original malformed JSON. Do not invent, drop, or summarize fields. 3. If a value is truncated, mark it with a [TRUNCATED] placeholder and set a "_repair_note" field to "truncated_value". 4. If a string is unescaped, escape it properly using valid JSON escape sequences. 5. If the structure is missing closing brackets or braces, infer the correct nesting from the schema and remaining content. 6. If the malformed JSON is unrecoverable, return a valid JSON object with a single field "_repair_error" set to "unrecoverable" and include the original error in "_original_error". 7. Do not add fields that are not in the expected schema unless they are repair metadata fields prefixed with "_repair_". <MALFORMED_JSON> [MALFORMED_JSON_STRING] </MALFORMED_JSON> <ERROR_MESSAGE> [PARSER_ERROR_MESSAGE] </ERROR_MESSAGE> <EXPECTED_SCHEMA> [EXPECTED_JSON_SCHEMA] </EXPECTED_SCHEMA>
To adapt this template, replace the three placeholders with live data from your application: [MALFORMED_JSON_STRING] should contain the raw model output that failed parsing, [PARSER_ERROR_MESSAGE] should contain the exact exception or error text from your JSON parser (e.g., json.JSONDecodeError: Expecting ',' delimiter: line 14 column 23), and [EXPECTED_JSON_SCHEMA] should contain a JSON Schema definition or a representative example of the target structure. For high-risk workflows where data fidelity is critical—such as financial transaction repair or clinical data extraction—always log both the original malformed output and the repaired version, and route outputs containing _repair_note or _repair_error fields to a human review queue before ingestion.
Prompt Variables
Placeholders required by the Output Repair Prompt for Malformed JSON. Each variable must be populated before the repair call. Validation notes describe how to check that the input is fit for purpose before sending it to the model.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[MALFORMED_JSON] | The raw, invalid JSON string that the model must repair. | {"name": "Acme Corp", "revenue": 1.2M,} | Must be a non-empty string. Parse attempt must throw an error. Check for truncation, trailing commas, unescaped quotes, or comment artifacts before sending. |
[EXPECTED_SCHEMA] | A JSON Schema, TypeScript interface, or example object describing the target structure. | {"type": "object", "properties": {"name": {"type": "string"}, "revenue": {"type": "number"}}, "required": ["name"]} | Must be a valid JSON Schema or a clear structural description. If null, the model repairs to any valid JSON. Validate that the schema itself is well-formed. |
[ERROR_CONTEXT] | The exact parse error message or exception string from the initial validation failure. | SyntaxError: Unexpected token M in JSON at position 42 | Should be a non-empty string. If the error is unknown, pass 'Unknown parse error'. The model uses this to localize the fault. Do not pass a stack trace longer than 500 characters. |
[REPAIR_INSTRUCTIONS] | Additional rules for how to handle ambiguous or unrecoverable fields. | If a value is unrepairable, set it to null and add a 'repair_note' field. Do not guess missing required fields. | Must be a string. If empty, the model applies default conservative repair logic. Review for safety: never instruct the model to invent PII, financial figures, or clinical data. |
[ORIGINAL_PROMPT_CONTEXT] | The prompt or task description that produced the malformed JSON. Provides semantic context for ambiguous repairs. | Extract company name, revenue, and employee count from the following text: ... | Optional. If provided, must be a string under 2000 tokens. Helps the model distinguish between a truncated number and a genuinely missing field. Redact any sensitive source text before passing. |
[MAX_REPAIR_ATTEMPTS] | The maximum number of times the repair prompt can be retried if the output is still invalid. | 3 | Must be an integer between 1 and 5. This variable is consumed by the application harness, not the prompt itself. The harness should track attempts and escalate after exhaustion. |
Implementation Harness Notes
How to wire the output repair prompt into a production application with validation, retries, and safe fallback.
The Output Repair Prompt for Malformed JSON is designed to sit inside a post-generation repair loop, not as a standalone prompt. After your primary generation model produces a response, your application should attempt to parse it. If parsing fails with a specific error (truncation, unescaped characters, trailing commas, or structural breaks), capture the raw output and the exact parse error, then invoke this repair prompt. The repair prompt should be treated as a stateless utility function: it receives the broken payload and the error, and it returns either a valid JSON object or a structured failure signal. Do not use this prompt for initial generation; it is a recovery mechanism, not a primary output strategy.
Implement the harness with three layers of defense. First, validate the primary output against your expected schema using a strict JSON parser (such as json.loads in Python or JSON.parse in Node.js). If validation fails, extract the error type and line number. Second, construct the repair call by injecting the raw broken string into the [MALFORMED_JSON] placeholder and the specific error message into [PARSE_ERROR]. Use a faster, cheaper model for the repair step (such as GPT-4o-mini or Claude Haiku) since repair is a constrained task that rarely requires full reasoning capacity. Third, validate the repaired output against the same schema. If it still fails, log the original payload, the repair attempt, and the failure reason, then escalate to a structured error response that your application can handle gracefully—never return raw broken JSON to downstream consumers.
Add observability and circuit breakers around the repair loop. Log every repair invocation with the original error type, the model used, latency, and whether repair succeeded. Track the repair success rate by error category (truncation vs. unescaped characters vs. structural breaks) to identify whether the primary generation prompt needs improvement. Implement a maximum retry limit (typically 1-2 repair attempts) to prevent infinite loops. If the repair model itself returns malformed output, do not recurse—fail immediately and return a predefined error payload. For high-throughput systems, consider caching repair results for identical broken patterns, but be cautious: structurally similar breaks may contain different data. Finally, if the repaired JSON passes schema validation but contains semantic errors (wrong field types, missing required fields, hallucinated values), route it to a separate validation layer that checks business rules before the payload enters your data pipeline.
Expected Output Contract
Fields, format, and validation rules for the repaired JSON output produced by the Output Repair Prompt. Use this contract to validate the model's response before passing it to downstream systems.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
repaired_json | object | Must parse as valid JSON. Use JSON.parse or equivalent. On failure, retry or escalate. | |
repair_log | array of objects | Each entry must contain 'issue' (string), 'fix' (string), and 'location' (string or null). Array must not be empty if any repair was performed. | |
repair_log[].issue | string | Must be one of: 'unescaped_character', 'truncated_object', 'trailing_comma', 'unquoted_key', 'comment_removed', 'invalid_number', 'missing_closing_bracket', 'encoding_error'. | |
repair_log[].fix | string | Must describe the specific action taken. Cannot be empty. Example: 'Escaped backslash in field "description"'. | |
repair_log[].location | string or null | JSONPath or dot-notation string pointing to the repaired location. Use null only if the fix is structural and not field-specific. | |
confidence_score | number | Float between 0.0 and 1.0. Score of 1.0 means no repairs were needed. Score below 0.5 should trigger human review if data is critical. | |
original_hash | string | If provided in the input, must be echoed back exactly. Used for downstream audit matching. Validate string equality, not format. | |
repair_warnings | array of strings | Present only if the model suspects data loss or semantic change. Each string must describe a specific risk. Example: 'Truncated array at index 14; 3 items may be missing'. |
Common Failure Modes
When using an output repair prompt for malformed JSON, these are the most common production failure patterns and how to prevent them before they corrupt downstream systems.
Silent Data Loss During Repair
What to watch: The repair prompt truncates long string values, drops array elements, or collapses nested objects to fit a perceived token limit, losing critical data without warning. Guardrail: Add an explicit instruction to preserve all original field values and array lengths. Validate repaired output against the original malformed payload by comparing key counts and array lengths before accepting the repair.
Hallucinated Field Values
What to watch: When the model encounters an unparseable segment, it invents plausible values for missing fields rather than flagging them as unrecoverable. This is especially dangerous for numeric, monetary, or identifier fields. Guardrail: Require the repair prompt to use a sentinel value like null or "UNRECOVERABLE" for any field it cannot confidently extract. Post-repair validation must reject any output containing invented data in critical fields.
Structural Drift from Original Intent
What to watch: The repair prompt restructures the JSON into a different shape than the original schema—flattening nested objects, converting arrays to objects, or re-keying fields—breaking downstream parsers that expect the original contract. Guardrail: Provide the exact target schema in the repair prompt and instruct the model to match the original structure, not optimize it. Run schema validation on the repaired output before ingestion.
Escape Character Corruption
What to watch: The model mishandles escaped quotes, backslashes, or unicode sequences during repair, producing technically valid JSON that contains garbled string content. Guardrail: Include examples of correct escape handling in the repair prompt. Post-repair, decode and re-encode string fields to verify round-trip fidelity, especially for fields containing user-generated content or code snippets.
Repair Loop Exhaustion
What to watch: The repair prompt itself produces malformed JSON, triggering an infinite retry loop that burns tokens and latency without converging on a valid output. Guardrail: Set a hard maximum of 2 repair attempts. After the second failure, log the original and all repair attempts, then escalate to a human or return a structured error payload. Never allow unbounded repair retries in production.
Context Window Truncation
What to watch: The original malformed JSON plus the repair instructions plus the schema exceed the model's context window, causing the repair prompt itself to be truncated before the model can act on the full payload. Guardrail: Measure the token count of the repair request before sending. If the payload exceeds 80% of the context window, split the repair into field-group batches or escalate to a longer-context model rather than silently truncating.
Evaluation Rubric
Criteria for evaluating the quality and safety of a repaired JSON output before it is passed to a downstream parser or application. Use this rubric to automate pass/fail decisions in a repair pipeline.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Structural Validity | Output is parseable by a standard JSON parser (e.g., | Parser throws a SyntaxError or equivalent. Output contains trailing commas, unquoted keys, or unescaped control characters. | Automated: Run |
Schema Compliance | Output validates against the provided [OUTPUT_SCHEMA]. All required fields are present and no extra fields exist if | Validation library returns errors for missing required fields, type mismatches, or disallowed extra properties. | Automated: Validate repaired output against [OUTPUT_SCHEMA] using a JSON Schema validator. Pass if |
Data Fidelity | All key-value pairs from the original [MALFORMED_INPUT] that can be recovered are present in the repaired output. No data is invented. | A field present in the original malformed string is missing from the repaired output. A new field or value appears that was not in the original input. | Automated: Diff the set of keys and values from a best-effort extraction of [MALFORMED_INPUT] against the repaired output. Flag missing or added data. |
String Repair Accuracy | Truncated strings are correctly closed with quotes. Unescaped characters (e.g., newlines, double quotes) within strings are properly escaped. | A string value is missing its closing quote. A string contains a literal newline or unescaped double quote that breaks the parser. | Automated: Parse the repaired output. If successful, re-serialize and check that all string values are properly quoted and escaped. |
Null and Default Handling | Missing fields that are required by the schema are set to an appropriate default (e.g., | A required field is absent from the output. A field is present but its value is the literal string "null" or "undefined" instead of the JSON null value. | Automated: Check for the presence of all required fields from [OUTPUT_SCHEMA]. Verify that no placeholder strings like "undefined" are used. |
No Hallucinated Content | The repaired output contains only data derived from the [MALFORMED_INPUT]. No explanatory text, apologies, or markdown fences are included. | Output string contains text before the first | Automated: Trim the output string. Assert that it starts with |
Truncation Recovery | If the original input was truncated mid-structure, the repair correctly closes all open brackets and braces, omitting only the unrecoverable data. | The repaired output has an unbalanced number of open and close braces/brackets. A known complete key is missing its value because the truncation point was mishandled. | Automated: Count the number of |
Confidence Annotation | If [INCLUDE_CONFIDENCE] is true, a top-level | The | Automated: If [INCLUDE_CONFIDENCE] is true, assert the field exists and |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Add the expected schema, field-level repair rules, and a confidence marker for every repaired field. Wrap in a retry loop with max attempts and structured logging.
codeRepair this malformed JSON to match [SCHEMA]. For each field, mark repair confidence: high, medium, low. If a field cannot be repaired, set it to null and flag it. Return: { "repaired": { ... }, "repair_log": [ {"field": "...", "action": "...", "confidence": "..."} ] } Input: [INVALID_JSON] Error: [PARSE_ERROR]
Watch for
- Silent format drift when the model changes field types to make parsing work
- Repair log fields that don't match actual changes
- Missing retry budget exhaustion handling

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us