Inferensys

Prompt

Multi-Format Output Repair Router Prompt

A practical prompt playbook for using the Multi-Format Output Repair Router Prompt in production AI workflows to classify malformed outputs and route them to the correct repair sub-prompt.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Define the job, reader, and constraints for the Multi-Format Output Repair Router.

This prompt is for AI platform architects and backend engineers who operate inference pipelines that consume structured output from multiple models or endpoints. The job-to-be-done is format-agnostic repair routing: a single entry point receives a malformed payload—which could be JSON, XML, YAML, CSV, or a typed object—and must classify the format, route it to the correct repair sub-prompt, and return a unified repair result. The ideal user is someone building a production repair harness who cannot afford to maintain separate classification logic for each format and needs a single prompt that acts as the dispatcher before specialized repair begins.

Use this prompt when your pipeline accepts heterogeneous structured outputs and validation failures can occur in any format. It is appropriate when you already have format-specific repair prompts (e.g., a JSON Schema Validation Retry Prompt, an XML Malformed Structure Correction Prompt, a CSV Field Count Mismatch Repair Prompt) and need a router to avoid manual triage. The prompt requires the raw malformed output, the expected output schema or format specification, and any validator error messages as input. It returns a structured routing decision with the detected format, a pointer to the appropriate repair sub-prompt, and a pre-populated repair context.

Do not use this prompt when you only deal with a single output format—a dedicated repair prompt will be simpler and more reliable. Avoid it when the malformed output is so severely corrupted that format detection itself is unreliable; in those cases, escalate directly to a human or a partial salvage workflow. This prompt is a router, not a repairer: it classifies and dispatches, but the actual correction happens downstream. If your retry budget is exhausted or the confidence in format detection is low, the prompt should return an escalation decision rather than guessing the format. Wire the output into a harness that logs the routing decision, tracks which repair sub-prompt was invoked, and measures end-to-end repair success so you can identify format-specific failure patterns over time.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Multi-Format Output Repair Router Prompt works and where it introduces more risk than it solves.

01

Good Fit: Heterogeneous Pipelines

Use when: your platform ingests model outputs from multiple providers or internal services that produce JSON, XML, YAML, or CSV. Why: a single router avoids duplicating format-specific repair logic across every integration point.

02

Good Fit: Pre-Parse Sanitization

Use when: downstream parsers reject malformed payloads before you can inspect the content. Why: the router classifies the format and dispatches to the correct repair sub-prompt before the payload reaches strict deserializers.

03

Bad Fit: Single-Format Workloads

Avoid when: your pipeline only ever produces one structured format. Why: the classification step adds latency and a potential misclassification failure mode without providing routing value. Use a format-specific repair prompt directly.

04

Required Inputs

Must provide: the malformed output string, the expected format hint if known, and the target schema or validation rules for the repair sub-prompt. Guardrail: missing schema input forces the router to return a best-effort repair with a low-confidence flag rather than a guaranteed-valid payload.

05

Operational Risk: Misclassification Cascades

Risk: the router classifies a JSON payload as XML and dispatches it to the wrong repair sub-prompt, producing a garbled result. Guardrail: add a format-consistency check after repair that re-validates the output against the declared format before returning it to the caller.

06

Operational Risk: Repair Drift

Risk: repeated repair attempts on the same payload can drift the semantic content further from the original intent. Guardrail: cap the router at one classification plus one repair attempt per payload, and escalate to human review if the repaired output still fails validation.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable router prompt that classifies malformed output, dispatches to a format-specific repair sub-prompt, and returns a unified repair result with diagnostics.

This prompt acts as the central dispatcher in a multi-format repair pipeline. Instead of guessing which repair strategy to apply, the model first inspects the failed output and any validation errors to determine the target format, then routes the payload to the appropriate repair sub-prompt. The output is a single structured envelope containing the corrected payload, the detected format, a repair confidence score, and any format-specific diagnostics that downstream systems need for logging or escalation.

code
You are a format-agnostic output repair router. Your job is to inspect a failed model output, determine its intended format, and route it to the correct repair procedure.

## INPUT
Failed Output:

[FAILED_OUTPUT]

code

Validation Errors (if available):

[VALIDATION_ERRORS]

code

Original Prompt Context (for semantic preservation):
[ORIGINAL_PROMPT_CONTEXT]

## TARGET SCHEMA (optional, if known)
[TARGET_SCHEMA]

## ROUTING RULES
1. Detect the intended format from the failed output and any error messages. Supported formats: JSON, XML, YAML, CSV, Typed Object (TypeScript/Python/Java interface).
2. If the format is ambiguous, default to the format specified in [TARGET_SCHEMA]. If still ambiguous, return a `format_undetermined` error.
3. Route to the appropriate repair sub-prompt based on detected format:
   - JSON: Fix trailing commas, unquoted keys, single quotes, bracket mismatches, and schema violations.
   - XML: Repair unclosed tags, missing root elements, namespace issues, and CDATA errors.
   - YAML: Normalize indentation, fix tab characters, correct nesting levels.
   - CSV: Realign columns, fix field count mismatches, handle quote escaping.
   - Typed Object: Rename properties, remove readonly fields, resolve union types, inject optional defaults.
4. Preserve all semantic content. Do not invent data to fill gaps unless a safe default is specified in [TARGET_SCHEMA].
5. If the output is truncated (incomplete), flag it as `truncated` and repair only the recoverable portion.

## OUTPUT SCHEMA
Return a single JSON object with this exact structure:
{
  "detected_format": "json" | "xml" | "yaml" | "csv" | "typed_object" | "format_undetermined",
  "repair_success": true | false,
  "repaired_output": "<the corrected payload as a string>",
  "repair_confidence": 0.0 to 1.0,
  "repair_actions": [
    {
      "action": "<description of what was fixed>",
      "location": "<field path, line number, or XPath>",
      "original": "<the broken fragment>",
      "repaired": "<the corrected fragment>"
    }
  ],
  "unrecoverable_fields": ["<field paths that could not be repaired>"],
  "truncated": true | false,
  "escalation_reason": "<null or reason if repair_success is false>"
}

## CONSTRAINTS
- Do not modify the semantic meaning of the original output.
- If a field value is ambiguous, prefer preserving the original over guessing.
- Flag any destructive changes in `repair_actions` with a note.
- If [RISK_LEVEL] is "high", set `repair_confidence` conservatively and escalate borderline cases.

To adapt this template, replace the square-bracket placeholders with your pipeline's actual inputs. [FAILED_OUTPUT] should receive the raw model response that failed validation. [VALIDATION_ERRORS] can be the raw error output from libraries like Ajv, Pydantic, or Xerces—the router uses these to confirm the format and identify specific fault locations. [ORIGINAL_PROMPT_CONTEXT] is critical for semantic preservation; include the user's original request and any system instructions so the repair doesn't drift from intent. If you know the target schema, provide it in [TARGET_SCHEMA] to eliminate format ambiguity. Set [RISK_LEVEL] to "high" for regulated or customer-facing workflows to force conservative confidence scoring and escalation of borderline repairs. Wire this prompt as the first stage in a repair harness, then pass the repaired_output field to your downstream parser after checking repair_success and repair_confidence against your operational thresholds.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the Multi-Format Output Repair Router Prompt. Each placeholder must be populated before the router can classify the malformed output and route it to the correct repair sub-prompt.

PlaceholderPurposeExampleValidation Notes

[MALFORMED_OUTPUT]

The raw, invalid model response that failed downstream parsing or validation

{"name": "Acme Corp", "revenue": "100K" "employees": 50}

Must be a non-empty string. If the output is truncated, include the partial payload and set [IS_TRUNCATED] to true

[TARGET_FORMAT_HINT]

The expected output format the model was originally instructed to produce

JSON

Must be one of: JSON, XML, YAML, CSV, TYPED_OBJECT. Used for initial format detection before deeper inspection

[VALIDATION_ERRORS]

Raw error messages from the validator that rejected the output

["Unexpected token '"employees"' at line 1 col 42", "Missing comma before property"]

Array of strings or null if no validator ran. When null, the router performs format detection only without error-guided repair routing

[TARGET_SCHEMA]

The expected schema, interface, or DTD the output must conform to

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

JSON Schema, XSD string, TypeScript interface definition, or CSV header row. Null allowed when no schema is available; router will skip schema-guided repair and use format-only repair

[ORIGINAL_PROMPT_CONTEXT]

The original prompt or instruction that produced the malformed output, used to preserve semantic intent during repair

Generate a JSON object with company name, revenue string, and employee count from the following text: Acme Corp earned 100K last year with 50 staff.

Must be a non-empty string. Critical for repair sub-prompts that need to infer missing fields or correct semantic drift without hallucinating

[RETRY_BUDGET]

Maximum number of repair attempts allowed before escalation

3

Integer between 1 and 5. Router uses this to decide whether to route to a repair sub-prompt or directly to the escalation prompt when budget is exhausted

[IS_TRUNCATED]

Flag indicating whether the malformed output was cut off by token limits or streaming interruption

Boolean. When true, router prioritizes the Structured Output Truncation Repair Prompt and sets partial-salvage expectations

[REPAIR_CONFIDENCE_THRESHOLD]

Minimum confidence score required to accept a repaired output without human review

0.85

Float between 0.0 and 1.0. Router passes this to the Repair Confidence Score Prompt; outputs below threshold are routed to escalation with a low-confidence reason code

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the Multi-Format Output Repair Router into a production inference pipeline with validation, routing, retries, and escalation.

The Multi-Format Output Repair Router is designed to sit immediately after a primary model response and its first-pass validation failure. In a production harness, you should not call this prompt directly from user-facing code. Instead, wrap it in a repair service that receives the raw model output, the expected output schema (JSON Schema, XSD, or a typed interface definition), and the original validation error. The service then calls this router prompt to classify the format, select the appropriate repair sub-prompt, and return a unified repair result. This indirection lets you swap repair strategies, add format-specific preprocessors, and log every repair attempt without changing the router prompt itself.

The harness must enforce a strict retry budget. Configure a maximum of 2-3 repair attempts per output. On each attempt, feed the router's repair_result and format_diagnostics back into the appropriate sub-prompt if the output still fails validation. Between attempts, increment a counter and check it against your threshold. If the budget is exhausted, do not retry again. Instead, invoke the Escalation Threshold Prompt to decide whether to return a partial result, request human review, or fail the request with a structured error. Log every attempt, the repair actions taken, and the final disposition for observability and debugging.

For model choice, use a model with strong instruction-following and structured output capabilities—GPT-4o, Claude 3.5 Sonnet, or Gemini 1.5 Pro are good defaults. Avoid smaller or older models that may struggle with multi-step classification and repair tasks. Set temperature to 0 or near-zero to minimize variability in repair decisions. If your pipeline uses tool calling, the router can be implemented as a tool that the primary agent calls when its output fails validation, but prefer a dedicated repair service for reliability and independent retry control.

Validation is the gate. After the router returns a corrected_payload, run it through the same schema validator that rejected the original output. If it passes, return the payload to the calling application. If it fails, check the repair_confidence score. For high-confidence repairs (≥0.9) that still fail, log the mismatch as a potential validator bug or schema ambiguity. For low-confidence repairs (<0.7), escalate immediately without consuming further retry budget. Always attach the format_diagnostics and repair_log to your observability traces so operators can audit what the router changed and why.

When wiring this into a production system, ensure the harness handles partial failures gracefully. If the router itself times out or returns malformed JSON, catch that exception, log the raw response, and fall back to the Partial Output Salvage Prompt to extract any valid fields. Never let a repair failure crash the entire inference request. Finally, set up a dashboard or alert on repair attempt counts and escalation rates—rising trends often signal schema changes, model drift, or an increase in edge-case inputs that need new few-shot examples in the repair sub-prompts.

IMPLEMENTATION TABLE

Expected Output Contract

Fields, types, and validation rules for the unified repair result returned by the Multi-Format Output Repair Router. Use this contract to build downstream parsers and decide whether to accept, retry, or escalate.

Field or ElementType or FormatRequiredValidation Rule

repair_id

string (UUID v4)

Must be a valid UUID v4 generated by the router. Reject if missing or malformed.

detected_format

enum: json, xml, yaml, csv, typed_object, unknown

Must match one of the allowed enum values. If unknown, escalate immediately without attempting repair.

original_output

string

Must be a non-empty string containing the raw model output before repair. Null or empty string triggers an input validation error.

repaired_payload

string or null

Must be a valid string in the detected_format when repair_status is success or partial. Must be null when repair_status is failed. Parse check against format-specific validator required.

repair_status

enum: success, partial, failed

Must be one of the three allowed values. If failed, repaired_payload must be null and repair_log must contain at least one entry with severity error.

repair_log

array of repair_entry objects

Must be a non-empty array when repair_status is partial or failed. Each entry must have field, issue, action, and severity properties. Empty array allowed only when repair_status is success.

repair_confidence

number (0.0 to 1.0)

Must be a float between 0.0 and 1.0 inclusive. Values below [CONFIDENCE_THRESHOLD] should trigger human review or escalation. Reject if outside range.

escalation_recommended

boolean

Must be true when repair_confidence is below [CONFIDENCE_THRESHOLD] or repair_status is failed. Downstream systems must check this flag before consuming repaired_payload.

PRACTICAL GUARDRAILS

Common Failure Modes

When a multi-format repair router fails, the entire downstream pipeline stalls. These are the most common breakpoints and how to prevent them before they reach production.

01

Format Misclassification

What to watch: The router incorrectly identifies a malformed JSON payload as XML or plain text, routing it to the wrong repair sub-prompt. This often happens with truncated outputs or mixed-format responses. Guardrail: Implement a two-pass classification with a confidence threshold. If confidence is below 0.9, run a secondary structural heuristic (bracket counting, tag detection) before routing.

02

Silent Semantic Drift During Repair

What to watch: The repair sub-prompt fixes the structural error but alters a field's meaning, such as coercing a null string to a literal null or mapping an enum value to a semantically different option. The output validates but the data is wrong. Guardrail: Require a before/after diff log for every repaired field. Flag any field where the repair changed the value rather than just the structure for human review.

03

Retry Loop Exhaustion Without Escalation

What to watch: The router retries the same repair sub-prompt repeatedly, consuming the entire retry budget without making progress. This occurs when the error message is unparseable or the model cannot converge on a valid fix. Guardrail: Track repair attempt deltas. If the same validation error persists for two consecutive attempts, escalate immediately with a structured reason code rather than continuing the loop.

04

Partial Repair Masking Total Failure

What to watch: The repair sub-prompt successfully fixes some fields but silently drops or nullifies others that were irreparable. The output passes validation but is incomplete, and downstream systems process partial data as if it were complete. Guardrail: Always return a repair_coverage score indicating what percentage of original fields were preserved. Require explicit acknowledgment of dropped fields in the repair result.

05

Schema Version Mismatch in Repair Context

What to watch: The router passes the wrong schema version to the repair sub-prompt, causing valid fields to be flagged as errors or new required fields to be ignored. This is common in CI/CD pipelines where schemas evolve independently of the repair harness. Guardrail: Embed the schema version in the repair request and validate it matches the expected version before routing. Reject repair requests with mismatched schema identifiers.

06

Unified Result Payload Inconsistency

What to watch: The router aggregates repair results from different format-specific sub-prompts into a unified response, but the output schema varies by format, causing field name collisions or missing diagnostic fields. Guardrail: Define a strict unified result envelope with required fields: corrected_payload, format_detected, repair_actions, confidence, and escalation_flag. Reject any sub-prompt output that does not conform to this envelope.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for evaluating the quality of the Multi-Format Output Repair Router Prompt before deployment. Use these tests to ensure the router correctly classifies, routes, and returns unified repair results without introducing new errors.

CriterionPass StandardFailure SignalTest Method

Format Classification Accuracy

Correctly identifies the malformed format (JSON, XML, YAML, CSV) for all supported types, including edge cases like truncated outputs.

Router misclassifies format (e.g., labels truncated JSON as plain text) or returns 'unknown' for a supported format.

Run against a golden set of 20 malformed payloads across all supported formats. Assert classification accuracy >= 95%.

Routing Correctness

Routes the repair request to the correct sub-prompt based on the classified format. The sub-prompt name in the output matches the expected repair strategy.

Router sends a JSON error to the XML repair sub-prompt, or the output references an incorrect or non-existent sub-prompt.

Trace the routing decision for each golden-set payload. Assert that the routed_to field matches the expected sub-prompt for the given format.

Unified Result Schema Conformance

Output strictly conforms to the defined unified repair result schema, including original_format, routed_to, repaired_payload, and diagnostics fields.

Output is missing a required field, contains extra fields, or uses incorrect types (e.g., repaired_payload is a string instead of an object).

Validate the router's JSON output against the unified result JSON Schema. Assert zero schema violations.

Semantic Content Preservation

The repaired_payload in the unified result is semantically identical to the original malformed output, with only syntactic corrections applied.

Repaired payload contains hallucinated data, missing key-value pairs, or altered numerical values not related to format repair.

Perform a field-by-field diff between the original (pre-corruption) source data and the final repaired_payload. Assert no semantic drift beyond format coercion.

Diagnostics Completeness

The diagnostics field contains a list of all identified errors, each with a type, location, and action_taken field.

Diagnostics list is empty when errors were present, or an error is missing its action_taken description.

For each golden-set payload with known errors, assert that the number of diagnostic entries matches the number of injected errors.

Repair Confidence Flagging

The unified result includes a repair_confidence score (0.0-1.0) that accurately reflects the severity and number of repairs performed.

Confidence is always 1.0 even for destructive repairs, or is 0.0 for a trivial comma fix.

Correlate the repair_confidence score with the count and type of diagnostics. Assert that high-severity repairs (e.g., data type coercion) lower the score more than low-severity fixes (e.g., trailing commas).

Escalation Threshold Adherence

When the router determines an output is unrepairable (e.g., >50% data loss), it returns an escalation decision instead of a best-effort repair.

Router attempts to repair a completely garbled payload, producing a hallucinated or empty result instead of escalating.

Inject payloads with catastrophic corruption (e.g., random binary data). Assert that the output contains an escalation object with a reason_code and no repaired_payload.

Idempotency of Repair

Running the router on an already-valid payload returns the original payload unchanged with a repair_confidence of 1.0 and an empty diagnostics list.

Router modifies a valid payload, adds unnecessary fields, or reports false-positive errors.

Pass a set of perfectly valid JSON, XML, YAML, and CSV payloads through the router. Assert that repaired_payload is byte-for-byte identical to the input.

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single model and a simple routing map. Hardcode format detection with regex before calling the LLM router. Skip the repair confidence score and partial salvage logic. Return the corrected payload and a short diagnostic string.

Prompt modification

Replace the router's format-detection step with a pre-processing rule: if the raw output starts with { or [, route to JSON repair; if it starts with <, route to XML repair. Remove the [REPAIR_CONFIDENCE] and [PARTIAL_SALVAGE] sections.

Watch for

  • Format misclassification when output has leading whitespace or markdown fences
  • Router sending valid output through repair unnecessarily
  • No retry budget, so one failure ends the pipeline
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.