Immutable solution version
ToolChoiceViolationError and ToolCallRepairError: enforcing and fixing tool calls
Complete solution
The v7 errors around tool-call discipline: the model violated your toolChoice constraint, or your repairToolCall function failed to fix a bad call.
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 1 · 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>