← Blog

Claude API Custom Tool Schema Validation Guide

2026-11-14 · 5 min read · SubToAPI Team

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:

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:

  1. 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.
  2. Missing descriptions on parameters. Claude uses the description field to decide what to put in each argument. A field with no description gets guessed, often wrong.
  3. 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.
  4. Numbers as strings (or vice versa). Claude sometimes returns "5" instead of 5 for numeric fields, especially when the field name is ambiguous. Always coerce and re-validate on your side.
  5. 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:

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.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →