All blocks
BlocksTools & Utilities

Tool Call Repair

Use this when your model often sends tool arguments that fail schema validation and you want them fixed before execution without extra chat turns.

Loading pattern preview

Problems solved

Technical challenges this implementation handles out of the box.
  • Repair malformed tool calls before retrying execution
  • Fix schema validation errors with generateObject
  • Avoid polluting message history with extra repair turns
  • Stream repair metadata to the client as data parts

Use cases

Products and workflows this pattern is designed to support.
  • Agents using smaller models that misformat tool args
  • Complex Zod schemas that fail first-pass validation
  • Automatic tool-call error recovery
  • Improving tool success rates in production agents

Compare with

Similar patterns, and when each one is the better fit.

Setup

Requirements, wiring steps, and what this pattern adds to your project.

Getting started

Pick how you want to pull this pattern in. Then wire env vars and routes the same way.

  1. 1

    Download → Next.js app

    Use Download → Next.js app in the preview toolbar. You get a zip scaffold (Pro downloads rewrite preview `/view/.../api/*` paths to `/api/*` automatically).

  2. 2

    Extract the zip and install dependencies

    Unzip the archive, then run `pnpm install` (or npm / yarn) in the project folder.

  3. 3

    Fill .env.local

    The download includes .env.local placeholders when needed. Add real values for: OPENAI_API_KEY.

  4. 4

    Customize the agent and tool files

    Adapt prompts, tools, and stop conditions in the generated project for your product.

  5. 5

    Run the app and open the pattern route

    Run `pnpm dev`, open the setup/checklist page if present, then navigate to the generated pattern route and verify responses.

Environment variables

Capabilities

AI SDK APIs

streamTextconvertToModelMessagestooluseChatexperimental_repairToolCallgenerateObjectNoSuchToolErrorcreateUIMessageStreamResponse

Providers

OpenAI

Files added

7 files
  • app/page.tsx
  • app/layout.tsx
  • app/api/tool-repair/route.ts
  • components/tool-repair-demo.tsx
  • lib/types.ts
  • tools/weather.ts
  • tools/weather-ui.tsx

Dependencies

15 total

npm packages

7
AISDKshadcn/uilucide-reactNext.jsReactstreamdownZod

Shadcn IconRegistry components

8
alertemptyinput-groupseparatormessage-scrollermessagebubbletooltip

Critical files

2 files
  • tool-repair/route.ts

    API route implementing experimental_repairToolCall. When a tool call fails validation, uses generateObject with the tool's inputSchema to repair malformed input. Demonstrates streaming repair metadata via data parts.

  • tools/weather.ts

    Weather tool with strict Zod schema designed to trigger repair scenarios. Includes repair state detection via experimental_context.

Requirements

Complexity
Intermediate
Setup time
~5 minutes
Node
24+
Memory
512MB