Small models and JSON: what breaks, and what each fix buys
An AI agent loop needs more than a correct answer: it needs the answer in an exact structure. Small language models struggle with that, which makes them hard to build on. I measured where they fail and what helps.
Details
- Published
- Reading time
- Long read · 10 min
The short version
Hypothesis. A small language model can fail at JSON in two ways. The format breaks, or the format holds and the content is wrong. I wanted to know which fix repairs which.
Method. I followed one model, Qwen3-1.7B, through four tasks. I sorted every answer into fully correct, wrong content or broken format, and I tested each fix alone. Ten more models are the check at the end.
Result. A grammar mask repairs the format and never the content. Training on 5,000 examples repairs both. On labels the model scores 53.0% as it comes, 53.6% with the mask and 91.8% after training.
Part 01
The task
From text to structure
A program cannot read a sentence. It reads a structure: named fields, each with a value of a known type. Structured output turns the first into the second. Today the structure is mostly JSON, written by a language model.
Text to structure has three levels
Each row turns a text into JSON. The levels differ in where the values come from.
- Copied from the text
- Not in the text
Level 1
Copy
Values already stand apart
amount: 250; currency: EUR; to: Anna
a rule
{ "amount": 250, "currency": "EUR", "to": "Anna" }
For example: log lines, CSV rows, forms
Level 2
Extract
Values inside a sentence
Please send 250 EUR to Anna tomorrow.
reading
{ "amount": 250, "currency": "EUR", "to": "Anna" }
For example: named entity recognition, function calls
Level 3
Decide
Answer not in the text
I sent Anna 250 EUR on Monday. When will it arrive?
judgement
{ "intent": "transfer_timing" }
For example: classification, questions about a text
Level 1 needs no model. Levels 2 and 3 do. At every level the output must also be exact: a human reader forgives a missing brace, and a parser does not.
Two ways to fail
A program checks an answer twice. It must be able to read the answer, and the values must be right.
An answer can fail in two ways
One bank message and three answers. The answer must be one of 77 labels.
My account has a weird withdrawal.
Fully correct
{"intent": "cash_
withdrawal_ "}not_ recognised - Format
- Content
Right format, wrong content
{"intent": "transfer_
not_ "}received_ by_ recipient - Format
- Content
Broken format
{"intent": "withdrawal_
issue "}Not one of the 77 labels
- Format
- Content
A broken format is an answer the program cannot use. Wrong content is an answer it can use, with a wrong value in it. These outcomes keep their colours in every figure below.
How often each one happens
I tested eleven open models, from 135 million to 8 billion parameters. This post follows one of them, Qwen3-1.7B, which runs on a laptop. The other ten come at the end, in one table.
The model answered four tasks, 500 test records each.
Most answers are well-formed. Many of them are wrong.
Each bar is 500 answers of Qwen3-1.7B to one task, sorted into the three outcomes of figure 2.
- Fully correct
- Right format, wrong content
- Broken format
Pick a label
Which of 77 intents is this bank message?
List the entities
Find every name in a sentence, and its type
Fill a nested answer
Answer a question about a text, in a given schema
Call a function
Turn a request into a function name and its arguments
The grey parts are small. On labels, 21 of 500 answers (4.2%) break the format, and 214 (42.8%) hold a wrong label. Only the entity lists break often: 92 of 500 (18.4%). For this model the format is the smaller problem and the content is the larger one.
Under the hood · How I score an answer · 2 min
Every answer goes through the same three checks, in this order.
- Does it parse? The whole output must be one JSON value. Text before or after it, such as a code fence, fails this check. So does an answer that runs out of tokens.
- Does it fit the schema? I validate the value against the task’s JSON Schema. A missing key, an extra key, a wrong type or a label outside the allowed list fails here.
- Is every value right? For labels, the label matches. For function calls, the name matches and every argument is one of the values BFCL accepts. For nested answers, the object equals the reference. For entities, the list holds exactly the right (text, type) pairs.
An answer that fails check 1 or 2 is a broken format. One that passes both and fails check 3 is wrong content. For entities I also report F1 over (text, type) pairs, because a list with one missed name is not worthless.
Differences come with 95% bootstrap intervals over records, paired on the same records. Three training seeds move an adapter’s score by at most 2.0 points. Another batch layout changes up to 70 of 500 outputs and moves a score by at most 0.6 points. I read a difference under 2 points as no difference.
Part 02
Inside one answer
One token at a time
A language model writes one token at a time: a word or a piece of one. Before each token it gives every token of its vocabulary a probability, and it writes the most likely one.
Here is one answer from the label task. The right label is card_acceptance.
A model writes one token at a time, and one token decides this answer
Before each token the model gives every token of its vocabulary a probability. Here are its best candidates at each step. The answer plays by itself and stays on each key moment. Pause it, or step with the buttons.
The prompt, shortened
Classify the intent of this banking customer message. Reply with one JSON object that follows this JSON Schema.
{"intent": one of 77 labels} such as card_acceptance, card_arrival, exchange_rate, visa_or_mastercard
It will be fine to use at any establishment that accepts Mastercard.
The answer so fartoken 5 of 10
{"intent": "visa_or_mastercard"}end
The model's best candidates for this token
- visa60%written→ visa_or_mastercard
- card36%→ 10 labels, one of them card_acceptance, the right one
- fi3%→ fiat_currency_support
- supported0.4%→ supported_cards_and_currencies
- vi0%→ 2 labels
Nine of the ten tokens are safe, with 88% to 100% each. The answer hangs on the fifth: visa gets 60% and card
gets 36%. This wrong answer is one token that won a close vote. The JSON around it is perfect, so no
check of the format can see the error.
Where the model looks
Attention shows where the model looks while it writes a token. Here is the same answer, and next to it a clear message with the same label.
On the clear message the model locks on one name in the list. On the unclear one it does not.
The prompt, with each token shaded by the attention it gets while the model writes one token of the answer. The answer plays by itself and stays on each key moment. Then switch the message.
The answer. The marked token is the one being written.
{"intent": "visa_or_mastercard"}end
Its attention: 56% on the schema, 18% on the message, 2% on the instructions, 24% on its own answer so far.
Instructions
Classify the intent of this banking customer message. Reply with one JSON object that follows this JSON Schema. Write only the JSON object, nothing before or after it.
Schema, with 5 of its 77 labels
{"intent": ["age_limit", "card_acceptance", "cash_withdrawal_not_recognised", "transfer_fee_charged", "visa_or_mastercard", … and 72 more labels]}
Message
It will be fine to use at any establishment that accepts Mastercard.
A darker token gets more attention. The shade is relative to the strongest prompt token of that step. The underlined name is the right label. The other 72 labels get little attention at every step and are left out.
The answer. The marked token is the one being written.
{"intent": "visa_or_mastercard"}end
Its attention: 56% on the schema, 17% on the message, 2% on the instructions, 25% on its own answer so far.
Instructions
Classify the intent of this banking customer message. Reply with one JSON object that follows this JSON Schema. Write only the JSON object, nothing before or after it.
Schema, with 1 of its 77 labels
{"intent": ["visa_or_mastercard", … and 76 more labels]}
Message
Are both Visa and Mastercard accepted?
A darker token gets more attention. The shade is relative to the strongest prompt token of that step. The underlined name is the right label. The other 76 labels get little attention at every step and are left out.
On the clear message the model reads the label from the list. When it writes visa, almost all of its attention
in the schema is on one name out of 77. Then the attention moves along that name, token by token.
The wrong answer differs at one step. At visa the model locks on no name, and the right label,
card_acceptance, gets almost no attention. The prompt is the model’s only memory. It finds the key in the
schema, the label in the list and the clue in the message.
Under the hood · What attention shows, and what it does not · 2 min
Attention says where a model looks. It does not prove what the model used. So I checked the picture with a second method, gradient times input. It measures how much the score of the written token changes with each earlier token. Here is the whole answer at once, for the clear bank message above and for two function calls.
The request lights up only where a value is copied from it
Each row is one token of the answer, each column one part of the prompt. A cell is the share of the model's attention on that part while it writes the token. The marked rows are the values.
Qwen3-1.7B. The marked rows are the values.
| Token written | Instructions | Schema | Function list | Request | Own answer |
|---|---|---|---|---|---|
| {" | 36 | 49 | 10 | 5 | 0 |
| name | 12 | 55 | 10 | 4 | 19 |
| ": | 4 | 45 | 12 | 3 | 37 |
| ·" | 5 | 43 | 11 | 2 | 39 |
| latest | 4 | 29 | 26 | 9 | 32 |
| _exchange | 2 | 22 | 31 | 10 | 35 |
| _rate | 1 | 18 | 32 | 16 | 33 |
| ", | 2 | 26 | 17 | 6 | 49 |
| ·" | 4 | 40 | 8 | 4 | 44 |
| arguments | 4 | 49 | 6 | 2 | 40 |
| ": | 2 | 36 | 9 | 2 | 51 |
| ·{" | 2 | 36 | 9 | 4 | 50 |
| source | 1 | 36 | 16 | 9 | 38 |
| _currency | 1 | 33 | 16 | 12 | 39 |
| ": | 1 | 27 | 12 | 8 | 52 |
| ·" | 2 | 23 | 11 | 13 | 53 |
| British | 1 | 14 | 9 | 33 | 42 |
| ·P | 1 | 11 | 6 | 31 | 52 |
| ounds | 1 | 11 | 9 | 29 | 50 |
| ", | 1 | 11 | 7 | 16 | 66 |
| ·" | 1 | 23 | 9 | 6 | 62 |
| target | 1 | 29 | 12 | 5 | 53 |
| _currency | 1 | 23 | 13 | 10 | 53 |
| ": | 1 | 16 | 8 | 8 | 67 |
| ·" | 1 | 14 | 6 | 12 | 68 |
| Japanese | 1 | 9 | 5 | 33 | 53 |
| ·Y | 1 | 7 | 4 | 31 | 58 |
| en | 1 | 9 | 5 | 27 | 59 |
| "}} | 1 | 14 | 11 | 7 | 67 |
| end | 7 | 18 | 9 | 5 | 62 |
At the 6 value tokens the request gets 27% to 33%. At every other token it gets at most 16%.
At the 6 value tokens the request gets 13% to 36%. At every other token it gets at most 14%.
Qwen3-0.6B. The marked rows are the values.
| Token written | Instructions | Schema | Function list | Request | Own answer |
|---|---|---|---|---|---|
| {" | 51 | 39 | 7 | 3 | 0 |
| name | 13 | 52 | 8 | 3 | 24 |
| ": | 6 | 42 | 6 | 2 | 43 |
| ·" | 5 | 40 | 7 | 2 | 46 |
| calculate | 6 | 36 | 17 | 5 | 35 |
| _final | 4 | 30 | 20 | 6 | 41 |
| _velocity | 2 | 25 | 25 | 10 | 40 |
| ", | 3 | 26 | 14 | 5 | 52 |
| ·" | 6 | 33 | 7 | 2 | 53 |
| arguments | 6 | 44 | 6 | 2 | 43 |
| ": | 2 | 31 | 9 | 3 | 54 |
| ·{" | 2 | 29 | 11 | 4 | 54 |
| height | 1 | 38 | 16 | 8 | 37 |
| ": | 1 | 25 | 12 | 9 | 54 |
| · | 2 | 22 | 10 | 14 | 52 |
| 1 | 1 | 15 | 9 | 30 | 46 |
| 5 | 1 | 11 | 6 | 31 | 51 |
| 0 | 1 | 12 | 6 | 25 | 56 |
| }} | 1 | 19 | 8 | 8 | 64 |
| end | 9 | 14 | 6 | 4 | 67 |
At the 3 value tokens the request gets 25% to 31%. At every other token it gets at most 14%.
At the 3 value tokens the request gets 8% to 11%. At every other token it gets at most 8%.
Qwen3-1.7B. The marked rows are the values.
| Token written | Instructions | Schema | Request | Own answer |
|---|---|---|---|---|
| {" | 48 | 48 | 4 | 0 |
| intent | 18 | 58 | 3 | 21 |
| ": | 5 | 52 | 6 | 37 |
| ·" | 4 | 53 | 6 | 37 |
| visa | 2 | 56 | 17 | 25 |
| _or | 1 | 49 | 13 | 37 |
| _master | 1 | 50 | 10 | 39 |
| card | 1 | 44 | 11 | 45 |
| "} | 2 | 36 | 10 | 52 |
| end | 16 | 25 | 5 | 54 |
The label is not copied from the request: it is one of 77 names listed in the schema. At its four tokens the schema gets 44% to 56%, and the request 10% to 17%.
The label is not copied from the request: it is one of 77 names listed in the schema. At its four tokens the schema gets 79% to 84%, and the request 3% to 6%.
Shares in percent. A row sums to about 100 with the last column, the model's own answer so far. Chat markers are left out. Saliency asks a different question than attention: how much the written token's score would change with each earlier token.
The two methods agree on the source of each value. In both function calls, the request column peaks at the value
tokens under either method. For the token British, the most salient prompt token is British in the request.
The bank label is different. It is one of 77 names listed in the schema, so the model reads it there and not in
the message.
On the wrong answer the two methods differ at the deciding token. Attention is spread, as the figure above
shows. Saliency points at the rival: the prompt token that moves the score of visa most is _accept, inside
card_acceptance.
The two also disagree on the instructions: saliency gives them about a fifth of the weight (median 22%), and attention almost none. The figures average attention over all heads of layers 15 to 28, the upper half. I replayed the recorded answers on a Mac in 32-bit floats. On the exchange-rate call, the model agrees with the recorded token at all 30 steps.
Part 03
The fixes
A mask over the next token
The standard fix for a broken format is constrained decoding. Before each token, a grammar built from the JSON Schema sets every token that would break the schema to zero. I call this the mask.
The mask removes the label that is not on the list. It cannot choose the right one.
The model's best candidates for the first token of the label, before and after the mask.
The message
My account has a weird withdrawal.
- withdraw95%0%No label starts like this
- transfer4%73%Written under the mask
- cash1%27%The right label starts here
- balance0%0.6%
- Freehand
- Broken format: not one of the 77 labels
{"intent": "withdrawal_
issue "} - With the mask
- Right format, wrong content: a listed label, and not the right one
{"intent": "transfer_
not_ "}received_ by_ recipient - The right answer
{"intent": "cash_
withdrawal_ "}not_ recognised
Freehand, the model writes withdrawal_issue, a label that is not on the list. The mask removes it, and the next
candidate wins. The answer is now valid, and it is wrong: the right label had 1%. The mask can only choose
among the model’s own candidates. It repairs the format and adds no knowledge.
The totals agree. Under the mask all 500 label answers are valid, and 3 more are right: 53.0% becomes 53.6%. The mask costs no speed: 298 tokens per second freehand, 301 with it.
Under the hood · How a grammar mask works · 3 min
A model’s last layer gives one score per token of its vocabulary, about 150,000 scores for Qwen3. A softmax turns the scores into probabilities, and the decoder picks a token.
A mask engine sits between the scores and the pick. It compiles the JSON Schema into a grammar and keeps a parser state for the text written so far. At each step it asks the parser which tokens can continue a valid answer. The scores of all other tokens are set to minus infinity, so their probability is zero.
Here A is the set of allowed tokens and p is the model’s own distribution. The sum in the denominator is the probability the model already put on allowed tokens. In the figure above it is 5%, so the mask changes the answer. When it is near 100%, the mask changes nothing.
The hard part is speed. A token can be several characters, and a parser state can be deep inside nested braces. Checking 150,000 tokens against the grammar at every step would be slow. Outlines (Willard and Louf, 2023) precomputes, for each state of an automaton, the tokens that fit. I used XGrammar (Dong and others, 2024). It splits the tokens into those it can check without the stack and the few that need it. JSONSchemaBench (Geng and others, 2025) compares such engines on about 10,000 real schemas.
The mask guarantees the schema and nothing more. If the schema says “a string”, every string is allowed.
What moves the content
I measured every fix on the label task. The last group is training: a LoRA adapter (Hu and others, 2021). It is a small set of extra weights that learns from examples while the model stays frozen.
For this model, tricks at decoding time change little. Data changes the most.
Qwen3-1.7B on the label task. Each bar is the change in fully correct answers against the stock model, which scores 53%. In points.
- Better than stock
- Worse
- No measurable change
At decoding time
Grammar maskfree
Retry up to five times1 to 5 calls
Think before the answerabout 20 times the tokens
Plain text in place of JSONyou write the parser
Numbers in place of label namesa shorter answer
A bigger model
Qwen3-4B2.4 times the parameters
Qwen3-8B4.7 times the parameters
Training data: a LoRA adapter
50 examples
200 examples
1,000 examples
5,000 examplesabout 20 minutes on one GPU
The black line is the stock model. An adapter is a small set of extra weights, trained on labelled examples of this task.
Nothing at decoding time helps much. Retries change nothing, because a wrong label parses as well as a right one. Plain text in place of JSON loses 12.4 points. That speaks against a popular claim that JSON makes a model less clever (Tam and others, 2024).
A model with 4.7 times the parameters adds 15.8 points. Training on 200 examples adds 14.4, and on 5,000 it adds 38.8: the score goes from 53.0% to 91.8%. With an adapter the model also writes the format by itself, so the mask adds nothing on top.
Under the hood · The adapter in detail · 3 min
One recipe for every model and task, chosen before the runs and not tuned afterwards.
- LoRA rank 8, alpha 16, on the attention and MLP projections. Learning rate 2e-4, 16 records per step, two epochs.
- 5,000 training records per task, taken from the dataset’s training split. The test records are never seen.
- The training target is the reference JSON, written compactly, after the same prompt the stock model gets.
- Function calls train on 5,000 Glaive function-calling examples and test on BFCL, because BFCL has no training split.
Three seeds on two tasks moved the score by at most 2.0 points for labels and 1.6 F1 points for entities. The gain from the adapter is 20 to 110 times that spread.
On one RTX 4090, training takes about 20 minutes for the label task and 72 for the long nested answers. Here is the adapter on all four tasks.
The mask moves almost nothing. The adapter moves the content.
Qwen3-1.7B on each task: as it comes, with the mask, and with an adapter trained on 5,000 examples of that task.
- Fully correct
- Right format, wrong content
- Broken format
Pick a label
Which of 77 intents is this bank message?
- As it comes
- With the mask
- With an adapter
List the entities
Find every name in a sentence, and its type
- As it comes
- With the mask
- With an adapter
Fill a nested answer
Answer a question about a text, in a given schema
- As it comes
- With the mask
- With an adapter
Call a function
Turn a request into a function name and its arguments
- As it comes
- With the mask
- With an adapter
Complete entity lists go from 4.2% to 55.4%, and nested answers from 30.0% to 48.6%. Function calls stay at 92%: that adapter trained on one dataset and was tested on another. With the adapter and the mask together, no score moves by more than 0.4 points.
One adapter per task is a cost. So I tested each adapter on the three tasks it never saw.
An adapter is a specialist: off its own task it does nothing, or harm
Each row is one adapter, each column one task. A cell is the change against the stock model, in points. Read along a row. The last row is one adapter trained on all four tasks.
- Better than stock
- Worse than stock
- Its own task
Qwen3-1.7B
| Trained on ↓ tested on → | Label | Entities | Nested | Function |
|---|---|---|---|---|
| Label | +39 | +8.8 | 0 | −1.4 |
| Entities | −11 | +49 | −11 | −3.2 |
| Nested | −4.8 | +2.5 | +18 | −3.6 |
| Function | −1.2 | +8.2 | −4.2 | 0 |
| All four | +31 | +44 | +9.2 | 0 |
Off its own task an adapter helps little, and it can harm. The entity adapter costs 11.4 points on labels and 10.8 on nested answers. The last row is one adapter trained on all four tasks, with 1,250 examples of each. It keeps most of the gain: 84.2% on labels, against 91.8% for the label adapter.
Part 04
The other models
Ten more models
Now the same four tasks for all eleven models. The tinted rows are the model of this post. Switch the mask on and watch the small models.
Small models break the format. Large ones still miss the content.
One bar per model and task. Three models have a second row: the same model with an adapter. Switch the mask on and watch grey turn blue.
- Fully correct
- Right format, wrong content
- Broken format
Pick a labelBanking77
List the entitiesFew-NERD
Fill a nested answerSOB
Call a functionBFCL
SmolLM2-135M
SmolLM2-360M
with an adapter
Qwen3-0.6B
with an adapter
Qwen3.5-0.8B
Granite 4.0-1B
Qwen3-1.7B
with an adapter
SmolLM2-1.7B
Qwen3.5-2B
Phi-4-mini 3.8B
Qwen3-4B
Qwen3-8B
The number is the share of fully correct answers. For entities that means every entity right, so the bar is strict.
Under 0.5B parameters almost every freehand answer is grey. There the mask is the difference between a broken system and a working one: on function calls it takes SmolLM2-360M from 0% to 55% fully correct.
Size helps the content slowly. Qwen3-8B still gets 30% of the labels wrong. Training helps it fast: on labels, all three tuned models score above the stock 8B model. Function calls are the exception. There the stock 8B model wins, with 95.8% against 92.2%.
Part 05
Conclusions
What I would do
Start with the mask. It is free, and for the smallest models it is the difference between a broken system and a working one. Keep the real label names in the schema: numbers in their place cost this model 24 points.
Then look at what is left. If the answers are well-formed and wrong, no decoding trick will help. Collect 1,000 examples of your own task and train an adapter.
Structured output is two problems that look like one. The format is a skill, and a grammar enforces it. The content is knowledge of the task, and only data or size brings it.
The open question is the adapter’s narrowness. An adapter trained on one task does little for another, and it can harm. I do not know yet how many different schemas a model must see before it follows a new one as well as its own.
Limits
One model carries the main text. The three bank messages are single answers that explain, and the bars carry the evidence. Most of the eleven models are from two families, Qwen3 and SmolLM2. The tasks are English, with one dataset each. The adapters use one recipe that I did not tune. The function-call adapter trained on another dataset than the test, so that task understates what an adapter can do.
Method and sources
Everything ran in four nights on one rented RTX 4090, for 11.03 US dollars in all. Each model answered 300 to 500 test records per task, with greedy decoding and the schema in the prompt.
Under the hood · Models, tasks and decoding · 1 min
Models. SmolLM2-135M, 360M and 1.7B; Qwen3-0.6B, 1.7B, 4B and 8B; Qwen3.5-0.8B and 2B; Granite 4.0-1B; Phi-4-mini. All from their public Hugging Face repositories, with their own chat templates, thinking off unless stated.
Tasks. Banking77 (77 intents), Few-NERD (8 coarse entity types), SOB (nested answers from text) and BFCL (single function calls). 500 test records per cell, and 300 for SmolLM2-135M, the Qwen3.5 models, Granite, Phi-4-mini and the longest runs. The schema stands in the prompt in every arm, so the mask never knows more than the prompt says.
Decoding. Greedy, except where sampling is the subject. The mask is XGrammar 0.2.9 on the task’s JSON Schema. For every masked cell I checked that the grammar accepts every reference answer.
Compute. Four nights on one rented RTX 4090, for 11.03 US dollars in all, and a MacBook for the replays of single answers. More than 400 evaluation cells and about 40 adapters.
Code and data
The code and the result files are not public yet. Every dataset and model above is.
References
- Z. Tam and others, “Let me speak freely? A study on the impact of format restrictions on performance of large language models”, 2024. arXiv:2408.02442.
- B. Willard and R. Louf, “Efficient guided generation for large language models”, 2023. arXiv:2307.09702. The method behind Outlines.
- Y. Dong and others, “XGrammar: flexible and efficient structured generation engine for large language models”, 2024. arXiv:2411.15100.
- E. Hu and others, “LoRA: low-rank adaptation of large language models”, 2021. arXiv:2106.09685.
- I. Casanueva and others, “Efficient intent detection with dual sentence encoders”, 2020. arXiv:2003.04807. It introduces Banking77.
- N. Ding and others, “Few-NERD: a few-shot named entity recognition dataset”, 2021. arXiv:2105.07464.
- A. K. Singh and others, “The Structured Output Benchmark: a multi-source benchmark for evaluating structured output quality in large language models”, 2026. arXiv:2604.25359.
- S. G. Patil and others, “The Berkeley Function Calling Leaderboard (BFCL): from tool use to agentic evaluation of large language models”, ICML 2025. OpenReview.
- S. Geng and others, “JSONSchemaBench: a rigorous benchmark of structured outputs for language models”, 2025. arXiv:2501.10868.
The figures that step through one answer borrow from A. Madsen, “Visualizing memorization in RNNs” (Distill, 2019).
Cite this post
@misc{hamolia2026json,
author = {Vladyslav Hamolia},
title = {Small models and JSON: what breaks, and what each fix buys},
year = {2026},
url = {https://vhamolya.com/blog/structured-output/}
}New posts by email
Each post here is one experiment. Leave your email, and I will write to you when the next one is out.
One email per new post. Nothing else.