Claude Structured Output with tool_use and JSON Schema
👋 In plain words: When another program has to use Claude's answer, you need clean JSON in the exact same shape every time, not a friendly paragraph. This lesson shows how to get reliable JSON and how to design fields so Claude never invents data that isn't in the document.
Structured extraction is a whole exam scenario. You need to know how to guarantee valid JSON, how to design schemas that don’t push Claude to invent data, and when a retry will — and won’t — fix a problem (Task Statements 4.3 and 4.4).
🔑 Key terms
- Structured output: Claude's answer in a fixed, machine-readable shape such as JSON.
- JSON schema: A template listing each field, its type, and which fields are required.
- Nullable field: A field allowed to be
null(empty) when the information isn't in the source. - Enum: A field limited to a fixed list of values, such as hardware, software or other.
- Validation-retry loop: Checking the output and, if a rule fails, asking Claude again with the exact error.
📖 The story in this lesson
ShopNest's finance team receives supplier invoices as PDFs. Claude turns each one into JSON for the accounting system. Some invoices have no purchase-order number, and an early version invented one.
💡 Think of it like this
A schema is a paper form with labelled boxes. Claude fills the boxes instead of writing a letter. A nullable box has a 'not on the invoice' tick option. Without that option, a helpful assistant feels pressure to write something in every box.
🛠️ tool_use as the structured-output mechanism
Define an “extraction tool” whose input_schema is your output schema. Claude’s tool call arguments are your structured data — syntactically valid and schema-compliant.
{
"name": "record_invoice",
"description": "Record fields extracted from ONE supplier invoice.",
"input_schema": {
"type": "object",
"properties": {
"vendor": {"type": "string"},
"invoice_date": {"type": ["string", "null"], "description": "ISO-8601; null if absent"},
"po_number": {"type": ["string", "null"]},
"category": {"type": "string", "enum": ["hardware", "software", "services", "other", "unclear"]},
"category_detail": {"type": ["string", "null"]},
"stated_total": {"type": "number"},
"calculated_total": {"type": "number"},
"conflict_detected": {"type": "boolean"}
},
"required": ["vendor", "category", "stated_total", "calculated_total", "conflict_detected"]
}
}
🌍 Real-world example: Invoices into clean JSON
ShopNest defines a record_invoice tool whose input schema is the invoice format. Claude 'calls' the tool, and the tool's arguments are the extracted data, always valid JSON in the right shape. But the totals still need checking: valid JSON can still contain wrong numbers.
📏 Schema design rules
- Nullable for possibly-absent data. A required field the document doesn’t contain pushes the model to fabricate a value.
- Enum + “other” + detail string for extensible categories, and “unclear” for genuinely ambiguous cases.
- Self-check fields such as
calculated_totalnext tostated_total, plusconflict_detected. - Format rules in the prompt (dates to ISO-8601, amounts as numbers) alongside the schema.
🌍 Real-world example: The invented PO number
po_number was required, so Claude made one up for invoices that had none. Making it nullable lets Claude return null honestly. A category enum with 'other' plus a detail field handles unusual purchases without forcing a wrong category.
"po_number": {"type": ["string", "null"]},
"category": {"type": "string", "enum": ["hardware", "software", "services", "other", "unclear"]}🔁 Validation and retry loops
When validation fails, send a follow-up with the original document, the failed extraction and the specific validation error.
| Retries fix | Retries can’t fix |
|---|---|
| Wrong date or number format | Information absent from the provided source |
| Missing or misplaced keys | Data that lives only in an external document you didn’t supply |
| Structural output errors | — route these to null + human review |
In review pipelines, add a detected_pattern field to findings so you can analyse which code constructs developers keep dismissing.
🌍 Real-world example: Fixing a date format, not a missing value
An extraction returns '12/11/26' where ISO format is required. ShopNest sends back the document, the bad output and the exact error, and the retry returns '2026-11-12'. When the supplier simply left the date off the invoice, retrying can't help, so the field stays null and a human checks it.
🛠️ Try it yourself (about 10 minutes)
- Write a small schema for a receipt: shop name (required), date (nullable), total (required).
- Give Claude a receipt text with no date in it.
- Ask it to fill the schema using a tool.
- Check what comes back for the date.
✅ You are done when: the date comes back as null, not an invented date.
🧪 Exam-Style Practice Questions
Try these before opening the answers. They use the same scenario style as the real exam.
Question 1. Which schema design reduces fabrication?
- A. Make fields that may be missing from the source nullable or optional
- B. Remove all enums
- C. Make every field required
- D. Use free-text fields only
Show answer
Answer: A. Required fields push the model to invent values. C causes exactly that. B and D lose structure.
Question 2. What can a retry with validation-error feedback not fix?
- A. A value that doesn't exist anywhere in the provided document
- B. A date in the wrong format
- C. A string where a number was expected
- D. A missing required key
Show answer
Answer: A. Absent information can't be produced by retrying, which only invites fabrication. B, C and D are format or structure errors that retries do fix.
Question 3. Why use tool_use with a JSON schema for extraction?
- A. It reliably produces schema-compliant, syntactically valid structured output
- B. It removes the need for validation
- C. It's cheaper than text
- D. It makes outputs semantically correct
Show answer
Answer: A. B and D are wrong: semantic errors are still possible, so you still validate. C isn't the reason.
📝 Assignment
Do these tasks in order. Each one has a Done when check, so you know when it is complete. The stretch task is optional but close to what the exam expects from an architect. Keep your answers in one notes file or Git repository, and revisit them in your final week of revision together with the cheat sheet in the last lesson of this course.
- Task 1: Define an extraction tool with one nullable field and an enum with other and unclear values.
✅ Done when: Your schema has a nullable field, an enum containing other and unclear, and a detail string for the other value. - Task 2: Process a document missing one field and confirm you get null, not a made-up value.
✅ Done when: The missing field comes back as null in three runs out of three. - Stretch: build a validation-retry loop that sends the document, the failed output and the exact error.
✅ Done when: A deliberately wrong date format is corrected on the retry, and your log shows the exact error you sent back.
❓ Frequently Asked Questions
🤔 How do I guarantee valid JSON from Claude?
Define a tool whose input schema is your output schema and read Claude's tool_use arguments.
🤔 Does a JSON schema guarantee correct data?
No. It stops syntax errors, but values can still be wrong, such as totals that don't add up.
🤔 Why make fields nullable?
A required field that is missing from the document pushes the model to invent a value.
🤔 When does retrying fail?
When the information is not in the provided source. Return null and route the document to a human.
🏁 Summary
- tool_use + JSON schema = reliable structure; validate semantics separately.
- Nullable fields prevent fabrication; enum “other” + detail keeps categories extensible.
- Retry with specific feedback for format errors; never retry missing information.
Next, continue with Claude Message Batches API and Multi-Pass Review Design.
Independent study material by SQA Tools. Not affiliated with, endorsed by or sponsored by Anthropic. Claude is a trademark of Anthropic. Mock questions are original practice items, not real exam content. Exam facts checked against Anthropic's official exam guides and certification FAQ on 11 November 2026.