# 25.7 Structured outputs: a schema on the response (/building-ai-workers/prompts-and-structured-output/structured-outputs)

---
type: Document
title: "25.7 Structured outputs: a schema on the response"
description: "A schema the API holds on the response, how the model says a value is not on the invoice, and what a schema still cannot do."
status: stable
order: 325.07
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T00:02:23Z
chapter: '25'
part: III
expert_status: co-build
concepts: [ "25.7" ]
last_verified: '2026-11-29'
sources:
  - id: anthropic-structured-outputs
    title: Structured outputs (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/structured-outputs
  - id: anthropic-prep-course-prompting-craft
    title: Claude Certified Developer, Foundations prep path, Module 2, Prompting Craft, Anthropic Partner Academy (sign-in needed, read 2026-11-29)
    resource: https://anthropic-partners.skilljar.com/path/claude-certified-developer-foundations/production-grade-prompting-agents-tool-use/486743/scorm/3k33sihfb1tww
generated:
  at: 2026-11-30T00:02:23Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:cc6fd9ac5f72207d4365ecbda63d87d1b64f99e05519b358a45b9f5d9bfd6f8f
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** A clinic's registration form has a box for your date of birth. It has a list of blood groups to tick, with "not known" at the end. It has a line for a second phone number, marked "leave empty if none". You cannot write a paragraph in a tick list. You can still tick the wrong blood group. The form holds the shape of your answer and knows nothing about whether it is true.

**What you will learn.** You practice this in Lab 25, Task 3.

- What is a structured output?
- Why does the list end with "other"?
- How does the model say "not on the invoice"?
- What does a schema on the response cost?
- What can the schema still not do?

**Why it matters.** A person coped with any layout. Code cannot, so something must hold the form before the code reads it.

**At work.** With the red case fixed (25.6), the builder turns to INV-8340 and its "Net 30 days" (the overview). Lab 24's check refused that answer after it was written. The builder wants an answer that cannot be written that way at all.

### What is a structured output?

A structured output is an answer the API holds to a JSON Schema that you send with the request. The builder could add the five terms to the prompt, and the model could still miss them (25.3). A schema sent this way is enforced while the answer is being written. At each step the model can only write what the schema allows next. The prep course's name for this is constrained decoding.[^anthropic-prep-course-prompting-craft]

On the Messages API the schema goes in `output_config.format`:[^anthropic-structured-outputs]

```json
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 1024,
  "output_config": {
    "format": {
      "type": "json_schema",
      "schema": {
        "type": "object",
        "properties": {
          "number": { "type": "string" },
          "vendorId": { "type": "string" },
          "invoiceDate": { "type": "string", "format": "date" },
          "terms": {
            "type": "string",
            "enum": ["Due on receipt", "Net 15", "Net 30", "Net 45", "Net 60", "other"]
          },
          "poNumber": { "type": ["string", "null"] },
          "subtotalCents": { "type": "integer" },
          "taxCents": { "type": "integer" },
          "amountCents": { "type": "integer" }
        },
        "required": ["number", "vendorId", "invoiceDate", "terms", "poNumber", "subtotalCents", "taxCents", "amountCents"],
        "additionalProperties": false
      }
    }
  }
}
```

<!-- OPEN: the field list follows the Chapter 24 spec (subtotalCents, taxCents, poNumber beside 14.5's amountCents). The fields are to be decided in specs/labs/part-2-facts.md first. Which field is nullable (poNumber here) is this drafter's choice and needs the same decision. -->

<!-- EDITOR: the answer has the same eight fields in 25.3, 25.7, 25.8, 25.9 and Lab 25: number, vendorId, invoiceDate, terms, subtotalCents, taxCents, amountCents and poNumber. 14.5's dueDate and status are left out, on the reading that code computes the first and the portal sets the second. The other option: the answer carries all of 14.5's fields, with dueDate nullable. -->

The answer comes back as JSON in the response's text block.[^anthropic-structured-outputs] `"additionalProperties": false` is required, and it means no other field may appear. With `terms` as an **enum**, a fixed list of allowed values, "Net 30 days" can no longer be written at all. This is [14.5](../structuring-programs/schema-rules.md)'s rule, "one of a fixed set". What changed is who holds it. Now the API holds the shape while the answer is written. Structured outputs are not offered in Managed Agents, which is why the extraction is a Messages API call from the portal's code.[^anthropic-structured-outputs]

Ana labels INV-8340 as a new case, so the suite holds 42. The whole suite runs, and the new case passes as Net 30.

### Why does the list end with "other"?

**Because a form with no honest answer forces a dishonest one.** Suppose an invoice prints terms that are not among the five. With only five values allowed, the model must pick one of them. The answer has the right shape and a wrong value, and the schema made it so.

"other" lets the model say "this is not one of yours" inside the form. If the model picks "other", the failure moves from shape to value (25.9).

### How does the model say "not on the invoice"?

**With a nullable field.** A nullable field may hold `null`, which means "no value". Some invoices print no purchase order number. If `poNumber` must be a string, the model has to write something, and an invented PO number looks exactly like a real one. Written as `"type": ["string", "null"]` and still listed as required, the field must always be there, and the model can fill it with `null`. This is the same pattern as [21.2](../agent-loop-by-hand/a-tool-is-a-name-a-description-and-a-schema.md)'s tool inputs.

The person who read the answer could tell a blank from a guess. "other" and `null` are how the schema lets the model tell your code which one it is.

### What does a schema on the response cost?

**Four small costs, and one large one.** The prep course names the four.[^anthropic-prep-course-prompting-craft]

- **A slower first call.** The first request with a new schema takes longer, because the API first compiles the schema, which means it turns it into rules it can enforce.
- **A cache with a clock.** The compiled schema is kept for 24 hours from its last use.[^anthropic-structured-outputs]
- **Slightly more input tokens.** The API adds a system prompt of its own that describes the format.
- **No prefill** (25.3). It cannot be combined with structured outputs.[^anthropic-structured-outputs]

<!-- EDITOR: the line that said a schema used once each weekday morning is compiled again after every weekend is cut, here and in quiz item 25.7-04, because the source says only "24 hours from last use". The other option: keep it and add "and may be compiled again on a weekday too". -->

There is a fifth cost, and it is the largest. The schema you send can hold less than your own: 25.9 says what is left out.

Asking for JSON in the prompt and checking it in code is still fine where no enforced schema is offered. Lab 23's morning report, written inside a hosted session, does exactly that.

### What can the schema still not do?

**Two things: it cannot make every answer match, and it cannot make any answer true.**

**It does not make every answer match.** A decline or a cut answer may not fit the schema, so the code reads the stop reason first (25.10).[^anthropic-structured-outputs]

**It holds the shape, never the truth (25.9).** The red case had every field in place and a wrong total, and a schema passes it.

**The operating rule:** let the API hold the shape. Give every fixed list an "other", and make every field that can be missing nullable. The model can then say "not on the invoice" inside the form. Last, check the values yourself.

**Do this now.** Ask your coding agent to draft the schema before any request is sent. Copy this line:

> Write the JSON Schema for the invoice extraction as the API will hold it. Make the terms an enum of our five terms plus "other". Make every field that an invoice may leave out a string or null, and keep it required. Close every object to extra fields. Then list each rule in our code's own invoice schema that you left out, and why. Send no request.

Check: the list of rules left out should include the number range on the amount. Those are the rules your code must still check.

[^anthropic-prep-course-prompting-craft]: Claude Certified Developer, Foundations prep path, Module 2, Prompting Craft, Anthropic Partner Academy.
[^anthropic-structured-outputs]: Structured outputs, Anthropic.
