Immutable solution version
ToolChoiceViolationError and ToolCallRepairError: enforcing and fixing tool calls
Complete solution
Shows how to fix toolChoiceViolationError and ToolCallRepairError: enforcing and fixing tool calls. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
ToolCallRepairError: your repair function threw while trying to fix a malformed tool call. import { generateText, tool } from 'ai'; import { z } from 'zod'; const result = await generateText({ model: 'your-model-id', tools: { getWeather: tool({ inputSchema: z.object({ city: z.string() }), execute: async ({ city }) => ({ city }) }) }, toolChoice: { type: 'tool', toolName: 'getWeather' }, prompt: 'Hi there', }); What to know: 1. toolChoice: 'required' or a specific tool forces the model's hand.
When to use
You are seeing this: Two errors for the strict end of tool calling. Use this skill when you run into "ToolChoiceViolationError and ToolCallRepairError: enforcing and fixing tool calls".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Two errors for the strict end of tool calling. ToolChoiceViolationError: the response violated the toolChoice you enforced (e.g. you required a specific tool and the model called none). ToolCallRepairError: your repair function threw while trying to fix a malformed tool call.
import { generateText, tool } from 'ai'; import { z } from 'zod';
const result = await generateText({ model: 'your-model-id', tools: { getWeather: tool({ inputSchema: z.object({ city: z.string() }), execute: async ({ city }) => ({ city }) }) }, toolChoice: { type: 'tool', toolName: 'getWeather' }, prompt: 'Hi there', });
What to know:
- toolChoice: 'required' or a specific tool forces the model's hand. When the model cannot comply (the prompt does not need tools), you get ToolChoiceViolationError instead of a graceful text answer. Only enforce when the task genuinely needs the tool.
- repairToolCall receives the malformed call and returns a fixed one. If your repair function throws or returns garbage, ToolCallRepairError replaces the original error. Keep repair functions total: handle every shape, never throw.
- Log the raw tool call inside repairToolCall during development. The most common malformations are wrong arg types (string where number expected) and extra unknown fields.
- InvalidToolInputError is what repair is trying to prevent. The pipeline is: model call -> validation fails -> repairToolCall -> revalidate -> InvalidToolInputError or ToolCallRepairError if still broken.
- For user-facing tools, prefer a schema the model can actually satisfy over aggressive repair. Simpler schemas beat clever repairs.
- Test toolChoice enforcement with prompts that do and do not need tools. Enforcement that only works on the happy path will surprise you in production.
Evidence
Verified against the live AI SDK docs 2026-09-26. The errors reference lists ToolChoiceViolationError and ToolCallRepairError under tools and approvals. The tools-and-tool-calling guide documents the tool() helper with inputSchema and the tools map on generateText/streamText.
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_w3rl79MKfH2Or4k0jfqKwQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ToolChoiceViolationError and ToolCallRepairError: enforcing and fixing tool calls <span>— Vectle</span></a>