Claude API Custom Tool Schema Validation Guide
When you define a custom tool for Claude's tool use feature, the model doesn't validate your JSON Schema for you — it just reads it and tries to generate arguments that match. Validation failures usually show up later, either as malformed arguments from Claude, a 400 error from the API rejecting your tool definition, or a runtime crash in your own tool handler when it receives unexpected input. Fixing this means validating your schema in three places: before you send it, when Claude returns arguments, and inside your tool execution code.
This guide covers what Claude actually checks about tool schemas, the mistakes that cause silent failures, and a practical validation pattern you can drop into any integration.
How Claude validates tool schemas
Claude's API accepts tool definitions as JSON Schema objects (draft 2020-12 subset). The API does minimal structural validation at request time — it checks that the input_schema is valid JSON and has the expected top-level shape (type: "object", a properties map, optional required array). Beyond that, Claude relies on the model to interpret your schema correctly, which means:
- Type mismatches are not rejected by the API. If your schema says
"type": "integer"but Claude generates a string, you'll only find out when you try to parse the response. - Unsupported JSON Schema keywords are silently ignored. Things like
patternProperties,oneOfnested deeply, or customformatvalidators may not influence generation at all. - Required fields are a strong hint, not a hard guarantee. Claude usually respects
required, but under ambiguous prompts it can omit a field or hallucinate a plausible-looking value.
This is the core thing to internalize: tool schema is a prompt, not a contract. The model uses it to decide what to generate, but nothing enforces that the output conforms until your code checks it.
Common schema mistakes that break tool calls
A few patterns account for most tool-use bugs we see:
- Overly permissive types.
"type": "string"for a field that should be an enum invites free text. Use"enum": [...]whenever the set of valid values is finite. - Missing descriptions on parameters. Claude uses the
descriptionfield to decide what to put in each argument. A field with no description gets guessed, often wrong. - Deeply nested objects. Claude handles flat-ish schemas more reliably than schemas with 4+ levels of nesting. If a tool needs nested data, consider flattening with prefixed keys or splitting into multiple tool calls.
- Numbers as strings (or vice versa). Claude sometimes returns
"5"instead of5for numeric fields, especially when the field name is ambiguous. Always coerce and re-validate on your side. - Array items without a schema.
"type": "array"with no"items"key means Claude has no idea what should go in the array.
A practical validation pattern
Don't trust tool call arguments. Validate them against the same schema you sent to Claude, using a real JSON Schema validator (ajv for JavaScript, jsonschema or pydantic for Python). This catches drift between what you specified and what the model produced.
import Ajv from "ajv";
const ajv = new Ajv({ coerceTypes: true });
const schema = {
type: "object",
properties: {
city: { type: "string" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
days: { type: "integer", minimum: 1, maximum: 14 }
},
required: ["city", "unit"],
additionalProperties: false
};
const validate = ajv.compile(schema);
function handleToolCall(toolInput) {
const valid = validate(toolInput);
if (!valid) {
// Return the error to Claude as a tool_result with is_error, not a crash
return {
tool_use_id: toolInput.id,
content: `Invalid arguments: ${ajv.errorsText(validate.errors)}`,
is_error: true
};
}
return runTool(toolInput);
}
The key move here is is_error: true in the tool result. Instead of throwing and killing the request, you feed the validation error back to Claude as a tool result. In most cases, Claude will self-correct and retry the call with fixed arguments on the next turn — you get a second chance without writing retry logic yourself.
Reduce ambiguity at the schema level
Validation after the fact only catches problems — better schemas prevent them. A few concrete adjustments:
- Replace
"type": "string"withenumfor any field with a known value set (units, categories, statuses). - Add
minimum/maximumorminLength/maxLengthconstraints so Claude has explicit boundaries, even though they're not enforced server-side — they still improve generation quality. - Write descriptions as if explaining the field to a new engineer, not as a label. "The ISO 4217 currency code, e.g. USD" beats "currency."
- Set
additionalProperties: falseso downstream validators (like ajv above) reject unexpected extra keys Claude might add. - Keep tool names and parameter names unambiguous —
get_weather(location)beatsget_weather(input).
Where this fits in a production pipeline
If you're running tool use through SubToAPI, the request/response shape for tools is identical to calling Claude directly — you send your tools array with input_schema and get back tool_use blocks with Claude's generated arguments, same as in the official docs. SubToAPI doesn't alter or pre-validate your schemas; it exposes the same tool-calling contract over a stable API key (sub_live_...) so you can swap providers or rotate keys without touching your tool definitions. See the tool use docs and message format reference for the exact request shape, or the quickstart if you're setting this up for the first time.
Whether you're calling Claude directly or through a gateway, the validation responsibility is the same: your code, not the API, enforces correctness. Build that validation layer once, log the failures, and use them to tighten your schemas over time.
questions
Does the Claude API reject invalid tool schemas before running the request? Only structurally — it checks the JSON is well-formed and has the right top-level keys. It doesn't validate types, enums, or constraints against Claude's actual output, so semantic errors only appear when arguments come back.
What's the best way to handle a tool call with bad arguments? Validate with a JSON Schema library, then return a tool_result with is_error: true describing the problem instead of crashing. Claude typically retries with corrected arguments on the next turn.
Can I use full JSON Schema features like oneOf or patternProperties? You can include them, but Claude's generation quality degrades with complex nested logic. Stick to flat objects, enum, basic types, and clear descriptions for the most reliable results.