This prompt is for backend and platform engineers who need to recover API payloads, configuration blocks, or structured data records that were cut off by token limits, streaming interruptions, or model early-stopping. The job-to-be-done is straightforward: take a partial, unparseable JSON string and produce a structurally valid, parseable JSON output that preserves every piece of the original partial data without inventing new content. The ideal user is someone integrating LLM outputs into a production pipeline where downstream consumers—databases, API gateways, configuration parsers—will reject malformed JSON outright. You already have a partial payload in hand, you know the expected schema or at least the intended structure, and you need a repair that can be automated without manual intervention.
Prompt
Truncated JSON Completion Repair Prompt Template

When to Use This Prompt
Define the job, reader, and constraints for repairing truncated JSON in production systems.
This prompt is designed for scenarios where the model output is almost complete but missing closing braces, brackets, or the final characters of a string value. It works best when you can provide the truncated output alongside the original task context or schema description so the model understands what it's repairing. You should not use this prompt when the truncation is so severe that the majority of the structure is missing—at that point, you're better off re-requesting the full generation with a higher token limit or using a continuation prompt instead. Similarly, avoid this prompt when the partial output contains hallucinated fields or values that need removal; this is a structural repair tool, not a factuality filter. For high-risk domains where the repaired JSON will trigger financial transactions, clinical actions, or legal commitments, always route the repaired output through a human review step and schema validation before it reaches any execution path.
Before wiring this into production, define clear failure boundaries. If the repair attempt produces JSON that still fails to parse after one retry, log the failure, capture the original truncated payload, and escalate rather than looping indefinitely. Pair this prompt with a JSON schema validator in your harness to catch field-level errors that structural repair alone won't fix. The next step after reading this section is to copy the prompt template, adapt the placeholders to your payload shape, and run it against a set of deliberately truncated test cases to establish your baseline repair success rate.
Use Case Fit
Where this prompt works, where it fails, and what you must provide before using it in production.
Good Fit: Token-Limit Truncation
Use when: The model output is valid JSON up to the point of truncation, and the finish_reason is length or max_tokens. Guardrail: Always check the finish reason before invoking repair. If the model stopped for content filtering or a tool call, this prompt is the wrong recovery path.
Bad Fit: Mid-String Corruption
Avoid when: Truncation severed an escape sequence, a string literal, or a multi-byte character in the middle. Guardrail: Run a structural scan first. If the break is inside a string, use a specialized escape-repair prompt instead of the general completion repair template.
Required Input: Partial Output Plus Schema
What you must provide: The truncated JSON fragment, the expected JSON Schema, and the original task context. Guardrail: Never send only the fragment. Without the schema, the model will hallucinate missing fields. Without the task context, it will drift from the original intent.
Operational Risk: Hallucinated Completion
What to watch: The model invents plausible but incorrect values for truncated fields, especially nested objects and array elements. Guardrail: Post-repair, diff the original fragment against the repaired output. Flag any field whose value changed in the preserved portion as a hallucination and require human review.
Operational Risk: Repair Loop Sprawl
What to watch: A single truncated output triggers multiple repair attempts, each consuming tokens and latency budget without converging. Guardrail: Set a max retry count (recommended: 2). If the output still fails schema validation after two repair passes, log the fragment and escalate to a dead-letter queue for manual triage.
Bad Fit: Semantic Truncation
Avoid when: The output is syntactically complete but semantically cut off—for example, a JSON array that closed correctly but is missing the last three items. Guardrail: This is a content-completeness problem, not a structural repair problem. Use a continuation prompt with the original task context instead of a structural repair prompt.
Copy-Ready Prompt Template
A reusable prompt template with square-bracket placeholders for repairing truncated JSON objects, arrays, and strings into valid, parseable output.
This prompt template is the core instruction you send to a language model when a JSON payload has been cut off by token limits, streaming interruptions, or early stopping. It is designed to be copied directly into your repair harness, with placeholders that your application code populates at runtime. The template instructs the model to act as a strict JSON repair engine: it must preserve all valid partial data, infer only the minimal structural closures needed, and never hallucinate new fields or values beyond what the partial input provides.
textYou are a strict JSON repair engine. Your only job is to convert a truncated, incomplete JSON payload into valid, parseable JSON. ## Input [TRUNCATED_JSON_PAYLOAD] ## Expected Output Schema (if known) [OUTPUT_SCHEMA] ## Constraints - Preserve every valid key, value, string, number, boolean, and null from the input exactly as it appears. - Do not invent new fields, keys, or values. If a value is partially written (e.g., a truncated string or number), complete it to the most minimal valid form that allows parsing, or mark it with a placeholder like `"[INCOMPLETE]"` if the original intent is unrecoverable. - Close all open objects, arrays, and strings. Infer missing closing brackets (`}`, `]`), quotes, and commas based on the structure of the partial input. - If the input contains a truncated array, close the array after the last complete element. Do not add new elements. - If the input contains a truncated object, close the object after the last complete key-value pair. Do not add new pairs. - If a string value is truncated mid-content, close the string with a quote and append `[TRUNCATED]` inside the string value. - If a number is truncated, complete it to the nearest valid number or replace it with `null` and add a `"_repair_note"` field at the same level. - Output only the repaired JSON. No explanations, no markdown fences, no surrounding text. ## Examples ### Example 1: Truncated Object Input: `{"name": "Alice", "age": 30, "city": "New Y` Output: `{"name": "Alice", "age": 30, "city": "New Y[TRUNCATED]"}` ### Example 2: Truncated Array Input: `[{"id": 1, "val": "a"}, {"id": 2, "val": "b` Output: `[{"id": 1, "val": "a"}, {"id": 2, "val": "b[TRUNCATED]"}]` ### Example 3: Missing Closing Brace Input: `{"results": [{"x": 1}, {"x": 2}]` Output: `{"results": [{"x": 1}, {"x": 2}]}` ## Risk Level [RISK_LEVEL]
To adapt this template, replace each square-bracket placeholder with runtime values from your application. [TRUNCATED_JSON_PAYLOAD] should contain the raw, incomplete JSON string exactly as received from the model or streaming buffer. [OUTPUT_SCHEMA] is optional but strongly recommended: provide the expected JSON Schema, TypeScript interface, or a plain-text description of the expected fields and types to help the model distinguish between structural truncation and intentionally missing optional fields. [RISK_LEVEL] should be set to high if the repaired JSON will be written to a database, sent to a user, or used in a downstream decision; in those cases, add an instruction like "If any field is unrecoverable, set its value to null and add a top-level _repair_errors array describing each repair action." For low-risk debugging or log contexts, you can omit the error-tracking instruction. After populating the template, wrap it in your standard chat message structure (system or user message) and send it to the model. Always validate the output with a JSON parser before accepting it, and log the repair attempt for observability.
Prompt Variables
Required and optional inputs for the Truncated JSON Completion Repair Prompt. Each placeholder must be populated before the prompt is sent to the model. Validation notes describe how to programmatically verify the input before execution.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[TRUNCATED_JSON] | The incomplete JSON payload that was cut off by token limits, streaming interruption, or model early-stopping | {"users":[{"id":1,"name":"Alice","email":"alice@ | Parse as string; must not be empty; must contain at least one unclosed brace, bracket, or quote; reject if already valid JSON |
[EXPECTED_SCHEMA] | JSON Schema or natural-language description of the expected output structure to guide repair without hallucinating fields | {"type":"object","required":["users"],"properties":{"users":{"type":"array","items":{"type":"object","required":["id","name","email"]}}}} | If provided as JSON Schema, validate it parses; if natural language, check string length > 20 chars; null allowed when schema is unknown |
[CONTEXT_HINT] | Optional description of what the JSON represents to help the model infer missing values from domain context | "User profile export from CRM with id, name, email, and role fields" | String or null; max 500 chars; strip PII before passing; null allowed |
[REPAIR_STRATEGY] | Instruction for how to handle unrecoverable fields: infer from context, insert placeholder, or mark as missing | "infer_from_context" | Must be one of: infer_from_context, insert_null, insert_placeholder, mark_missing; default to insert_null if not specified |
[MAX_REPAIR_DEPTH] | Maximum nesting depth the model should attempt to repair before marking the structure as unrecoverable | 5 | Integer between 1 and 20; default 10; higher values increase risk of hallucinated nested structures |
[ALLOW_HALLUCINATION] | Boolean flag controlling whether the model may invent missing field values or must leave them as null placeholders | Must be true or false; default false for factuality-critical pipelines; set true only for non-critical display contexts | |
[OUTPUT_FORMAT] | Target format for the repaired output; typically json but may be jsonl for streaming reassembly | "json" | Must be json or jsonl; default json; jsonl requires newline-delimited validation in harness |
Implementation Harness Notes
How to wire the Truncated JSON Completion Repair Prompt into a production application with validation, retries, and observability.
The repair prompt is not a standalone utility; it is a recovery step inside a larger generation pipeline. The typical integration point is immediately after a model response fails JSON parsing or schema validation due to truncation. Your application should catch the parse error, extract the partial JSON string, and invoke this repair prompt before falling back to a full regeneration or escalating to a human. This keeps latency low and avoids discarding valid partial work that the model already produced.
Validation gates are mandatory before the repaired output enters any downstream system. After the repair prompt returns, run the result through a strict JSON parser. If parsing fails, apply a secondary structural repair (e.g., balancing braces with a deterministic algorithm) and retry the repair prompt once with the re-balanced partial input. If the second attempt fails, log the failure, capture the raw partial and repaired strings, and either request a full regeneration from the original task prompt or route to a dead-letter queue for manual review. For high-risk domains, always require human approval before repaired payloads are written to databases, sent to users, or used in automated decisions.
Model choice matters for repair reliability. Smaller or faster models often struggle with precise structural completion, especially for deeply nested JSON. Prefer a capable model with strong JSON-following behavior for the repair step, even if you use a cheaper model for the initial generation. Set temperature=0 and keep max_tokens high enough to complete the truncated structure. Log every repair attempt with the original partial output, the repaired output, parse success/failure, and the model version used. This trace data is essential for debugging systemic truncation issues and tuning your initial generation token limits.
Expected Output Contract
Defines the required fields, types, and validation rules for the repaired JSON output. Use this contract to build post-processing validators and eval harnesses before deploying the repair prompt.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
repaired_json | Valid JSON object | Must pass JSON.parse without throwing an error. No trailing commas, unclosed braces, or broken string literals allowed. | |
completion_strategy | string enum: structural_closure | value_inference | continuation_request | Must be one of the three allowed values. If the model cannot determine the strategy, the output is invalid. | |
truncation_point | string (JSONPath expression) | Must be a valid JSONPath string pointing to the exact location where the input was truncated. Example: $.items[2].description | |
repaired_segments | array of objects | Each object must contain a 'path' (JSONPath), 'action' (added | inferred | closed), and 'original_value' (null or string). Array must not be empty. | |
repaired_segments[].path | string (JSONPath) | Must resolve to a valid location within the repaired_json object. Paths must be unique within the array. | |
repaired_segments[].action | string enum: added | inferred | closed | Must be one of the three allowed values. 'added' for structural closures, 'inferred' for value completions, 'closed' for delimiter fixes. | |
repaired_segments[].original_value | string or null | Must be the exact partial string from the input at the truncation point, or null if no partial value existed. Cannot be an invented value. | |
confidence_score | number (0.0 to 1.0) | Must be a float between 0.0 and 1.0 inclusive. Scores below 0.5 should trigger human review if the output is used in high-risk workflows. |
Common Failure Modes
Truncated JSON is one of the most common production failures when models hit token limits or streaming interruptions. These cards cover the specific ways repair prompts break and how to guard against them.
Hallucinated Field Completion
What to watch: The model invents plausible values for truncated fields instead of marking them as unrecoverable. A partial "email": "jane.d becomes "email": "jane.doe@example.com" with no evidence. Guardrail: Explicitly instruct the model to use a sentinel value like "__TRUNCATED__" for any field it cannot recover verbatim from the partial input. Validate that no field value contains invented content not present in the original truncated string.
Structural Mismatch After Repair
What to watch: The repaired JSON parses successfully but violates the expected schema—missing required fields, wrong types on repaired values, or extra keys the model added during completion. Guardrail: Run the repaired output through the same JSON Schema validator used for normal outputs. Reject repairs that change the schema shape. Track schema_pass_rate separately from parse_success_rate in eval harnesses.
Silent Data Loss in Truncated Arrays
What to watch: A truncated array [obj1, obj2, obj3, {"id": 4, "na gets repaired by closing the array after the last complete element, silently dropping the partial obj4 record. Guardrail: Require the repair prompt to preserve partial elements as incomplete records with a _repair_status: "partial" flag. Count elements before and after repair. Flag any repair that reduces element count without explicit truncation markers.
String Escape Boundary Corruption
What to watch: Truncation occurs mid-escape sequence—"path": "C:\Users\—and the repair either doubles the backslash or leaves an unescaped character that breaks the parser. Guardrail: Include explicit escape-sequence boundary examples in the repair prompt. Test with a harness that injects truncation points at known escape positions (\n, \", \\, \u) and verifies the repaired string is byte-identical to the original where recoverable.
Repair Loop Exhaustion Without Escalation
What to watch: The repair prompt itself fails validation, triggering another repair attempt, creating a loop that burns tokens without converging. After three retries the output is no better than the first attempt. Guardrail: Implement a hard retry limit (max 2 repair attempts). After the limit, log the partial output with an unrecoverable_truncation flag and escalate to a dead-letter queue for human review or fallback handling. Never loop indefinitely.
Context Drift in Continuation Requests
What to watch: When requesting continuation from a truncation point, the model loses the original task context and produces output in a different tone, format, or direction than the truncated portion. Guardrail: Include the original system prompt and task instructions in the repair request, not just the partial output. Add a constraint: "Continue from the exact truncation point. Do not restate, summarize, or change the output format." Validate continuation coherence with embedding similarity against the original partial output.
Evaluation Rubric
Criteria for evaluating the quality of a repaired JSON output before it is allowed into downstream systems. Use these checks in an automated eval harness or LLM-as-judge step.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Structural Validity | Output parses successfully as JSON without syntax errors | JSON.parse throws an exception or returns null | Automated parse check using the target language's JSON parser |
Field Preservation | All valid key-value pairs from the truncated input are present in the repaired output | A key from the partial input is missing or its value has been altered | Diff the keys and values of the partial input against the repaired output |
No Hallucinated Data | No new keys, values, or array elements are invented beyond what is necessary to close the structure | A new field appears that was not present in the truncated input | Schema comparison: the set of top-level keys in the output must be a subset of or equal to the keys in the partial input |
String Closure | All strings are properly closed with matching quotes and valid escape sequences | A string value ends without a closing quote or contains an invalid escape like '\x' | Automated scan for unescaped control characters and unmatched quote pairs |
Bracket and Brace Balance | All objects and arrays are closed with matching braces/brackets | The count of '[' does not equal ']' or '{' does not equal '}' | Automated bracket-matching counter applied to the raw output string |
Schema Compliance | Repaired output passes validation against the expected JSON Schema, if provided | Schema validator returns errors for missing required fields or type mismatches | Run output through a standard JSON Schema validator with the expected schema |
No Content Truncation | The repaired output does not end mid-string, mid-key, or mid-value | The final value in the output is a partial string or number | Check that the last meaningful token is a complete value, not a fragment |
Idempotency | Running the repair prompt on the repaired output produces an identical result | A second repair pass changes the structure or values | Execute the repair prompt twice and assert deep equality between the two outputs |
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
Use the base prompt with a lightweight wrapper. Focus on structural repair only—close braces, brackets, and quotes. Skip field-level validation and completeness checks. Accept any parseable JSON as success.
codeYou are a JSON repair tool. The following JSON output was truncated mid-generation. Partial JSON: [TRUNCATED_JSON] Repair the JSON so it is valid and parseable. Close any open structures. Do not invent new data beyond what is implied by the partial input. Return ONLY the repaired JSON, no explanation.
Watch for
- Repaired output that parses but contains hallucinated field values
- Over-aggressive completion of truncated string values
- No mechanism to detect when truncation occurred mid-key vs mid-value

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