Inferensys

Prompt

Invalid Tool Call Schema Repair Prompt Template

A practical prompt playbook for recovering from tool call schema validation failures in production agent harnesses. Includes the copy-ready template, harness wiring, evaluation rubric, and failure mode analysis.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the precise conditions, required context, and failure boundaries for deploying the Invalid Tool Call Schema Repair Prompt in a production agent harness.

This prompt is a surgical recovery tool for AI engineers building agent harnesses where a model-generated tool call is rejected by a downstream JSON Schema validator. The job-to-be-done is strictly post-validation repair: the model has already attempted a tool call, the application layer has caught a ValidationError, and you need a corrected payload that passes schema checks without losing the original intent. The ideal user is an integration engineer or platform developer who controls the retry loop, has access to the exact validation error message, and can inject the target schema into the model context. This prompt is not for initial tool call generation, nor for debugging why the model produced an invalid call in the first place—those are separate concerns addressed by system prompt design and tool description hygiene.

Use this prompt when you have three concrete artifacts available: the invalid payload exactly as the model returned it, the target JSON Schema that defines the expected structure, and the specific validation error message produced by your validator (e.g., 'required' property 'user_id' is missing, 'status' value 'activee' is not a valid enum, or additionalProperty 'phoneNumber' is not allowed). The prompt works best inside a structured retry loop with a bounded budget—typically 1–3 attempts—because schema repair is a narrow, well-defined task that should converge quickly. Do not use this prompt when the failure is semantic rather than structural (e.g., the tool call is valid JSON but selects the wrong tool or passes logically incorrect arguments), when the tool execution itself timed out or returned a business-logic error, or when the schema is unavailable or too large to fit in the context window alongside the payload and error message.

Before wiring this into production, establish clear retry budget limits and escalation paths. If the repair prompt fails to produce a valid payload after two attempts, escalate to a human operator or log the failure for offline analysis rather than burning tokens in an infinite loop. For high-risk domains—such as finance, healthcare, or infrastructure automation—require human review of any repaired payload before execution, even if it passes schema validation, because structural correctness does not guarantee semantic safety. The next section provides the copy-ready prompt template you can adapt directly into your retry handler.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Invalid Tool Call Schema Repair Prompt Template works, where it fails, and the operational preconditions for safe use.

01

Good Fit: Known Schema, Unknown Payload

Use when: you have a strict, versioned JSON Schema for the target tool and the model produced a structurally invalid call. Guardrail: inject the exact schema and the raw validation error into the repair prompt. Do not rely on the model to recall the schema from memory.

02

Bad Fit: Semantic Intent Drift

Avoid when: the tool call is structurally valid but selects the wrong tool or hallucinates parameter values from thin air. Schema repair fixes shape, not intent. Guardrail: route intent errors to a separate tool-selection recovery prompt before attempting schema repair.

03

Required Input: Validation Error Message

Risk: repairing without the exact error message forces the model to guess what broke, often producing the same invalid shape. Guardrail: always pass the raw validator output as a required input. Include the JSON path of the first failure to focus the repair on the specific fault location.

04

Required Input: Original Intent Context

Risk: the repair prompt may produce a valid but semantically empty payload if it loses the user's original goal. Guardrail: include the conversation turn or task description that triggered the tool call. Instruct the model to preserve all recoverable parameter values from the original attempt.

05

Operational Risk: Repair Loop Exhaustion

Risk: repeated schema repair attempts can cycle without convergence, burning tokens and latency budget. Guardrail: set a hard retry cap (typically 2-3 attempts). After exhaustion, log the full failure chain and escalate to a human operator or fallback workflow. Never loop indefinitely.

06

Operational Risk: Hallucinated Field Injection

Risk: the repair prompt may add plausible but fabricated fields to satisfy the schema. Guardrail: instruct the model to only use values present in the original payload or explicitly derivable from conversation context. Add a post-repair diff check that flags any new top-level keys not present in the original attempt.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready prompt that repairs an invalid tool call by applying schema validation errors to produce a corrected JSON payload.

This prompt template is designed to be injected into your retry handler immediately after a tool call is rejected by your schema validator. It forces the model to confront the exact validation error, the original (invalid) arguments, and the expected schema simultaneously. The goal is not to re-explain the task but to surgically repair the structural defect while preserving the user's original intent and any valid parameters that were already correct.

text
You are a tool-call repair agent. Your only job is to fix an invalid function call argument payload so that it strictly conforms to the provided JSON Schema.

## INPUTS
- **Original User Intent:** [USER_INTENT]
- **Function Name:** [FUNCTION_NAME]
- **Expected JSON Schema:**
  ```json
  [EXPECTED_SCHEMA]
  • Invalid Arguments (failed validation):
    json
    [INVALID_ARGUMENTS]
  • Validation Error Message: [VALIDATION_ERROR]

REPAIR RULES

  1. Schema Conformance: The output must validate cleanly against the Expected JSON Schema. Fix type errors, missing required fields, extra fields, and enum violations.
  2. Intent Preservation: Do not change the semantic meaning of the original arguments. Infer missing required parameters from the User Intent only if the inference is unambiguous. If a required parameter cannot be inferred, set its value to null and add a note to the _repair_warnings array.
  3. Hallucination Prevention: Do not invent values for fields that were not present in the invalid arguments and cannot be directly inferred from the User Intent. Do not add fields that are not defined in the Expected JSON Schema.
  4. Error Addressing: The repair must directly resolve the specific issue described in the Validation Error Message.

OUTPUT FORMAT

Return a single JSON object with exactly two keys:

  • repaired_arguments: The corrected arguments object that strictly conforms to the Expected JSON Schema.
  • _repair_warnings: An array of strings describing any non-critical issues, such as parameters that were set to null due to insufficient context.

EXAMPLE

User Intent: "Find me a direct flight from SFO to JFK tomorrow morning" Function Name: search_flights Expected JSON Schema: {"type": "object", "properties": {"origin": {"type": "string"}, "destination": {"type": "string"}, "departure_date": {"type": "string", "format": "date"}}, "required": ["origin", "destination", "departure_date"]} Invalid Arguments: {"from": "SFO", "to": "JFK"} Validation Error: "Required property 'departure_date' is missing. Unknown properties: 'from', 'to'."

Your Response: { "repaired_arguments": { "origin": "SFO", "destination": "JFK", "departure_date": null }, "_repair_warnings": ["'departure_date' set to null because 'tomorrow morning' is ambiguous without a current date context."] }

To adapt this template, replace the square-bracket placeholders in your application code before sending the request to the model. [EXPECTED_SCHEMA] should be the raw JSON Schema object for the tool's parameters. [VALIDATION_ERROR] is the exact string returned by your JSON Schema validator (e.g., Ajv, jsonschema). The _repair_warnings array in the output is critical for downstream logic—it allows your harness to decide whether to proceed with a null value or escalate for human clarification without needing to parse the model's free-text reasoning. For high-stakes actions where a null required field is unacceptable, configure your harness to treat any non-empty _repair_warnings array as a terminal failure that triggers an escalation prompt.

IMPLEMENTATION TABLE

Prompt Variables

Each placeholder must be populated at runtime before the retry call. Missing or stale values cause the repair prompt to produce another invalid tool call or hallucinate corrections.

PlaceholderPurposeExampleValidation Notes

[ORIGINAL_TOOL_CALL_JSON]

The invalid tool call payload that failed schema validation

{"tool":"search","args":{"q":"acme"}}

Must be valid JSON string. Parse check before injection. Do not prettify or truncate.

[EXPECTED_SCHEMA]

The complete JSON Schema or tool definition the call must conform to

{"type":"object","required":["query"],"properties":{"query":{"type":"string"}}}

Must be the authoritative schema from the tool registry. Version must match the active tool definition.

[VALIDATION_ERROR_MESSAGE]

The exact error returned by the schema validator or tool harness

Field 'query' is required but was missing. Field 'q' is not defined in the schema.

Inject verbatim. Do not paraphrase. Multi-error messages must preserve order and field paths.

[CONVERSATION_CONTEXT]

The last N turns of the conversation leading to the tool call attempt

User: Find Acme Corp filings. Assistant: I'll search for that.

Truncate to last 3 turns if token budget is tight. Must include the user intent that triggered the tool call.

[TOOL_DESCRIPTION]

The human-readable description of the tool's purpose and parameter semantics

Searches the corporate filings database. query: full-text search string. year: optional fiscal year filter.

Pull from the tool manifest. Include parameter descriptions and examples if available. Helps the model map intent to correct fields.

[RETRY_ATTEMPT_NUMBER]

Current retry count for budget tracking and loop detection

2

Integer starting at 1. Must be compared against [MAX_RETRIES] before the retry call. Passed for logging, not just the prompt.

[MAX_RETRIES]

The retry budget ceiling before escalation

3

Integer. Must match the harness configuration. If [RETRY_ATTEMPT_NUMBER] >= [MAX_RETRIES], skip this prompt and escalate.

[IDEMPOTENCY_KEY]

A stable key to prevent duplicate side effects on retry

req_8a7b3c_2025-01-15_search_acme

Must be preserved across retries for the same logical attempt. Generate once per original tool call, not per retry.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the Invalid Tool Call Schema Repair prompt into a production agent retry loop with validation, retry budgets, and escalation.

The schema repair prompt is not a standalone fix; it is one step inside a structured retry harness. When a tool call fails validation, the harness must catch the error, extract the exact validation failure message and the invalid payload, inject them into the repair prompt, and submit the corrected output for re-validation before execution. This loop must be bounded by a retry budget to prevent infinite cycles when the model cannot converge on a valid payload.

Implement the harness as a retry loop with three clear stages: catch, repair, and re-validate. In the catch stage, intercept the validation error from your schema validator (e.g., jsonschema, pydantic, or a custom function signature checker) and capture both the full error message and the offending JSON. In the repair stage, populate the prompt template's [INVALID_TOOL_CALL] with the raw failed payload, [EXPECTED_SCHEMA] with your tool's JSON Schema or function signature, and [VALIDATION_ERROR] with the exact error string. Submit the repair prompt and parse the [CORRECTED_TOOL_CALL] from the response. In the re-validate stage, run the corrected payload through the same validator. If it passes, proceed to tool execution. If it fails, increment the retry counter and loop, feeding the new error back into the repair prompt. After a configurable maximum retries (typically 3–5), exit the loop and escalate to a human review queue or a fallback model.

Log every attempt with the retry count, the validation error, and the corrected payload for observability. Track the schema_repair_success_rate and retry_budget_exhaustion_rate metrics to detect when the repair prompt itself needs tuning. Avoid silent retries that mask systemic schema mismatches; if the same validation error repeats across attempts, the repair prompt may need stronger constraints or a few-shot example of the correct output shape. For high-risk domains, require human approval before executing the first corrected tool call in a session, then allow autonomous retries for subsequent repairs once the pattern is trusted.

IMPLEMENTATION TABLE

Expected Output Contract

The structure the repair model must return. Validate this before accepting the repair and resubmitting the tool call.

Field or ElementType or FormatRequiredValidation Rule

tool_call

object

Top-level object must parse as valid JSON

tool_call.name

string

Must exactly match a function name in [AVAILABLE_TOOLS]

tool_call.arguments

object

Must parse as valid JSON object; must not be a string-encoded JSON blob

tool_call.arguments.[PARAMETER_NAME]

per schema

per schema

Must satisfy required/optional constraints from [TOOL_SCHEMA]; no extra hallucinated fields allowed

repair_notes

array of strings

If present, each element must be a non-empty string describing a specific correction made

repair_notes[*]

string

Must reference a concrete change: field renamed, type cast, missing field inferred, or invalid value mapped

clarification_needed

boolean

Must be true if any required parameter could not be safely inferred; false otherwise

clarification_question

string

Required when clarification_needed is true; must ask a specific, answerable question about the missing parameter

PRACTICAL GUARDRAILS

Common Failure Modes

Schema repair prompts fail in predictable ways. These are the most common production failure modes and the specific guardrails that prevent them.

01

Intent Drift During Repair

What to watch: The repair prompt produces a schema-valid call that passes validation but no longer fulfills the original user intent. The model over-corrects to satisfy the schema at the expense of semantic correctness. Guardrail: Include the original user request and the original (failed) tool call as locked context in the repair prompt. Add an eval check that compares the repaired arguments against the original intent before execution.

02

Hallucinated Field Injection

What to watch: The model adds plausible but fabricated fields that were not present in the original call and are not required by the schema. These pass validation silently and produce incorrect downstream results. Guardrail: Add an explicit constraint in the repair prompt: 'Do not add any field not present in the original arguments unless the schema marks it as required and it was missing.' Run a post-repair diff against the original argument keys and flag any net-new fields for review.

03

Error Message Misinterpretation

What to watch: The model misreads the validation error, fixes the wrong field, or applies a correction that doesn't address the root cause. This is common with nested validation errors where the error path is deep. Guardrail: Inject the exact validation error message with its JSONPath or field path into the repair prompt. Use structured error format: {path: '$.parameters.threshold', error: 'expected number, got string'}. Validate that the repaired call resolves the specific error before retrying.

04

Silent Parameter Dropping

What to watch: When the model cannot fix a malformed parameter, it removes it entirely rather than attempting correction. The resulting call is valid but incomplete, causing downstream tools to use defaults or fail silently. Guardrail: Compare argument cardinality before and after repair. Flag any missing keys that were present in the original call. Add a repair instruction: 'Preserve all original parameters. If a parameter is malformed, correct it; do not remove it unless the schema explicitly forbids it.'

05

Retry Loop Exhaustion Without Convergence

What to watch: The repair prompt produces a corrected call that still fails validation, triggering another repair attempt. After N retries, the system either loops indefinitely or gives up without useful output. Guardrail: Implement a retry budget with a hard cap (e.g., 3 attempts). Track whether successive repairs are making progress (error count decreasing). If errors are static or increasing, break the loop and escalate with the full failure history. Include a staleness detector: if the same error persists across 2 attempts, stop retrying.

06

Context Window Overflow from Error Accumulation

What to watch: Each retry appends the previous failure and error message to the prompt context. After multiple retries, the context window overflows, truncating the original intent or the schema definition. Guardrail: Use progressive error disclosure: on retry 1, include only the error type and path. On retry 2, include the full error message. On retry 3, include the full error plus the diff from the original call. Never append raw stack traces. If context exceeds 80% of the window, compress prior attempts into a summary before retrying.

IMPLEMENTATION TABLE

Evaluation Rubric

Test the repair prompt against these criteria before deploying to production. Each criterion validates a specific failure mode of schema repair prompts.

CriterionPass StandardFailure SignalTest Method

Schema Conformance

Repaired JSON passes strict validation against the target tool schema with zero errors

Validation errors remain after repair; required fields still missing; extra fields not removed

Run JSON Schema validator on output; compare error count before and after repair

Intent Preservation

Repaired call preserves the original semantic intent and all explicitly provided parameter values

Original parameter values altered without justification; tool purpose changed; user-specified data lost

Diff original vs repaired arguments; flag any value changes not attributable to type coercion or enum mapping

Hallucinated Field Removal

All fields not present in the target schema are removed from the repaired output

Fabricated fields persist in output; model invents plausible-sounding parameters not in schema

Extract all keys from repaired JSON; assert every key exists in the target schema definition

Error Message Utilization

Repair addresses every validation error present in the injected error message

Repair ignores specific error messages; same error class recurs on retry; generic fix applied instead of targeted correction

Parse error message for violation types; verify each violation category has a corresponding change in the repaired output

Type Coercion Safety

Type corrections preserve data fidelity without silent corruption

String-to-number coercion drops significant digits; boolean coercion inverts meaning; array wrapping loses element order

Assert type-corrected values round-trip to semantically equivalent representations; flag precision loss or semantic drift

Clarification Escalation

Prompt requests clarification when inference confidence is below threshold rather than guessing

Model fabricates plausible values for missing required parameters without flagging uncertainty; no clarification request generated

Inject inputs missing critical required parameters; assert output contains clarification request or explicit uncertainty marker

Idempotency Preservation

Repaired call preserves idempotency keys, request IDs, and correlation identifiers from the original call

Idempotency keys stripped or regenerated; correlation IDs lost; duplicate execution risk introduced

Extract idempotency fields from original and repaired payloads; assert identity preservation for all tracking identifiers

Nested Structure Integrity

Repair targets only the failing path; valid sibling fields and parent structures remain unchanged

Valid nested objects flattened or restructured; sibling fields removed during repair; deep copy introduces structural changes

Compare JSON tree at paths not referenced in error messages; assert structural identity for all non-failing branches

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Add structured error injection, multi-field repair prioritization, and a validation wrapper that re-checks the corrected call before submission. Include retry budget tracking and idempotency key preservation.

code
The previous tool call failed validation with these errors:
[ERROR_LIST]

Expected schema (only repair the failing fields):
[SCHEMA]

Original call intent:
[ORIGINAL_INTENT]

Failed call payload:
[FAILED_CALL]

Rules:
- Fix ONLY the fields listed in the errors.
- Preserve all valid fields unchanged.
- If a required field is missing, infer it from [ORIGINAL_INTENT] or set to null with a note.
- Remove any fields not present in the schema.
- Return the complete corrected JSON payload.

Watch for

  • Silent format drift across retries
  • Hallucinated field values when inference is unsafe
  • Missing human review escalation after retry budget exhaustion
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.