This prompt is for integration engineers and agent-harness builders who need to recover from invalid JSON tool calls without losing the original intent. The job-to-be-done is straightforward: a model produced a tool call that failed validation, you have the exact error message from the validator, and you need a corrected JSON payload that passes the same schema. The prompt is designed to reuse the error message as the primary repair signal rather than asking the model to guess what went wrong. This works best when the error message is precise—think JSON Schema validation errors, missing required field messages, or type mismatch reports—not vague runtime exceptions.
Prompt
Tool Call JSON Repair Prompt with Error Message Reuse

When to Use This Prompt
Define the job, reader, and constraints for repairing invalid JSON tool calls using validation error messages as direct feedback.
The ideal user is someone wiring tool-call execution into a production harness. You already have a validation step that rejects malformed calls and produces structured error output. You need a retry instruction that takes the original attempted call, the tool schema, and the validator's error message, then produces a corrected call without hallucinating new parameters or silently dropping required fields. This prompt is not for first-attempt tool-call generation. Use a standard function-calling prompt for initial calls. This prompt is specifically for the repair loop after validation fails. It is also not a substitute for fixing the underlying schema mismatch if your tool definitions have changed—pair this with schema version tracking and deprecation handling.
Do not use this prompt when the error message is empty, misleading, or comes from an external API that returns HTTP 500 with no body. The repair quality depends entirely on the error signal quality. If the validator says 'invalid JSON' without a position or reason, the model has little to work with. In those cases, fall back to a structural repair prompt that re-parses the raw string. Also avoid this prompt when the original tool call was hallucinated—if the model invented a function name that doesn't exist, use a tool-selection recovery prompt instead. For high-stakes tool calls that mutate data, schedule payments, or change production config, always route the repaired output through human approval before execution, regardless of validation pass.
Use Case Fit
Where the Tool Call JSON Repair Prompt with Error Message Reuse works well, where it fails, and the operational preconditions required before wiring it into a production harness.
Good Fit: Deterministic Validation Failures
Use when: The tool call JSON is rejected by a schema validator, JSON parser, or API gateway with a specific, machine-readable error message. Guardrail: Inject the exact validator output into the repair prompt. The model can map missing required property 'user_id' directly to a corrected payload.
Bad Fit: Semantic or Intent Errors
Avoid when: The JSON is structurally valid but the arguments are logically wrong (e.g., wrong user ID, incorrect date range). Guardrail: Structural repair cannot fix intent. Route to a separate disambiguation or clarification prompt that re-evaluates conversation context, not just the error message.
Required Input: The Raw Error Message
Risk: Repairing without the exact validator output forces the model to guess what broke, often producing the same invalid shape. Guardrail: Always pass the unmodified error string from the parser, API, or JSON Schema validator into the [ERROR_MESSAGE] placeholder. Never paraphrase or summarize it.
Required Input: The Original Invalid Payload
Risk: Discarding the original arguments loses user intent embedded in the malformed call. Guardrail: Include the full original [INVALID_PAYLOAD] in the repair prompt so the model can preserve valid fields and correct only the broken ones. Partial repair is safer than regeneration from scratch.
Operational Risk: Multi-Error Overwhelm
Risk: A payload with 20 validation errors produces a long error block that can confuse the model or exceed context limits. Guardrail: Prioritize errors by severity (required fields first, then type mismatches, then enum violations). Inject only the top N errors into the repair prompt and re-validate after each fix.
Operational Risk: Repair Loop Without Escape
Risk: A malformed payload that fails validation, gets repaired, and fails again can loop indefinitely. Guardrail: Set a retry budget (e.g., 3 attempts). After exhaustion, log the final payload and error, then escalate to a human review queue or a fallback clarification prompt. Never retry silently beyond the budget.
Copy-Ready Prompt Template
A reusable prompt that repairs an invalid JSON tool call using the exact validation error message as feedback.
This prompt template is designed to be injected into a retry loop after a model-generated tool call fails JSON schema validation. Instead of asking the model to guess what went wrong, you provide the raw invalid JSON and the precise error message from your validator. The model's only job is to apply the error message to fix the structural problem while preserving the original intent and argument values. This approach reduces repair drift—where the model silently changes correct fields while fixing broken ones—and keeps the repair loop bounded.
textYou are a JSON repair agent. Your task is to fix an invalid JSON tool call using the exact validation error provided. ## INPUT - Invalid JSON: [INVALID_JSON] - Validation Error: [ERROR_MESSAGE] - Expected Tool Schema: [TOOL_SCHEMA] ## CONSTRAINTS 1. Change ONLY what is necessary to resolve the validation error. 2. Preserve all original argument values unless they directly cause the error. 3. If the error message references a specific field path, fix only that path. 4. If the error is ambiguous or you cannot determine the correct fix, output a clarification request instead of guessing. 5. Do not add, remove, or rename fields that are not mentioned in the error message. 6. Return ONLY the corrected JSON object. No markdown fences, no commentary. ## OUTPUT FORMAT If repairable: { "status": "repaired", "corrected_tool_call": { ... } } If clarification needed: { "status": "clarification_needed", "ambiguous_field": "[FIELD_PATH]", "question": "[CLEAR_QUESTION]" }
To adapt this template, replace [INVALID_JSON] with the raw string that failed validation, [ERROR_MESSAGE] with the exact output from your JSON schema validator (e.g., Ajv, Pydantic, or jsonschema), and [TOOL_SCHEMA] with the expected schema definition. For multi-error scenarios, prioritize errors by severity—structural errors like missing closing braces before type errors—and inject them as a numbered list. If you're using this in a production harness, always validate the corrected_tool_call output against the same schema before submitting it for execution. A repair that introduces a new error is a failure that should increment your retry counter and may require a different recovery strategy.
Prompt Variables
Variables required to construct the Tool Call JSON Repair Prompt. Inject these into the prompt template to provide the model with the exact context needed to repair the invalid tool call.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[INVALID_JSON_PAYLOAD] | The raw, malformed JSON string that failed validation. This is the exact text the model must repair. | {"tool": "search", "args": {"q": "AI trends" "limit": 10}} | Must be a non-empty string. Validate that it is the exact payload that triggered the error, not a sanitized version. |
[VALIDATION_ERROR_MESSAGE] | The exact error string returned by the JSON parser or schema validator. Provides the model with specific failure details. | Expecting ',' delimiter: line 1 column 35 (char 34) | Must be a non-empty string. Check that the error message corresponds to the [INVALID_JSON_PAYLOAD] to prevent misleading feedback loops. |
[TOOL_SCHEMA] | The expected JSON Schema definition for the tool's arguments. Grounds the repair in the target structure. | {"type": "object", "properties": {"q": {"type": "string"}, "limit": {"type": "integer"}}, "required": ["q"]} | Must be a valid JSON Schema object. If null, the repair will only target generic JSON syntax errors. Validate schema before injection. |
[ORIGINAL_USER_INTENT] | The user's original request that led to the tool call. Provides semantic context to prevent the repair from drifting from the user's goal. | Find the latest news about AI trends, limit to 10 results. | Must be a non-empty string. This is critical for preventing the model from 'hallucinating' a correction that is syntactically valid but semantically wrong. |
[CONVERSATION_HISTORY] | The preceding turns in the conversation. Provides additional context for resolving ambiguous arguments or intent. | [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}] | Optional. If provided, must be a valid array of message objects. Can be truncated to the last N turns to manage context window. |
[MAX_RETRY_ATTEMPT] | The current retry count. Injected to inform the model of the urgency and prevent repetitive, non-converging repairs. | 2 | Must be an integer string. Use this to trigger a change in repair strategy (e.g., 'be more conservative' or 'escalate') when the value is high. |
[ERROR_PRIORITY_HINT] | A hint for prioritizing fixes when multiple validation errors exist. Guides the model to fix the most critical error first. | Fix syntax errors before addressing schema violations. | Optional. If provided, must be a short, imperative string. Use when a single payload has multiple errors to prevent partial fixes that still fail. |
Implementation Harness Notes
How to wire the JSON repair prompt into a production agent harness with validation, retry budgets, and error injection.
This prompt is designed to sit inside a tool-call execution loop—the harness code that receives a raw model response, attempts to parse and execute the tool call, and catches validation errors. When a JSON parse failure or schema validation error occurs, the harness should not immediately discard the attempt. Instead, it captures the exact error message from the parser or validator, injects it into the [ERROR_MESSAGE] placeholder alongside the original [INVALID_JSON] payload, and re-invokes the model with this repair prompt. The model receives the specific failure reason (e.g., "missing required property 'user_id' at line 12, column 5") rather than a generic "invalid JSON" message, which dramatically improves repair accuracy.
Harness integration steps: (1) Wrap the initial tool-call attempt in a try/catch or schema validation block. (2) On failure, extract the raw error string from your JSON parser (e.g., json_decode_last_error_msg() in PHP, JSONDecodeError in Python, or the validator library's error output). (3) Populate [INVALID_JSON] with the exact string the model produced and [ERROR_MESSAGE] with the parser's error. (4) Send the repair prompt to the model and parse the response. (5) Re-validate the repaired output against the original tool schema before execution. Critical constraint: Never execute a repaired tool call without re-validation—the repair prompt can itself produce malformed output, especially on the first retry. If the repaired output fails validation again, increment a retry counter and repeat the loop with the new error message. Stop after a configurable MAX_RETRIES (typically 2–3) and escalate to a human or fallback workflow.
Multi-error prioritization: When a validator returns multiple errors (e.g., three missing fields and one type mismatch), do not dump all errors into the prompt at once. Inject the first blocking error on retry 1, the second on retry 2, and so on. This progressive disclosure prevents the model from becoming confused by overlapping correction instructions. If the same error persists across retries, the harness should log the staleness and escalate—the model is likely hallucinating a fix rather than applying the error message. Logging and observability: Record each retry attempt with the original invalid JSON, the error message injected, the repaired output, and the validation result. This trace is essential for debugging prompt drift and identifying tool schemas that are inherently difficult for the model to satisfy. Model choice: Use a model with strong instruction-following and JSON generation capabilities. Smaller or older models may struggle with error-message interpretation and produce repairs that pass validation but lose semantic intent—always eval repair accuracy, not just schema conformance.
Expected Output Contract
The repaired JSON payload must conform to this contract before being resubmitted to the tool harness. Validate each field against the rules below.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
tool_call_id | string | Must match the original failed call ID exactly; no modification allowed | |
function_name | string | Must exist in the provided tool catalog; reject if hallucinated | |
arguments | object | Must be valid JSON; parse check required before schema validation | |
arguments.* | any | Each field must conform to the tool schema; remove any field not in the schema | |
arguments.required_fields | per schema | All required parameters must be present; null allowed only if schema permits | |
arguments.enum_fields | per schema | Must match an allowed enum value; fuzzy match to closest valid value if mismatch detected | |
arguments.type_coercion | per schema | Strings representing numbers must be cast to number type; booleans must be true or false, not strings | |
repair_metadata | object | Optional wrapper for repair trace; include error_message_used and repair_attempt_number if present |
Common Failure Modes
Tool-call JSON repair loops fail in predictable ways. These are the most common failure modes when reusing validation error messages as repair feedback, along with practical guardrails to prevent them.
Error Message Overload Causes Drift
What to watch: Dumping a full multi-line JSON Schema validation trace into the repair prompt overwhelms the model. It fixes the first reported error but introduces new structural mistakes elsewhere, or hallucinates fields to satisfy vague constraints. Guardrail: Inject only the first N errors (start with 1-3), sorted by JSONPath depth. Use a structured error format with path, expected, and received fields rather than raw validator output.
Repair Loop Converges to Wrong Shape
What to watch: The model produces valid JSON that passes schema checks but no longer represents the original tool-call intent. Parameters get dropped, values get replaced with safe defaults, or the wrong tool is selected to make validation pass. Guardrail: After repair, diff the argument keys against the original failed call. Flag any dropped required parameters. Run a semantic similarity check between original and repaired argument values before submitting the retry.
Infinite Retry on Unfixable Errors
What to watch: Some validation errors are unfixable by repair alone—missing required context, conflicting constraints, or tool schema bugs. The repair loop retries until the budget is exhausted, wasting latency and compute with no path to success. Guardrail: Classify errors before retrying. Permanent errors (missing required context, schema version mismatch, tool not found) should escalate immediately. Only retry transient errors (type coercion, enum mismatch, structural malformation).
Error Message Leaks Internal State
What to watch: Raw validation errors often contain internal field names, file paths, stack traces, or schema fragments that shouldn't be exposed to the model. This can cause prompt injection risks or teach the model to exploit internal validation logic. Guardrail: Sanitize error messages before injection. Map internal field names to the public tool schema names. Strip file paths, line numbers, and stack traces. Use a whitelist of safe error attributes.
Context Window Exhaustion from Error Accumulation
What to watch: Each retry appends the previous attempt and its error to the prompt. After 3-4 retries, the context window fills with failed attempts, leaving no room for the actual tool schema or conversation context. Guardrail: Truncate prior attempts to only the failing path and error. Drop successful sibling fields from retry context. Set a hard retry limit (max 3) and escalate with a compressed failure summary rather than full history.
Silent Hallucination of Missing Required Fields
What to watch: When the error message says a required field is missing and the model cannot infer it from context, it sometimes invents plausible values rather than signaling uncertainty. The repaired call passes validation but contains fabricated data. Guardrail: Require the repair prompt to output an uncertain_fields array alongside the repaired JSON. If any required field was inferred rather than sourced from context, flag it for human review or clarification request instead of silent submission.
Evaluation Rubric
Criteria for testing the Tool Call JSON Repair Prompt before shipping. Each row defines a pass standard, a failure signal, and a concrete test method.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Schema Conformance After Repair | Output JSON passes strict validation against the target tool schema with zero errors | Output still contains schema violations such as missing required fields, wrong types, or extra prohibited fields | Run the repaired output through a JSON Schema validator using the exact tool definition. Assert 0 validation errors. |
Error Message Incorporation | The specific violation described in the injected [ERROR_MESSAGE] is resolved in the repaired output | The same error class persists in the repaired output, or the repair addresses a different field than the one flagged | Parse the [ERROR_MESSAGE] for field paths and violation types. Assert the repaired output corrects exactly those violations. |
Intent Preservation | The repaired tool call preserves the original semantic intent, parameter values, and action purpose from the failed attempt | The repair silently drops parameters, changes values to unrelated defaults, or selects a different tool action | Diff the original failed arguments against the repaired arguments. Flag any value changes not directly required by the error message. |
No Hallucinated Fields | The repaired output contains only fields defined in the tool schema, with no invented or hallucinated properties | The repair adds plausible but undefined fields, or invents enum values not present in the schema | Extract all top-level and nested keys from the repaired output. Assert every key exists in the tool schema definition. |
Multi-Error Prioritization | When multiple errors are present, the repair addresses all of them or explicitly notes which require separate resolution | The repair fixes only the first error and ignores subsequent violations in the same payload | Inject a payload with 3 schema violations. Assert the repaired output resolves all 3 or returns a structured partial-fix note. |
Idempotency Under Repeated Repair | Running the repair prompt twice on the same failed input produces the same corrected output | Repeated repair attempts oscillate between different corrections or introduce new errors on each pass | Execute the repair prompt 3 times on the same failed payload. Assert output identity across all 3 runs. |
Clarification Escalation When Unrepairable | When the error cannot be resolved from available context, the prompt returns a structured clarification request instead of guessing | The prompt fabricates plausible values for missing required fields without flagging the uncertainty | Inject a payload missing a required field with no inferable value. Assert the output contains a clarification request marker and does not invent the value. |
Latency Budget Compliance | The repair prompt completes within the configured [RETRY_TIMEOUT_MS] and does not exceed the [MAX_RETRY_TOKENS] budget | The repair prompt times out, truncates mid-JSON, or consumes more tokens than allocated for retry | Run the repair prompt with a 2-second timeout and 500-token limit. Assert completion within both constraints and valid JSON output. |
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
Start with the base repair prompt and a single error message injected into [VALIDATION_ERROR]. Use a lightweight harness that catches JSON parse failures, feeds the raw output and error back to the model, and accepts the first structurally valid response. No schema re-validation yet.
codeYou received this validation error for a tool call: [VALIDATION_ERROR] Original tool call attempt: [FAILED_TOOL_CALL] Repair the JSON to fix the error. Return only the corrected JSON.
Watch for
- The model may fix the reported error but introduce a new one elsewhere
- Without re-validation, downstream parsers will still break
- Error messages from different validators have inconsistent formats—test with real errors from your stack

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