This prompt is for integration engineers and backend developers who receive a truncated structured output (JSON, XML, YAML, or CSV) from a language model and need to repair it into a valid, complete structure without re-running the original, expensive generation. The job-to-be-done is automated recovery: detect the intended format from the partial content, infer the missing structural elements, and produce a parseable payload that passes schema validation. The ideal user is building a production AI pipeline where downstream parsers reject malformed outputs, and manual repair is not scalable.
Prompt
Truncated Structured Output Repair Prompt Template

When to Use This Prompt
Define the job, reader, and constraints for the Truncated Structured Output Repair Prompt Template.
Use this prompt when you have a partial, syntactically invalid payload and you know the expected output schema. It is designed for cases where the model's generation was cut off mid-structure—for example, a JSON object missing closing braces, an XML document without its end tags, or a CSV row with an incomplete final column. The prompt requires you to provide the truncated content, the target schema, and any format-specific constraints. It is not suitable for repairing outputs that are semantically wrong but syntactically valid, nor for recovering from truncation that lost critical data rather than just structural tokens. Do not use this prompt when the truncation point is ambiguous or when the partial content is too short to infer the format.
Before wiring this into an application, define clear failure modes: the repair may produce valid syntax that violates business logic, or it may hallucinate missing field values. Always validate the repaired output against your schema and, for high-risk domains, route ambiguous repairs for human review. The next section provides the copy-ready template you can adapt with your schema, format rules, and validation constraints.
Use Case Fit
Where the Truncated Structured Output Repair prompt works, where it fails, and the operational risks to manage before wiring it into a production pipeline.
Good Fit: Deterministic Parsers Downstream
Use when: a structured output (JSON, XML, YAML, CSV) is truncated mid-stream and a strict parser rejects the payload. Guardrail: the repair prompt must receive the raw truncated string plus the expected schema to reconstruct valid syntax.
Bad Fit: Semantic Truncation Without Structural Damage
Avoid when: the output is valid JSON but missing a logical field (e.g., a summary cut off mid-sentence). Guardrail: this prompt repairs syntax, not missing content. Use a continuation prompt for semantic gaps.
Required Inputs
Risk: repair fails without the original partial output, the expected schema, and the format type. Guardrail: always pass [TRUNCATED_OUTPUT], [EXPECTED_SCHEMA], and [FORMAT_TYPE] as explicit variables. Never rely on the model to guess the schema.
Operational Risk: Repair Loop Amplification
Risk: a repair prompt that produces another invalid output triggers infinite retry loops. Guardrail: enforce a hard [MAX_REPAIR_ATTEMPTS] (default 2) and escalate to a human or fallback model after exhaustion.
Operational Risk: Silent Data Corruption
Risk: the repair prompt guesses missing values to close a structure, introducing plausible but incorrect data. Guardrail: require the repair to mark inferred fields with a repaired: true flag and log all repairs for audit.
Good Fit: Multi-Format Pipelines
Use when: a single ingestion pipeline handles JSON, XML, YAML, and CSV outputs and needs format-agnostic recovery. Guardrail: include a format-detection step before repair. If format detection confidence is below [CONFIDENCE_THRESHOLD], escalate.
Copy-Ready Prompt Template
A reusable prompt template that detects the output format from truncated content and reconstructs a valid, complete structure.
This prompt template is designed to be injected into a retry or repair harness when a model's output has been cut off due to token limits or context window overflow. Its primary job is to accept the partial, malformed payload, automatically detect the intended format (JSON, XML, YAML, or CSV), and generate a syntactically valid, complete version that preserves the original semantic intent. The template uses square-bracket placeholders for all dynamic inputs, making it safe to copy directly into your application's prompt management system or configuration store.
textYou are a structured output repair agent. Your task is to receive a truncated or incomplete output and reconstruct a valid, complete structure. [INPUT] [CONTEXT] [OUTPUT_SCHEMA] [CONSTRAINTS] [EXAMPLES] [TOOLS] [RISK_LEVEL] INSTRUCTIONS: 1. Detect the output format from the partial content provided in [INPUT]. Supported formats: JSON, XML, YAML, CSV. 2. If format detection is ambiguous, default to the format specified in [OUTPUT_SCHEMA]. If no schema is provided, default to JSON. 3. Analyze the partial content to understand the intended structure, field names, data types, and nesting. 4. Reconstruct a complete, syntactically valid output that: - Closes all open brackets, tags, or delimiters. - Preserves all intact data from the truncated input. - Uses null or sensible placeholder values for fields that were completely missing, unless [CONSTRAINTS] specifies otherwise. - Conforms to the [OUTPUT_SCHEMA] if provided. 5. If [CONTEXT] is provided, use it to infer missing values or structural intent. 6. If [EXAMPLES] are provided, match the style, key naming conventions, and structural patterns shown. 7. If [TOOLS] are specified, you may use them to validate the repaired output. 8. If [RISK_LEVEL] is set to HIGH, mark any inferred or reconstructed values with an "uncertainty" flag or comment as specified in [CONSTRAINTS]. 9. Return ONLY the repaired output. Do not include explanations, apologies, or markdown fences unless the detected format is markdown. 10. If the input is too damaged to repair with confidence, return a JSON object with an "error" key describing the failure and a "partial_repair" key containing the best-effort reconstruction.
To adapt this template for your specific pipeline, replace each placeholder with concrete values. The [INPUT] placeholder should receive the raw truncated string. [OUTPUT_SCHEMA] should contain your expected JSON Schema, XML Schema Definition, or a plain-text description of the expected fields. Use [CONSTRAINTS] to enforce domain-specific rules, such as 'dates must be ISO 8601' or 'do not invent missing financial figures.' The [RISK_LEVEL] flag is critical for high-stakes workflows like healthcare or finance; when set to HIGH, your harness should route the repaired output for human review before it reaches downstream systems. Always validate the repaired output against your schema programmatically after the model responds, and implement a retry budget to prevent infinite repair loops.
Prompt Variables
Placeholders required by the Truncated Structured Output Repair Prompt. Each variable must be populated before the prompt is assembled and sent. Validation notes describe how to check that the variable is correctly set before execution.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[TRUNCATED_OUTPUT] | The partial, truncated model response that needs repair. Must include the incomplete structure. | {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "na | Parse check: must be non-empty string. Schema check: confirm it is a prefix of a valid structured format (JSON, XML, YAML, CSV). If empty or null, abort retry and escalate. |
[EXPECTED_FORMAT] | The target output format to reconstruct. Used when the format is known in advance and detection is not required. | json | Enum check: must be one of json, xml, yaml, csv, or auto. If auto, the prompt must perform format detection from [TRUNCATED_OUTPUT]. If null, default to auto. |
[OUTPUT_SCHEMA] | Optional JSON Schema, XSD, or table definition that the repaired output must validate against. Provides field-level repair guidance. | {"type": "object", "properties": {"users": {"type": "array", "items": {"type": "object", "properties": {"id": {"type": "integer"}, "name": {"type": "string"}}, "required": ["id", "name"]}}}, "required": ["users"]} | Schema parse check: must be valid JSON Schema, XSD string, or CSV column definition. If provided, the repaired output must pass validation. If null, skip schema validation and only check structural completeness. |
[ORIGINAL_TASK] | The original instruction or prompt that produced the truncated output. Provides semantic context for what the complete output should contain. | Generate a JSON array of all active users with their id, name, and email fields. | Null check: must be non-empty string. If missing, the repair prompt can only fix structure, not missing semantic content. Log warning if absent. |
[TRUNCATION_POINT] | Optional marker indicating where truncation occurred, such as a token count, character position, or context window limit. | Output truncated at token 4096 of 8192 context window. | If provided, use to guide reconstruction priority. If null, the prompt must infer the truncation point from the incomplete structure. Accept string or null. |
[MAX_RETRY_TOKENS] | Token budget allocated for the repair attempt. Prevents the repair itself from being truncated. | 2048 | Type check: must be positive integer. If the repair output exceeds this budget, trigger escalation instead of another retry. Default to 4096 if not set. |
[RETRY_ATTEMPT_NUMBER] | Current retry count. Used to adjust repair strategy and prevent infinite loops. | 2 | Type check: must be non-negative integer. If greater than [MAX_RETRIES], abort repair and escalate to human review or fallback model. Increment before each attempt. |
Implementation Harness Notes
How to wire the Truncated Structured Output Repair Prompt into an application with validation, retries, and safe fallbacks.
The Truncated Structured Output Repair Prompt is designed to be called automatically by an application harness when a model response fails a completeness check or a structural validation. The harness must first detect that the output is truncated—typically by checking for balanced brackets, closing tags, or a valid end-of-file marker—and then invoke the repair prompt with the partial output and the expected schema. This is not a prompt for users to call manually; it is a recovery step in an automated pipeline where the cost of a malformed payload is high.
To wire this into an application, wrap the primary model call in a try/catch or validation gate. If the output fails a parse attempt (e.g., json.loads raises JSONDecodeError), pass the raw truncated string, the target format, and the expected schema into the repair prompt. The harness should include a retry budget—typically 1-2 repair attempts—before escalating. After each repair attempt, validate the output again using the same schema. If the repair succeeds, log the event as a recovery_success metric and proceed. If it fails, log the failure with the original and repaired payloads for debugging, then either return a controlled error to the user or route to a human review queue if the workflow is high-risk.
For model choice, use a model with strong instruction-following and code-generation capabilities, such as gpt-4o or claude-3.5-sonnet, since the repair task requires precise syntax reconstruction. Avoid smaller or older models that may hallucinate field values or invent missing data. The repair prompt should be configured with temperature=0 to maximize determinism. If your application uses structured outputs via the API (e.g., OpenAI's response_format or function calling), note that a truncated response may not be returned at all by the provider; in that case, your harness must catch the API error and retry the original request with a reduced context window before attempting repair.
Validation and evals are critical. After repair, run the output through a schema validator (e.g., jsonschema for JSON, lxml for XML) and a semantic completeness check: are all required fields present? Do enumerated values match the allowed set? Are numeric ranges respected? For high-stakes workflows, add a secondary LLM-as-judge eval that scores the repaired output against the original partial content to ensure no information was fabricated. If the repair prompt introduces data not present in the truncated original, flag it for human review. Never silently accept a repaired output that adds unsupported claims.
Expected Output Contract
Validation rules for the repair prompt output. The model must return a valid, complete structure in the detected format. Use this contract to build a post-processing validator before the repaired output enters downstream systems.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
detected_format | enum: JSON | XML | YAML | CSV | Must match one of the supported format strings exactly. If format cannot be determined, the repair should fail and escalate. | |
repaired_output | string | Must parse successfully using a strict parser for the detected_format. For JSON, use JSON.parse. For XML, use a DOMParser. For YAML, use a safe YAML loader. For CSV, use a RFC 4180 parser. | |
repair_notes | array of strings | Each note must describe one specific repair action taken (e.g., 'Closed unclosed string at line 4', 'Inferred missing closing bracket for object at path $.items[2]'). Empty array is acceptable if no repairs were needed. | |
truncation_point | object | If the input was truncated, this object must contain 'line' (integer) and 'reason' (string) fields. If the input was not truncated, this field must be null. | |
schema_compliance | boolean | If a [TARGET_SCHEMA] is provided, the repaired_output must validate against it. Set to true if validation passes, false if it fails. If no schema is provided, set to null. | |
confidence_score | number | A float between 0.0 and 1.0 indicating the model's confidence in the repair. Scores below [CONFIDENCE_THRESHOLD] should trigger human review or escalation. | |
original_error | string | The exact error message from the parser that rejected the original truncated output. Required if the repair was triggered by a parse failure. Set to null if the repair was proactive. |
Common Failure Modes
Truncated structured output repair fails in predictable ways. Here are the most common failure modes and how to guard against them before they reach production.
Format Misdetection from Partial Content
What to watch: The repair prompt misidentifies the output format when only a fragment is available—confusing truncated YAML for malformed JSON, or treating a CSV header as a key-value pair. Format detection heuristics fail when structural delimiters are missing. Guardrail: Provide an explicit [OUTPUT_FORMAT] parameter in the repair prompt rather than relying on auto-detection. If auto-detection is required, validate the detected format against a known schema registry before attempting repair.
Hallucinated Field Completion
What to watch: The model invents plausible values for truncated fields—generating fake dates, IDs, or numeric values to close an incomplete JSON object. This is especially dangerous when the original data contained ground-truth values that were cut off mid-stream. Guardrail: Instruct the repair prompt to use explicit null or "TRUNCATED" sentinel values for any field it cannot recover from the partial content. Post-repair validation should flag sentinel values for human review or source re-fetch.
Structural Overcorrection
What to watch: The repair prompt adds too many closing brackets, quotes, or tags—producing valid syntax that no longer matches the original schema. A truncated array of three objects might be repaired into a syntactically valid but semantically wrong structure with extra empty elements. Guardrail: Validate the repaired output against the expected [OUTPUT_SCHEMA] immediately after repair. Check array lengths, required field presence, and type consistency. Reject repairs that pass syntax checks but fail schema validation.
Context Loss Across Repair Boundaries
What to watch: The repair prompt loses semantic context from the truncated portion—entity references, cross-field dependencies, or enumeration states that were established earlier in the output. The repaired structure is valid but logically inconsistent with the original intent. Guardrail: Include the full original prompt and any preceding context alongside the truncated output in the repair request. Add a consistency check that verifies cross-field constraints (e.g., start date before end date, IDs matching referenced entities).
Infinite Repair Loop on Unrecoverable Truncation
What to watch: The repair prompt produces output that still fails validation, triggering another repair attempt, which fails again—creating a retry loop that burns tokens without converging. This happens when the truncation point destroys critical structural information that cannot be inferred. Guardrail: Set a hard retry budget of 2-3 repair attempts maximum. After the budget is exhausted, escalate to a fallback path: request a fresh generation with reduced context, return a partial result with error metadata, or route to human review.
Silent Schema Drift After Repair
What to watch: The repaired output passes initial validation but contains subtle schema violations—enum values shifted to nearby alternatives, numeric precision lost, or optional fields silently dropped. Downstream consumers accept the payload but produce incorrect results. Guardrail: Run the repaired output through the same full validation pipeline used for non-truncated outputs. Add field-level diffing against the partial original to detect unexpected value changes. Log repair events with before/after snapshots for auditability.
Evaluation Rubric
Use this rubric to test the Truncated Structured Output Repair Prompt before shipping. Each criterion targets a specific failure mode in format detection, structural reconstruction, or schema compliance.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Format Detection Accuracy | Correctly identifies JSON, XML, YAML, or CSV from a truncated prefix of at least 50 characters | Misclassifies format or returns 'unknown' for a valid partial prefix | Run 20 truncated samples across all four formats; require >=95% accuracy |
Structural Closure Completeness | Output is parseable by a standard parser for the detected format with zero syntax errors | Parser throws an exception due to unclosed brackets, tags, or quotes | Parse the repaired output with |
Schema Compliance After Repair | Repaired output validates against the provided [OUTPUT_SCHEMA] with no missing required fields | Required field is absent or a field has an incorrect type | Validate repaired output against the JSON Schema or equivalent schema definition; zero violations allowed |
Semantic Content Preservation | All key-value pairs, list items, and text nodes present in the truncated input appear unchanged in the repaired output | A value from the truncated input is altered, reordered, or dropped | Diff the truncated input content against the repaired output; require exact match for all present fields |
Enum and Constraint Adherence | Repaired values for enum fields and constrained types match allowed values from [OUTPUT_SCHEMA] | An enum field contains a value not in the allowed list or a string exceeds maxLength | Check all enum and constrained fields against schema definitions; zero violations allowed |
Null and Missing Field Handling | Fields absent from the truncated input are populated with | A missing field is filled with a hallucinated value instead of | Identify fields present in schema but absent in truncated input; verify each is |
Retry Budget Exhaustion Behavior | After [MAX_RETRIES] attempts, the prompt returns a partial result with an | Prompt retries indefinitely or returns a fabricated complete output | Set [MAX_RETRIES]=3 with intentionally unrecoverable truncation; verify flag is set and no further retries occur |
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 single output format (e.g., JSON only) and skip multi-schema validation. Test with manually truncated outputs at known cut points.
Prompt modification
- Remove
[ALTERNATE_FORMATS]placeholder and hardcode one format - Replace
[OUTPUT_SCHEMA]with a single inline schema - Add:
If you cannot determine the closing structure, return the partial content wrapped in a best-effort repair with arepair_confidencefield.
Watch for
- Format misdetection when truncation occurs mid-keyword
- Overly aggressive bracket insertion that changes data types
- No eval cases defined yet

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