Structured Output: JSON Mode Missed the Keys 24 of 24 Times
json_object returns valid JSON every time and still breaks the pipeline, because validity says nothing about the key names your code reads.
📜 Table of Contents
📖 Keep reading
Three ways to get structured records out of a language model, seventy-two calls, one test set. The cheapest mode won on price, on speed and on correctness, and it is the one with no schema at all: a plain text template parsed by fifteen lines of Python.
The mode that the documentation recommends for exactly this job, json_object, returned syntactically valid JSON twenty-four times out of twenty-four and used the wrong key names twenty-four times out of twenty-four. It is not that the answers were bad. Every value was right. The keys were called lines and quantity instead of line_items and qty, so a consumer expecting line_items reads an empty list and reports a record with no items.
The forced tool call was the only mode that enforced the schema, and it charged 362 extra input tokens per record for the privilege. Numbers below; the decision rule at the end is three lines long.
The three modes, and what was asked
- Twenty-four synthetic purchase orders, each naming a vendor, a PO number, a currency from three, two to five line items, and a trade discount that is absent on every fourth record. The text is one paragraph of prose, values embedded mid-sentence.
- Same system prompt and same record in all three arms, temperature 0, deepseek-flash, thinking mode disabled, 2026-11-28.
- Text template: five labelled lines, line items as sku|qty|price joined with semicolons, parsed with a regex. Validity here means the parser produced an object.
- json_object: response_format set, schema described in prose only. Tool call: a JSON Schema with required on all five fields, an enum for the currency and additionalProperties false, forced with tool_choice.
- Grading is mechanical: required keys present, currency inside the enum, discount as a fraction within 0.0005, same item count, every sku, quantity and unit price equal to the source.
Results
| mode | valid JSON | required keys present | records fully correct | prompt tokens per record | output tokens per record | mean ms |
|---|---|---|---|---|---|---|
| text template | 24 of 24 | 24 of 24 | 24 of 24 | 195 | 73 | 614 |
| json_object | 24 of 24 | 0 of 24 | 0 of 24 | 167 | 140 | 737 |
| forced tool call | 24 of 24 | 24 of 24 | 24 of 24 | 557 | 203 | 878 |
Read the second and third rows together. JSON mode produced a well-formed object every single time, which is what the parameter promises, and none of the twenty-four matched the contract the downstream code was written against. The tool call matched twice as often as that and cost just under three times the input tokens of the text template.
Validity is not conformance
The first json-mode answer is worth looking at whole. It reads: po_number, vendor, currency, lines, discount, payment_terms. Three fields renamed, one field invented, no nesting error anywhere. A JSON schema validator would reject it, json.loads accepts it happily, and a pipeline that does obj["line_items"] gets a KeyError that fires three hours later in a nightly job.
Across the run, JSON mode also spent 1,621 more output tokens than the text template for the same twenty-four records, because it pads the object with fields nobody asked for, payment terms being the recurring one. Nobody asked for a schema, so it invented a reasonable one.
The one validator message fixed all twenty-four
The retry arm took each failing json-mode answer, appended a single user turn naming the required keys, and asked again. Twenty-four of twenty-four came back conforming, at 335 input tokens and 134 output tokens per repair. That is cheap per fix, and it only fires on records that fail, so a pipeline can run json_object at 167 input tokens and pay the repair only where needed.
What it does not fix is the shape of the failure. The first pass looked successful to every check except a key-name comparison, which means the validator has to compare against the contract, not against json.loads. A pipeline that trusts parse success will ship empty item lists and call it a good night.
Two rejections worth knowing
Before any of this ran, twelve json-mode calls in a row came back as HTTP 400. The cause was the prompt: json_object on this provider wants the word json somewhere in the messages, and my first prompt said only extract the fields. Twelve of twelve rejected until the user turn said reply as a JSON object, and then twenty-four of twenty-four succeeded.
The second rejection is specific to a model that thinks before answering. Forcing a named tool with tool_choice returned Thinking mode does not support this tool_choice, twice, until thinking was disabled in the request body. A forced tool call and a reasoning pass are mutually exclusive on this API, and the error message at least says so plainly.
Cost per thousand records
| 1,000 records, off-peak | input | output | total |
|---|---|---|---|
| text template | $0.0293 | $0.0436 | $0.0729 |
| json_object | $0.0251 | $0.0841 | $0.1092 |
| json_object plus one validator retry | $0.0757 | $0.1645 | $0.2401 |
| forced tool call | $0.0836 | $0.1216 | $0.2052 |
Input at $0.15 per million cache-miss tokens and output at $0.60 per million, off-peak on 2026-11-28. The text template is 2.8 times cheaper than the tool call and 3.3 times cheaper than json plus a repair on every record. Where the fixed system prompt in a real job is long enough to hit the cache, those input figures drop by the factor measured in the cache run, which narrows the gap between the modes but does not reorder them, since output tokens are not cached.
What to do in a batch job
- Start with the text template and a validator that compares parsed values against the contract. For flat records it was the cheapest and the most accurate thing measured here, and a parse failure is loud instead of silent.
- Move to a forced tool call when the downstream consumer is code that needs types, nulls and enums enforced on the way in, and when the 362 extra input tokens per record are cheaper than the retries they prevent.
- Never use json_object alone as a schema guarantee. Use it, if at all, as a first pass with a key-name check behind it, and budget one repair message per failing record.
REQUIRED = {"vendor", "po_number", "currency", "discount", "line_items"}
ITEM = {"sku", "qty", "unit_price"}
def check(obj):
if not isinstance(obj, dict) or not REQUIRED <= set(obj):
return "keys"
if obj["currency"] not in {"USD", "EUR", "GBP"}:
return "currency"
for it in obj["line_items"]:
if not ITEM <= set(it):
return "item_keys"
return None
What this does not show
Twenty-four records, one model, one evening, one temperature. The records were generated to be regular, so the text template had an easy time of it: real invoices carry stray footers, two vendors in one document and unit prices printed as 1.234,56. Longer records would push the tool call further ahead on correctness and further behind on tokens. Nested output deeper than one level of items was not tested, and no provider other than this one was compared.
Three numbers to take away: 0 of 24 json_object answers used the required key names while all 24 parsed as valid JSON; 24 of 24 conformed after one validator message; the forced tool call conformed on 24 of 24 at 557 input tokens per record against 195 for the template. The same token accounting in the other direction is in four MCP servers and their 8,038 tokens of schema and in JSON versus CSV tool results; what a repeated long prompt does to the bill is in the cache measurement.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.