Models sometimes send stringified numbers or booleans like "42" or "true" when your schema expects primitive numbers or booleans. kenpachi automatically pre-coerces these primitive argument types before validating arguments with Zod.
Argument pre-coercion is enabled by default for every tool defined with defineTool.
What happens step by step
- Serialize — Tool Zod schemas are dynamically converted to standard JSON Schema (
type: "object", properties, required) using serializeZodSchema so the model receives clean parameter declarations.
- Pre-Coerce — Incoming arguments are pre-coerced (e.g.
"6" → 6, "true" → true) before schema parsing runs.
- Validate — Coerced arguments are checked against your Zod schema using
schema.safeParse().
- Error handling — If arguments remain invalid after coercion, formatted validation errors are captured and returned cleanly without crashing your execution loop.
Example
Set repairable: false when invalid argument attempts should fail immediately without retry attempts (e.g. handoffs, security-sensitive tools):
Configure max retry attempts per run with maxToolRepairAttempts on agent.run().