← Blog

Claude API JSON Schema Validation for Tool Use

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

When you define a tool for the Claude API, you give it a JSON Schema describing the expected input shape. Claude uses that schema to constrain how it formats the arguments it sends back in a tool_use block — but "constrain" doesn't mean "guarantee." Understanding exactly what Claude validates, what it doesn't, and how to add your own validation layer is the difference between a tool-calling feature that works in a demo and one that survives production traffic.

The short answer: Claude uses your input_schema to bias generation toward valid JSON that matches your types and required fields, but it is not a hard constraint solver. You still need to run the output through a real JSON Schema validator (Ajv, jsonschema, Zod, Pydantic) before you trust it, and you need a clear strategy for what happens when validation fails. The rest of this article covers how that works and how to build it correctly.

How Claude uses your input_schema

When you pass a tool definition like this:

{
  "name": "create_invoice",
  "description": "Create an invoice for a customer",
  "input_schema": {
    "type": "object",
    "properties": {
      "customer_id": { "type": "string" },
      "amount_cents": { "type": "integer", "minimum": 1 },
      "currency": { "type": "string", "enum": ["usd", "eur", "gbp"] },
      "due_date": { "type": "string", "format": "date" }
    },
    "required": ["customer_id", "amount_cents", "currency"]
  }
}

Claude reads the schema as part of the system prompt context and generates arguments it believes satisfy it. In practice, for well-structured schemas with clear types, enums, and required fields, Claude is reliable at producing syntactically valid JSON that respects types and enum values. Where it can still drift:

None of this is a bug — it's a reminder that the schema is guidance for a generative process, not a parser-enforced contract.

Build a real validation layer

Treat the schema you send to Claude and the schema you validate against as the same source of truth, and run every tool_use.input through it before executing anything.

import Ajv from "ajv";
import addFormats from "ajv-formats";

const ajv = new Ajv({ allErrors: true, strict: true });
addFormats(ajv);

const schema = {
  type: "object",
  properties: {
    customer_id: { type: "string" },
    amount_cents: { type: "integer", minimum: 1 },
    currency: { type: "string", enum: ["usd", "eur", "gbp"] },
    due_date: { type: "string", format: "date" }
  },
  required: ["customer_id", "amount_cents", "currency"],
  additionalProperties: false
};

const validate = ajv.compile(schema);

function handleToolUse(toolUseBlock) {
  const valid = validate(toolUseBlock.input);
  if (!valid) {
    return {
      type: "tool_result",
      tool_use_id: toolUseBlock.id,
      content: JSON.stringify(validate.errors),
      is_error: true
    };
  }
  return executeInvoiceCreation(toolUseBlock.input);
}

The key pattern here: when validation fails, don't throw and give up — send a tool_result with is_error: true back to Claude, including the Ajv error list. Claude reads that error and retries with corrected arguments in the next turn. This self-correction loop is far more reliable than trying to get perfect output on the first attempt.

Tightening the schema itself

A lot of validation failures are fixable before you ever call the model, by making the schema stricter:

Validation errors as part of the conversation loop

The most robust tool-use implementations treat schema validation as a conversational feedback loop, not a one-shot gate:

  1. Send the user message and tool definitions.
  2. Receive tool_use block, validate against your schema.
  3. If invalid, append a tool_result with is_error: true and a readable error message.
  4. Send the updated message array back to Claude.
  5. Claude retries with corrected arguments.
  6. Cap retries (2–3) and fall back to a clarifying question to the user if it still fails.

This is the same approach regardless of whether you call the Claude API directly or through a proxy. If you're running Claude behind SubToAPI to get a stable HTTPS endpoint with application-scoped API keys for your team, the validation loop is identical — you're still sending tool_use blocks and tool_result responses over the Messages-compatible API, just with your own sub_live_... key instead of juggling raw Anthropic credentials per environment. See the tool use docs for the exact request/response shapes, or the quickstart if you're setting this up for the first time.

Logging and debugging invalid outputs

Keep a log of every tool call that failed validation, with the raw input and the Ajv error array. Over a few hundred calls you'll usually find patterns — a specific enum Claude confuses with a synonym, a date format it prefers (MM/DD/YYYY instead of ISO), or a nested field it consistently omits. Fix those at the schema level (tighter description, explicit format, or splitting one ambiguous field into two simpler ones) rather than only handling them at the retry level. Schema fixes compound — a single improved description can cut your error rate on that field to near zero.

questions

Does Claude guarantee valid JSON matching my schema? No. Claude is strongly biased toward schema-conformant output but can occasionally produce missing fields, wrong types, or stray properties. Always validate tool_use.input with a real JSON Schema validator before executing it.

What's the best way to handle a schema validation failure? Return a tool_result with is_error: true and the validator's error messages in the same conversation turn. Claude will usually self-correct on the next attempt rather than requiring a brand-new request.

Should I use additionalProperties: false on every tool schema? Yes, for most tools. It catches stray fields Claude sometimes adds and makes validation errors explicit and actionable instead of silently passing extra data into your execution logic.

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 →