Inferensys

Prompt

Tool Call Repair with Schema-Aware Suggestions Prompt Template

A practical prompt playbook for generating multiple schema-aware repair candidates with confidence scores when tool calls fail validation. Designed for agent platform engineers who need the agent loop to select or validate the best repair without aborting execution.
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 ideal use case, required context, and boundaries for the schema-aware tool call repair prompt.

This prompt is for agent platform engineers and tool-integration developers who need to recover from invalid tool calls by generating multiple ranked repair candidates. Use it when a tool call fails validation and you want the agent loop to have options to choose from, rather than a single best-guess repair. The prompt takes the original invalid tool call, the tool's JSON schema, the validation error, and optional conversation context, then returns a ranked list of candidate repairs with confidence scores and explanations. This is not a single-repair prompt. It is designed for developer tooling that provides inline repair suggestions where the agent or a downstream validator selects the best candidate.

The ideal user is building an agent harness or orchestration layer where tool call failures must not abort the entire agent loop. You need the original invalid tool call payload, the exact JSON schema for the tool (including required fields, types, and enum values), and the specific validation error message. Optionally, you can include the preceding conversation context to help the model infer missing arguments or disambiguate intent. The output is a ranked list of candidate repairs, each with a confidence score and a brief explanation of what was changed and why. This structure allows your agent loop to try the top candidate, fall back to alternatives, or escalate to a human if no candidate meets a confidence threshold.

Do not use this prompt when you need a single deterministic repair, when the tool schema is unavailable, or when the validation error is ambiguous and cannot be traced to specific fields. If the failure is a simple syntax error in the JSON, a deterministic parser-based repair is more reliable and cheaper. If the tool schema is missing, the model cannot ground its repairs and will hallucinate field names and types. If the validation error is vague (e.g., 'invalid request'), the prompt cannot produce targeted repairs and will generate low-quality guesses. In high-risk domains such as finance or healthcare, always route low-confidence candidates to human review before execution.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Schema-Aware Suggestions prompt template delivers value and where it introduces risk. Use these cards to decide if this repair strategy fits your agent loop.

01

Good Fit: High-Volume Agent Loops

Use when: your agent makes frequent tool calls and you need automated recovery without aborting the entire loop. Schema-aware suggestions let the system propose multiple ranked repairs, keeping throughput high. Guardrail: always cap the number of repair attempts per turn to prevent infinite retry loops.

02

Bad Fit: Single-Shot, Low-Latency Requests

Avoid when: latency budgets are under 200ms or the user is waiting synchronously. Generating multiple candidate repairs with confidence scores adds inference overhead. Guardrail: use a simpler single-shot repair prompt or escalate directly to the user if speed is critical.

03

Required Input: Original Tool Schema

Risk: without the exact tool schema, parameter types, and enum values, the repair prompt cannot generate valid suggestions. Guardrail: always pass the complete function definition, including required fields, types, and allowed values, as part of the repair context.

04

Required Input: Validation Error Details

Risk: generic error messages like 'invalid JSON' produce vague repairs. Guardrail: include the specific validation error, the failing field path, and the expected versus received value so the prompt can target the exact problem.

05

Operational Risk: Suggestion Overload

Risk: generating too many candidate repairs can overwhelm the selection logic and increase costs. Guardrail: limit the prompt to 3-5 ranked suggestions and include a confidence threshold below which suggestions are discarded before selection.

06

Operational Risk: Hallucinated Repairs

Risk: the repair prompt may invent plausible but incorrect argument values when context is insufficient. Guardrail: require each suggestion to ground inferred values in conversation history or tool description, and flag suggestions with low confidence for human review.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready prompt for generating multiple schema-aware repair candidates with confidence scores for invalid tool calls.

This prompt template is designed to be placed directly into your agent's repair harness. When a tool call fails validation, this prompt instructs the model to act as a schema-aware repair engine. Instead of guessing a single fix, it generates multiple candidate repairs, each with a confidence score and a rationale. This allows your agent loop to select the best candidate, attempt validation again, or escalate to a human if no high-confidence repair is available. The key advantage is that it provides a ranked set of options, turning a binary pass/fail event into a decision point with measurable quality signals.

text
You are a schema-aware tool call repair engine. Your task is to analyze a failed tool call and generate multiple candidate repairs.

**Original Failed Tool Call:**
```json
[FAILED_TOOL_CALL_JSON]

Target Tool Schema:

json
[TOOL_SCHEMA_JSON]

Validation Error Message: [VALIDATION_ERROR_MESSAGE]

Conversation Context (for inferring missing values): [CONVERSATION_CONTEXT]

Repair Constraints:

  • Generate exactly [NUM_CANDIDATES] distinct repair candidates.
  • Each candidate must be a complete, valid JSON object conforming to the Target Tool Schema.
  • Do not invent values that cannot be reasonably inferred from the Conversation Context or the Original Failed Tool Call.
  • If a required field cannot be inferred, use null and set the confidence for that field to 0.0.
  • Prioritize repairs that make the minimal necessary changes to the original arguments.

Output Format: Return a JSON object with a single key "candidates" containing an array of repair objects. Each repair object must have the keys "repair", "confidence", and "rationale".

  • "repair": The fully repaired tool call arguments as a JSON object.
  • "confidence": A score from 0.0 to 1.0 indicating your confidence that this repair is correct and will pass validation.
  • "rationale": A brief, specific explanation of what was changed and why.

Output Example:

json
{
  "candidates": [
    {
      "repair": {
        "location": "London",
        "units": "celsius"
      },
      "confidence": 0.95,
      "rationale": "Corrected 'unit' to 'units' based on schema and set location to 'London' from context."
    },
    {
      "repair": {
        "location": "London, UK",
        "units": "celsius"
      },
      "confidence": 0.7,
      "rationale": "Inferred a more specific location string from context, though the schema may expect a simpler form."
    }
  ]
}

To adapt this template, replace the square-bracket placeholders with live data from your application. [FAILED_TOOL_CALL_JSON] should be the exact string that failed parsing or schema validation. [TOOL_SCHEMA_JSON] is the expected parameter schema for the target function. [VALIDATION_ERROR_MESSAGE] provides critical signal—pass the raw error from your validator (e.g., pydantic, jsonschema) to guide the repair. [CONVERSATION_CONTEXT] should include the last few messages from the agent's history to help infer missing arguments. Set [NUM_CANDIDATES] to a small odd number like 3 or 5 to enable a simple majority-vote or selection mechanism in your harness. The output format is strict JSON, making it straightforward to parse and feed into a validation retry loop. For high-risk domains, always log the original failure, all candidates, and the final selected repair for auditability.

IMPLEMENTATION TABLE

Prompt Variables

Every placeholder required by the Tool Call Repair with Schema-Aware Suggestions prompt. Validate these inputs before sending to prevent cascading repair failures.

PlaceholderPurposeExampleValidation Notes

[FAILED_TOOL_CALL_JSON]

The original malformed tool call JSON that failed validation

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

Must be valid JSON string. If the model output is not parseable as JSON, use a pre-processing step to extract the JSON fragment before populating this variable.

[TOOL_SCHEMA]

The complete JSON Schema or OpenAPI definition for the expected tool call

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

Must be a valid JSON Schema object. Validate with a schema validator before injection. Missing required fields or incorrect types in the schema will cause the repair prompt to suggest invalid fixes.

[VALIDATION_ERROR_MESSAGE]

The exact error message from the schema validator or function runner

TypeError: q must be string, got number

Include the raw error string. Do not paraphrase. The model uses error message patterns to target repairs. Truncate to 500 characters if longer.

[CONVERSATION_CONTEXT]

The preceding conversation or agent reasoning that led to the tool call

User asked for weather in Paris. Agent decided to call search tool.

Optional but strongly recommended. Max 2000 characters. Provides semantic grounding for inferring correct argument values when the original call is ambiguous or missing data.

[AVAILABLE_TOOLS_LIST]

List of all available tool names and brief descriptions for disambiguation

["search": "Search the web", "calculator": "Evaluate math"]

Required when tool name disambiguation is possible. If null, the repair prompt assumes the tool name in the failed call is correct. Validate that the list matches the current tool registry.

[REPAIR_CANDIDATE_COUNT]

Number of alternative repair suggestions to generate

3

Integer between 1 and 5. Higher values increase diversity but cost more tokens. Default to 3. Validate range before sending.

[RETRY_BUDGET_REMAINING]

Number of remaining repair attempts in the current agent loop

2

Integer >= 0. When 0, the prompt should recommend escalation instead of repair. Validate that this matches the agent loop's actual retry counter.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the schema-aware repair prompt into an agent loop with validation, retries, and selection logic.

The Tool Call Repair with Schema-Aware Suggestions prompt is designed to be called as a recovery step inside an agent execution loop, not as a standalone utility. When a tool call fails validation, the agent should invoke this prompt to generate multiple candidate repairs, each with a confidence score. The agent loop then selects the best candidate, re-validates it against the tool schema, and either executes the repaired call or escalates. This harness must track the retry budget, log repair attempts for observability, and prevent infinite repair loops when the model cannot produce a valid suggestion.

To wire this into an application, wrap the prompt in a repair function that accepts the original tool call, the validation error, the tool schema, and the conversation context. The function should call the LLM with the prompt template, parse the response into a list of candidate repairs with confidence scores, and sort them by score descending. Each candidate must be validated against the tool schema before execution. If the top candidate passes validation, execute it. If it fails, try the next candidate. If no candidate passes, decrement the retry budget and call the repair prompt again with the new failure context. After exhausting the retry budget, escalate to a human or log the failure for offline analysis. Log every repair attempt with the original error, the candidates generated, the selected repair, and the validation result to build a dataset for future fine-tuning or prompt improvement.

Model choice matters here: use a model with strong JSON generation and instruction-following capabilities. The prompt expects a structured output containing an array of repair objects, so enable structured output mode or use a JSON schema constraint if the model API supports it. For high-throughput systems, consider caching the tool schema to avoid passing it repeatedly. The confidence scores from the model are not calibrated probabilities—treat them as relative rankings. Implement a minimum confidence threshold below which repairs are discarded without attempting validation. Avoid using this repair prompt for security-critical tool calls where a hallucinated repair could cause harm; those should fail closed and require human review.

IMPLEMENTATION TABLE

Expected Output Contract

The exact JSON structure the repair prompt must return. Validate this contract in your harness before passing suggestions to the agent loop.

Field or ElementType or FormatRequiredValidation Rule

repairs

Array of objects

Must be a non-empty array. Reject if empty or not present.

repairs[].suggestion_id

String (UUID v4)

Must match UUID v4 regex. Reject on collision within the array.

repairs[].tool_call_index

Integer >= 0

Must reference the index of the failing tool call in the input array. Reject if out of bounds.

repairs[].corrected_arguments

Object

Must be valid JSON. Must pass validation against the provided tool schema. Reject on schema mismatch.

repairs[].confidence

Number (0.0 to 1.0)

Must be a float between 0.0 and 1.0 inclusive. Reject if out of range or non-numeric.

repairs[].repair_strategy

String (enum)

Must be one of: 'type_coercion', 'field_rename', 'value_normalization', 'structure_fix', 'inferred_default', 'hallucination_removal'. Reject on unknown value.

repairs[].rationale

String

Must be a non-empty string explaining the change. Reject if null, empty, or whitespace-only.

repairs[].diff

String (JSON Patch RFC 6902)

If present, must be a valid JSON Patch array. Reject on invalid patch syntax. Null allowed.

PRACTICAL GUARDRAILS

Common Failure Modes

What breaks first when using schema-aware repair suggestions in production agent loops, and how to guard against each failure mode.

01

Repair Introduces Hallucinated Values

What to watch: The repair prompt invents plausible but incorrect argument values, especially for missing required fields. The model fills gaps with contextually reasonable but factually wrong data. Guardrail: Require the repair prompt to mark inferred values with an inferred: true flag and a confidence score. Route any repair with inferred required fields above a low-confidence threshold to a human approval queue before execution.

02

Repair Suggestion Ranking Is Misleading

What to watch: The model generates multiple candidate repairs with confidence scores, but the highest-ranked suggestion is incorrect while a lower-ranked one is correct. The agent loop blindly executes the top suggestion. Guardrail: Implement a two-stage selection: first, filter candidates by a minimum confidence threshold. Second, when multiple candidates pass, execute the top N in a sandbox or dry-run mode and compare side effects before committing.

03

Schema Drift Causes Valid-but-Wrong Repairs

What to watch: The tool schema used in the repair prompt is stale—parameters were added, removed, or changed in the live API. The repair produces schema-compliant output that the API rejects or silently misinterprets. Guardrail: Version-stamp every tool schema and include the schema version in the repair prompt context. Validate repaired calls against the live API's OpenAPI spec before execution, not just the prompt's schema copy.

04

Repair Loop Exhausts Retry Budget on Unfixable Errors

What to watch: The repair prompt keeps generating variations that all fail validation, consuming the retry budget with no progress. The agent loop never escalates and eventually times out or returns a garbage fallback. Guardrail: Track repair attempt count and error type per tool call. If the same validation error persists across two repair attempts, stop retrying and escalate with the original error, repair history, and schema to a human or a higher-capability model.

05

Cross-Field Constraint Violations Survive Repair

What to watch: Individual fields are repaired to valid types and enums, but combinations violate business rules—mutual exclusion, co-requirement, or conditional validity. The tool executes with logically inconsistent arguments. Guardrail: Include cross-field validation rules as explicit constraints in the repair prompt, not just per-field schemas. Run a post-repair constraint checker before returning the candidate, and reject repairs that fail cross-field rules.

06

Repair Strips Necessary Context from Original Call

What to watch: The repair prompt over-corrects by removing or rewriting arguments that were valid and intentional, losing user intent or conversation context in the process. Guardrail: Require the repair prompt to produce a diff between the original and repaired call. Only accept repairs where removed fields are explicitly justified as hallucinated or invalid. Flag repairs with more than N field changes for human review.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for testing the quality of schema-aware tool call repair suggestions before integrating this prompt into an agent loop. Use these tests to evaluate suggestion diversity, ranking quality, and coverage of valid repairs.

CriterionPass StandardFailure SignalTest Method

Suggestion Diversity

At least 3 structurally distinct repair candidates are generated for a single malformed input

All candidates are identical or differ only in whitespace

Run 10 malformed tool calls through the prompt; count unique JSON structures per output

Valid Repair Coverage

At least one candidate passes full schema validation for 95% of test cases

No candidate passes validation for more than 10% of test cases

Validate each candidate against the tool's JSON Schema; track pass rate per test case

Confidence Score Calibration

The highest-confidence candidate is the correct repair in 80% of cases where a valid repair exists

High-confidence candidates are consistently wrong or low-confidence candidates are consistently correct

Compare confidence-ranked top candidate against ground-truth repair across 50 labeled cases

Schema Constraint Adherence

All suggested repairs respect required fields, enum values, and type constraints from the provided schema

Candidates introduce new required fields not in the schema or use invalid enum values

Schema-validate every candidate; flag any field that violates the provided tool definition

Hallucinated Field Injection

Zero candidates contain fields not present in the original tool schema or the malformed input

Candidates invent plausible but unsupported parameter names or values

Diff each candidate against the schema's property list; flag any extra keys not in the schema

Ranking Quality

The top-ranked candidate is the correct repair more often than any lower-ranked candidate

A lower-ranked candidate is correct more often than the top-ranked candidate across the test set

Compute top-1 accuracy vs. random candidate accuracy across 50 labeled repair cases

Context Preservation

Repaired arguments preserve the semantic intent of the original malformed call when intent is recoverable

Repairs change the meaning of the call (e.g., different tool, different action, different target entity)

Human review of 20 repair pairs: does the top candidate preserve the original call's intent?

Edge Case Handling

Prompt handles empty arguments, null values, and deeply nested malformation without crashing or returning empty suggestions

Prompt returns zero candidates, malformed JSON, or a refusal for edge-case inputs

Test with empty args object, null required field, 5-level nested malformation; check for valid response structure

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single candidate suggestion and relaxed confidence scoring. Replace the multi-candidate ranking with a simpler 'best guess + confidence' format. Drop the diversity and coverage eval checks in favor of a single schema compliance pass.

code
Generate ONE repair suggestion for the invalid tool call below. Return the corrected JSON and a confidence score (0.0-1.0).

Tool Schema: [TOOL_SCHEMA]
Invalid Call: [INVALID_TOOL_CALL]
Validation Error: [ERROR_MESSAGE]

Watch for

  • Overconfident scores on ambiguous repairs
  • Missing edge-case coverage without diversity checks
  • Single suggestion may be wrong with no fallback
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.