Structured Output with tool_use
Two supported mechanisms
| Mechanism | How | Guarantee |
|---|---|---|
| JSON outputs | output_config.format with type: "json_schema" and a schema | Response text is valid JSON matching the schema (constrained decoding) |
| Strict tool use | strict: true on a tool | Tool names and inputs conform to the schema |
They can be used together. The older output_format parameter is deprecated. SDK helpers exist (client.messages.parse() with Pydantic in Python, zodOutputFormat() in TypeScript).
The classic pattern: a tool as a schema (design guidance, uses verified tool mechanics)
Define a tool whose input_schema is the shape you want (for example record_invoice), and read the structured data from the tool_use block's input. Combine with tool_choice where supported (see 2.3): newer models reject forced any/tool, so use auto plus strict, or JSON outputs.
Prefilled assistant turns are no longer supported on newer models (400 error); Anthropic's migration advice for forcing a format is structured outputs.
Schema design (verified constraints)
Supported: object, array, string, integer, number, boolean, null; enum, const, anyOf, allOf (not with $ref), internal $ref, required, common string formats (date-time, date, email, uri, uuid ...). Every object needs additionalProperties: false.
Not supported by the grammar: recursive schemas, minimum/maximum, minLength/maxLength, minItems above 1, external refs, regex backreferences/lookaround. (SDK helpers may move these into descriptions and validate client-side.)
Limits: 20 strict tools per request, 24 optional parameters across strict schemas, 16 parameters using anyOf or type arrays; too complex returns a 400. Grammar compilation adds first-request latency; compiled grammars are cached for 24 hours.
Fields that prevent fabrication (design guidance)
- Nullable or optional fields for information that may be absent (
"type": ["string","null"]), so the model can say "not present" instead of inventing a value. - Enums with an escape value (
"other"plus a free-text detail field, or"unclear") so unusual cases do not get forced into the wrong category. - Separate extracted fields from inferred ones and say which is which.
What a valid schema does NOT guarantee
- Semantic correctness. A value can be well-formed and wrong.
- Enum capitalisation can vary; compare case-insensitively.
- Unsupported constraints (for example
minimum) are not enforced by the API. - Conformance on truncated or refused responses:
stop_reason: max_tokensmay cut the JSON off (retry with a higher limit);refusalreturns a 200 and the text may not match the schema. - Asking for step-by-step reasoning as a field can trigger a
reasoning_extractionrefusal; ask for a short explanation. - Streaming: accumulate the full text before parsing.
- Citations are incompatible with structured outputs on the same request (5.6).
Exam traps
- "Parse the JSON from free text with a regex." Use schema-constrained output.
- Assuming schema-valid means correct.
- Required fields for data that might not exist, forcing hallucination.
- Forgetting
additionalProperties: falseor using unsupported keywords. - Checking
stop_reasonlast (or never).
References
- Structured outputs — checked Oct 16, 2026
- Define tools — checked Oct 16, 2026
- Prompting best practices — checked Oct 16, 2026
Check your understanding
A field may legitimately be missing from the source document. Schema design?Show answerHide answer
Answer
Make it nullable or optional, not required.Which
stop_reasoncan leave you with truncated, unparseable JSON?Show answerHide answerAnswer
max_tokens.Schema-valid output can still be wrong. Name one thing the schema cannot check.Show answerHide answer
Answer
Semantic correctness, such as whether the extracted value matches the document.