Lesson 4.3Lesson 21 of 30
Structured output via tool use
Enforce structured output using tool use and JSON schemas
Key points
- 1The most reliable way to get schema-compliant output is a tool with a JSON schema in
input_schema. The data arrives already parsed in thetool_useblock's input, so JSON syntax errors disappear. Don't ask for JSON in the prompt and regex-parse the text. - 2
tool_choice: auto may return text instead of a call; any must call some tool (use it when there are several extraction schemas and the document type is unknown); {type: tool, name} forces one specific tool, e.g.extract_metadatabefore enrichment. - 3A strict schema stops syntax errors, not semantic ones: line items that don't add up to the total or a value in the wrong field still pass. Check those in code.
- 4Make a field required only if every source has it. Fields the source may lack should be nullable, otherwise the model invents values to satisfy the schema. Extensible enums get "other" plus a detail string, and ambiguous cases get "unclear".
- 5A schema checks types, not formats. Put normalisation rules (ISO 8601 dates, units, currency codes, path style) in the prompt alongside the strict schema.
What the exam guide checks here
Objective wording from the exam guide (English).
- 4.3.K1tool_use with JSON schema is the most reliable route to schema-compliant output, no syntax errors
- 4.3.K2tool_choice auto (may return text) vs any (must call some tool) vs forced (specific tool)
- 4.3.K3Strict schemas stop syntax errors, not semantic errors (sums, wrong fields)
- 4.3.K4Schema design: required vs optional, enum with "other" + detail
- 4.3.S1Define extraction tools with JSON schema inputs and read data from tool_use
- 4.3.S2tool_choice any when several extraction schemas exist and type is unknown
- 4.3.S3Force extract_metadata to run before enrichment
- 4.3.S4Make fields nullable when sources may lack them to stop fabrication
- 4.3.S5Add "unclear" and "other" + detail enum values
- 4.3.S6Add format normalisation rules to prompts alongside strict schemas
Lock it in
Answer scenario questions on this topic. Every option comes with an explanation.
Practise 4.3 · 26 questions