Skip to content

Design the extraction tool's schema

Complete an extraction tool's JSON schema (nullable fields, an enum with "other" plus a detail field) and the tool_choice setting that guarantees structured output for a stated requirement.

Scenario

Scenario: Structured Data Extraction. An appliance maker extracts warranty claims from customer emails and scanned forms: serial number, purchase date, fault type. The first version asked Claude to "reply with JSON" and parsed the text. A few replies a day broke the parser.

Then a quieter problem showed up. Claims with no purchase date came back with one anyway, usually the date of the email. Those claims looked like they were inside the warranty period, and were paid.

Explanation

Extract through a tool. Define a tool whose input_schema is the record you want, such as extract_claim. Claude "calls" it, and the tool_use block's input is your record. Nothing has to run: the schema is the contract. The docs give a rule of thumb: if you are writing a regex to pull a value out of model text, that value belongs in a schema.

Strict mode removes syntax errors. With strict: true on the tool, Claude's input is constrained to your schema: valid JSON, the right types, every required field. It does not make the content right. A tax amount in the shipping field, or line items that don't add up to the total, still pass. That needs validation in your code.

Pick the tool_choice for the step:

Setting Claude… Use it when
{"type": "auto"} may call a tool or answer in text the reply might rightly be text
{"type": "any"} must call one of the tools several schemas, document type unknown
{"type": "tool", "name": "…"} must call that tool one extraction must run, e.g. before enrichment

With any or a named tool, Claude writes no text before the tool call. Not every model supports forced tool use: Claude Opus 5.5, Sonnet 5.5, Fable 5.1 and Mythos 5.1 return a 400 error for any and tool, and so does any model with manual extended thinking (Opus 5 and Haiku 5.5, for example, still accept it). There the docs recommend auto with strict tool use, or structured outputs (output_config.format, JSON in the text block). Check the Define tools page for your model. The exam guide describes any and forced selection without this caveat, so answer its items as the table above says.

Design the schema for real documents:

  • Nullable when the source may not say it. A required string forces a value, so Claude supplies a plausible one. Use "type": ["string", "null"] and say in the description when to return null.
  • Enums with an escape. Add "other" plus a free-text detail field for categories you haven't listed, and "unclear" when the document is ambiguous. Otherwise Claude picks the closest listed value.
  • Normalization rules in the prompt or the field descriptions: dates as YYYY-MM-DD, amounts as numbers, currency as a three-letter code. The schema fixes the shape; the rules fix the format inside it.

Worked example

Before: every field is a required string, and the fault type is a closed list.

{
  "serial_number": {"type": "string"},
  "purchase_date": {"type": "string"},
  "fault_type": {
    "type": "string",
    "enum": ["electrical", "mechanical", "leak"]
  }
}

A claim without a purchase date gets one invented, and a cracked door glass becomes "mechanical".

After: the same properties, rewritten.

{
  "serial_number": {"type": ["string", "null"]},
  "purchase_date": {
    "type": ["string", "null"],
    "description": "YYYY-MM-DD; null if not stated"
  },
  "fault_type": {
    "type": "string",
    "enum": ["electrical", "mechanical", "leak",
      "other", "unclear"]
  },
  "fault_detail": {"type": ["string", "null"]}
}

On a model that supports forced tool use, the request forces this tool, so every response is a record, and the code reads the record from the tool_use block:

response = client.messages.create(
    model=MODEL,
    max_tokens=1024,
    tools=[extract_claim],
    tool_choice={"type": "tool", "name": "extract_claim"},
    messages=[{"role": "user", "content": claim_text}],
)
call = next(b for b in response.content
            if b.type == "tool_use")
record = call.input

A missing purchase date now arrives as null, and the pipeline routes that claim to a person instead of paying it.