IamBishal

Right Shape, Wrong Number: Structured Outputs, Measured

Structured outputs guarantee the shape. I graded every field of 1,680 invoice records to see what they don't guarantee.

120 invoices with deliberate traps, four ways to ask for JSON and three schema designs, 1,680 records graded field by field on Claude Haiku and Sonnet. The models read every value correctly; every error came from the schema.

wrong totals, dates or numbers in 1,620 records
0
JPY and CAD invoices recorded as USD
30 / 30
fully right: base vs designed schema
88% → 100%
schema-valid / still wrong, base schema
100% / 12%
  • Experiment
  • 6 min read
  • Developer Tools
  • Evals & AgentOps
  • Python
  • Claude
  • Structured outputs
  • JSON Schema
Figure 2. What happened to a JPY or CAD invoice

Overview

In "Structured Outputs from LLMs" I argued that constrained decoding solves syntax, not semantics: a schema can guarantee a total is a number, not that it is the right number. The article also recommended schema choices it hadn't measured: an explanation-first notes field, an OTHER value in closed enums, required-but-nullable fields, and business-rule validation after the schema. This experiment measures them.

The documents are 120 invoices rendered from real orders in the Kestrel Outfitters data from my Agentic AI book series, so every field has a known right answer. They come in four layouts (a clean table, a forwarded email, a noisy scan, prose), and most carry one deliberate trap: an order value printed bigger than the amount due, a pre-discount total, a JPY or CAD partner invoice, no due date, a credit note, a date like 09/10/2025 on a UK invoice, or the amount due only in words.

Seven conditions extract the same invoices: instructions only, tool use, strict tool use and structured outputs with a first-draft schema, then structured outputs with a designed schema, the designed schema without its notes field, and the designed schema plus business-rule checks with one retry. Every record is graded field by field against the truth, alongside whether it matched its schema. Claude Haiku 5.5 and Sonnet 5.5 produced 1,680 records for USD 4.87.

The models read every value trap correctly. Across the 1,620 records that came back there was not one wrong total, subtotal, date or invoice number. Every error came from the request. A closed USD, GBP or EUR enum made structured outputs record a ¥114,184 invoice as USD 114,184, schema-valid and wrong, on all 30 such records. The tool conditions declined to call the tool at all, and an optional due date was dropped four times though it was printed.

The designed schema, with descriptions, OTHER in the enum and a required-but-nullable due date, took structured outputs from 88% to 100% of records fully right on both models. The business rules caught none of the forced-USD records, because their arithmetic still added up. Two claims couldn't be tested on documents this clean: the notes field had no errors left to fix, and the validated condition never needed to retry.

Did it hold up?

Full results

The hypothesis, written down before the first run, and what the results said.

  1. Partly

    Structured outputs and strict tools give 100% schema-valid records; instructions and non-strict tools fall short.

    Structured outputs were valid on all 960 records; strict tool use only 88%, because the model declined to call the tool on JPY or CAD invoices.

  2. Partly

    Constrained decoding doesn't fix values: valid-but-wrong records appear, concentrated in the traps.

    12% valid-but-wrong with the base schema, but all from the closed currency enum; no subtotal, discount, date or words trap caused an error.

  3. Confirmed

    A closed enum forces a wrong answer; OTHER lets the model say none of these.

    30 of 30 JPY/CAD records came back as USD under the base schema; with OTHER, 90 of 90 came back OTHER.

  4. Partly

    Required-but-nullable beats optional for a missing due date.

    No condition invented a date, but the optional field let printed due dates be dropped 4 times; required-but-nullable never did (720 records).

  5. Not testable

    An explanation-first notes field improves accuracy on the ambiguous traps.

    With and without notes the designed schema scored 100% on both models: no headroom. The notes cost 15 to 18% more.

  6. Wrong

    Business rules catch most wrong records the schema can't, and one retry repairs a meaningful share.

    The rules caught none of the forced-USD records, whose arithmetic still added up; with the designed schema there was nothing left to retry.

What it does

  • 120 invoices built from real orders, with known true values and eight trap types
  • Four ways to ask for JSON: instructions, tool use, strict tool use, structured outputs
  • A first-draft schema against a designed one: descriptions, OTHER, required-but-nullable
  • An ablation of the explanation-first notes field
  • Business-rule validation with one retry that carries the errors back to the model
  • Field-by-field grading in code, plus the valid-but-wrong rate the article warns about
  • An offline dry run with an oracle and a mistake-maker that proves the grader for free

How it's built

  1. Inputs

    • 120 invoicesRendered from real orders
    • 8 trap typesSubtotal, currency, dates, words…
  2. Seven ways to ask

    • Instructions, tool, strict tool
    • Structured outputsbase schema
    • Designed schema, with and without notes
    • Business rules plus one retry
    • Haiku 5.5 and Sonnet 5.5
  3. Outputs

    • Records1,680, with schema errors
    • Field graderEight fields against the truth
    • Scorecard and chartsValid vs correct, by trap
schemas.py and rules.py
# schemas.py: the base schema's two quiet assumptions...
"currency": {"type": "string", "enum": ["USD", "GBP", "EUR"]},   # every invoice is in one of these
"due_date": {"type": "string", "format": "date"},                # optional: absent = "not printed"... or forgotten

# ...and the designed schema, which states what the base one only assumed.
"currency": {"type": "string", "enum": ["USD", "GBP", "EUR", "OTHER"],
             "description": "The document's currency. OTHER when it isn't USD, GBP or EUR."},
"due_date": {"anyOf": [{"type": "string", "format": "date"}, {"type": "null"}],
             "description": "The payment due date as YYYY-MM-DD, or null when the document doesn't state one."},
"discount": {"type": "number", "description": "The discount amount as a positive number, or 0 when there is none."},


# rules.py: what a schema can't say. A forced "USD 114,184" still passes this check.
def check(r):
    errors = []
    expected = r["subtotal"] - r["discount"] + r["tax"] + r["shipping"]
    if abs(expected - r["total"]) > 0.033:
        errors.append(f"subtotal - discount + tax + shipping = {expected:.2f}, but the total is {r['total']}")
    ...
    return errors

Figures

Figure 1. Right shape is not right answer1 of 3

What I learned

  • A schema-valid record is not a correct one. Structured outputs were 100% valid and 12% wrong with a first-draft schema.
  • Constrained decoding enforces what your schema claims, including what's false. A closed enum turned ¥114,184 into USD 114,184.
  • Strict tool use constrains the arguments of a call, not whether the call happens: on every JPY or CAD invoice the tool was never called.
  • Optional fields hide omissions. Four printed due dates were dropped; the required-but-nullable version never dropped one.
  • Say every convention out loud. One description, "a positive number", removed every negative-discount inconsistency.
  • Business rules only catch what doesn't add up. A consistently wrong record, like a forced currency, sails through.
  • On clean documents the models read the values perfectly. The design of the request decided the results more than the mechanism did.

Next steps

  • Harder documents: image scans, multi-page invoices, handwritten corrections
  • Rerun the notes ablation where accuracy has room to move
  • Measure the rules' catch rate on deliberately corrupted records
  • Add more closed enums (document type, payment method) and count the forced answers

Run it yourself

Everything on this page comes from code and data in the repository. The dry run is free and needs no API key. A full live rerun costs about USD 5.

Terminal
git clone https://github.com/bishal-saha/ai-lab-code.git
cd ai-lab-code/structured-output-benchmark
uv sync
uv run right-shape --dry-run         # an oracle and a mistake-maker through every condition, $0
mv results results-published         # the live runner resumes from committed results
cp .env.example .env                 # then add your ANTHROPIC_API_KEY
uv run --env-file .env right-shape --max-usd 10
uv run python -m right_shape.report

Bishal SahaLet's build together

Have a system to design, or an AI feature to get right?

Tell me about the problem. You'll get a clear, honest view of the best way forward.

  • Reply within 24 hoursOn business days, with my honest view and a next step.
  • Accepting select engagementsArchitecture, AI features and full builds.
  • Remote, worldwideWorking with startups, SMEs and enterprises.