Task 4.3
tool_use & Structured Output
Schema compliance vs semantic correctness
📘What You Need to Know
What tool_use Actually Guarantees
When Claude calls a tool via tool_use, the response conforms to your JSON schema — field names, types, required properties, and enum values are structurally valid. This is schema compliance. It is deterministic and reliable for format enforcement.
What tool_use does NOT guarantee is semantic correctness. The invoice total might be structurally valid as a number but mathematically wrong. The date might parse as a string but represent a date that does not exist on the document. The enum value might be valid but describe the wrong category. Always validate extracted values after tool_use returns.
tool_use = structural compliance. Business rule validation = semantic correctness. You need both.
tool_choice Options
tool_choice controls whether and how Claude uses tools. auto (default) lets Claude decide whether to call a tool. any forces tool use but lets Claude pick which tool. Forced specific — {"type": "tool", "name": "extract_invoice"} — guarantees Claude calls that exact tool, ensuring schema compliance for that extraction.
Use forced specific when you need guaranteed structured output. Use auto when the model should decide whether extraction is needed. Do not use any to prevent end_turn — that creates infinite loops in agentic workflows.
- •auto: Claude decides whether to use a tool (default)
- •any: must use a tool, can choose which one
- •forced specific: {"type": "tool", "name": "X"} — guarantees that tool is called
Schema Design Best Practices
Well-designed schemas improve extraction reliability before validation even runs. Every property needs a description that tells Claude what to extract and how. Required fields prevent partial outputs from passing silently. Nullable types handle legitimately missing data without schema violations.
- •Always include "required" for mandatory fields
- •Use "enum" for categorical fields with known values
- •Include "other" in enums plus a detail field for edge cases
- •Use ["string", "null"] for fields that might be missing on some documents
- •Add "description" to each property explaining what to extract
{
"name": "extract_invoice",
"input_schema": {
"type": "object",
"required": ["invoice_number", "line_items", "total"],
"properties": {
"invoice_number": { "type": "string", "description": "Invoice ID from header" },
"document_type": {
"type": "string",
"enum": ["invoice", "credit_note", "receipt", "other"],
"description": "Document classification"
},
"other_type_detail": {
"type": ["string", "null"],
"description": "Required when document_type is other"
},
"total": { "type": "number", "description": "Final total in dollars, not cents" }
}
}
}Enum Design with "other" for Edge Cases
Enums constrain output to known values — but real documents contain unexpected categories. Without an "other" option, Claude may hallucinate the closest enum value, silently misclassifying a proforma invoice as a standard invoice. With "other" plus a detail field, edge cases are captured explicitly and routed to human review.
The exam frequently tests whether you include "other" in enums. Answers that list only the happy-path categories without an escape hatch are wrong when documents can vary.
Claude may invent enum values not in your schema if you omit "other." Always provide an escape hatch with a detail field.
Post-Extraction Validation Pipeline
Production extraction pipelines follow a two-stage pattern: tool_use for structure, then business rules for semantics. Check that line items sum to the subtotal. Verify dates are plausible. Confirm required relationships hold (if document_type is "other", other_type_detail must be populated).
Schema compliance passing does not mean you can skip validation. The exam asks what to do after tool_use returns — the answer is always validate against business rules, never "trust the output because JSON parsed successfully."
⚠️Exam Traps
Trap: Assuming tool_use eliminates all extraction errors
Why it's wrong: tool_use guarantees structure, not semantic correctness — values inside can be wrong.
Trap: Skipping validation because the JSON parsed successfully
Why it's wrong: Valid JSON with wrong totals, dates, or categories is still wrong data in production.
Trap: Enums without an "other" category for fields with unexpected values
Why it's wrong: Claude may hallucinate the nearest enum value, silently misclassifying edge cases.
Trap: Using tool_choice "any" to force extraction on every message in an agent loop
Why it's wrong: Forcing tool use when the agent should finish creates infinite loops.
✓Concept Check
After Claude extracts invoice data via tool_use and the JSON parses successfully, what should you do next?
🔨Build Exercise
Intermediate45 minutesBuild a Schema-Compliant Extraction Pipeline
What you'll learn
- •Design tool schemas with required fields, enums, and descriptions
- •Implement post-extraction business rule validation
- •Handle edge cases with "other" enum values