4.3 Enforce structured output using tool use and JSON schemas
Get schema-compliant structured output through tool use with JSON schemas (or structured outputs), choose the right `tool_choice`, and design schemas (nullable fields, extensible enums, normalisation rules) so the model is never forced to fabricate.
Key points
- 1
Tool use with a JSON schema is the most reliable way to get structured output: define an extraction tool whose
input_schemais the record schema, force the call, and read the record from thetool_useblock'sinput. This eliminates JSON syntax errors, preambles and markdown fences that breakJSON.parseon text responses. - 2
tool_choicehas three relevant settings:{"type": "auto"}lets the model answer in text instead of calling a tool;{"type": "any"}forces it to call one of the provided tools but lets it choose which;{"type": "tool", "name": "extract_metadata"}forces that specific tool. (noneprevents tool calls.) - 3
Use
anywhen several extraction schemas exist and the document type is unknown but structured output is mandatory. Use a forced named tool when a particular extraction must run first, before a dependent enrichment step, and let the application sequence the follow-up. - 4
Prompt instructions such as "always call extract_metadata first" make an ordering likely; a forced
tool_choicemakes it certain. When the ordering or the structured response must always hold, use the API-level control. - 5
Strict schemas (
strict: trueon the tool, oroutput_config.formatstructured outputs) guarantee syntax and types, not semantics. Line items that do not sum to the total, or a unit price placed in thequantityfield, are valid JSON and pass the schema; catch them with semantic validation in code. - 6
Numeric range constraints (
minimum,maximum), string length limits and recursive schemas are not supported by strict structured output, and even in an ordinary validator they cannot express cross-field rules such as "rows must sum to total". - 7
Required fields force the model to produce a value even when the source has none, which is where fabricated PO numbers and invented dates come from. Make a field nullable (
anyOfstring/null) whenever the source may legitimately lack it, and describe when null is expected. - 8
Do not make everything optional: fields that are always present should stay required so validation still catches missing data. A prompt line saying "never guess" cannot override a schema that demands a value.
- 9
For categorical fields, use an enum extended with
"other"plus a detail string (and"unclear"where ambiguity is common) so unusual cases are captured without breaking the controlled vocabulary. Free-text categories destroy downstream consistency; a closed enum forces wrong assignments. - 10
Include format normalisation rules in the prompt alongside the schema: locale for numeric dates (day/month/year), the output format (ISO 8601), units, and the reference date for relative expressions. The schema constrains the shape of the value; the rules govern its interpretation.
- 11
Carry provenance for ambiguous values: a nullable source-text field and an ambiguity flag let downstream validation route uncertain records to review instead of accepting a confident but arbitrary reading.
- 12
A self-attestation field (
is_valid: true) is filled by the same model that made the mistake and is not validation. Switching between tool use andoutput_configformat changes nothing about semantic guarantees. - 13
Newer models may reject forced tool selection (
anyor a named tool); the documented alternative isautowithstrict: truetools or structured outputs. The exam tests theauto/any/ named-tool distinction as described in the exam guide.
Read the source
Further reading
Suggest a resource- Cookbook: Extracting structured JSON using tool use(opens in a new tab)Using tool schemas to extract structured JSON for summaries, entities and classification.Articleplatform.claude.comEditor’s pick
- Cookbook: Tool choice(opens in a new tab)Shows how tool_choice auto, any and a named tool change whether Claude must call a tool.Articleplatform.claude.comEditor’s pick
- Agent SDK: Get structured output from agents(opens in a new tab)How to have an Agent SDK run return JSON that matches a schema.Docscode.claude.comEditor’s pick
Test yourself on 4.3 Enforce structured output using tool use and JSON schemas
Ten questions, with the answer and explanation after each one.