Best Local LLM for Reliable Structured JSON Output?
By Hans Kuepper · Founder of PromptQuorum · Discovery engine for open-weight & open-source AI
Reliable structured JSON output from a local LLM comes mainly from grammar-constrained decoding at the inference layer, not from prompting technique alone. Grammar-based tools force the model's token choices to stay valid against a schema at every generation step.
Key Takeaways
- ✓Grammar-constrained decoding, not prompting alone, is what makes JSON output reliable
- ✓Most local serving tools support some form of schema-constrained generation
- ✓Constrained decoding guarantees valid syntax but does not guarantee semantically correct field values
- ✓Prompting alone is acceptable for low-stakes, human-reviewed output where an occasional malformed response is tolerable
- ✓Constrained decoding holds only while generation completes and the tool supports your schema features, so validate and keep a retry path in code

Quick Answer
Use grammar-constrained decoding rather than relying on prompting alone. Local serving tools that support grammar or JSON-schema-constrained generation restrict which tokens the model is allowed to generate at each step, so the output is guaranteed to be valid JSON matching your schema. This holds only when the tool supports the schema features you use and generation is not cut off at the token limit.
- ▸Prompting alone ("respond only in JSON") reduces but does not eliminate malformed output.
- ▸Grammar-constrained decoding restricts token choices to only valid JSON at each generation step.
- ▸Most local serving tools support some form of JSON-schema-constrained generation, so any capable model can produce reliable output when paired with it.
- ▸Constrained decoding enforces valid syntax only while the tool supports the schema features you use and generation is not cut off at the token limit.
Best Approach: Constrained Decoding
Use grammar- or schema-constrained decoding whenever downstream code will automatically parse the model's output. This approach restricts the model's token choices at every generation step to only those that keep the output syntactically valid against your target schema — it does not rely on the model choosing to follow instructions correctly, so it guarantees well-formed syntax regardless of model size or prompt phrasing. This holds only when the tool supports the schema features you use and generation is not cut off at the token limit.
Best for production pipelines: schema-constrained decoding through your serving tool's native grammar support, paired with a downstream validation step that checks semantic correctness (not just syntax) before the data is used. Best for quick scripts or prototyping: prompting alone with a clear JSON example in the prompt, accepting the occasional retry when output is malformed.
Why Constrained Decoding Matters
Prompting a model to respond only in JSON reduces the rate of malformed output but does not eliminate it, since the model can still generate an invalid token at any step — a stray comma, an unescaped quote, or a truncated closing brace all break downstream parsing. Grammar-constrained decoding solves this at the source by restricting the set of tokens the model is allowed to sample from at each step to only those that keep the output valid against a JSON grammar or schema, which guarantees well-formed output regardless of model size. This holds only when the tool supports the schema features you use and generation is not cut off at the token limit.
Constrained decoding guarantees syntactic validity, not semantic correctness — the model can still put a wrong value in a correctly formatted field. Always pair schema-constrained generation with a downstream check that validates the actual field values make sense for your use case, not just that the JSON parses.
When Prompting Alone Is Enough
Skip constrained decoding when a human reviews every output before it is used, since an occasional malformed response is caught and can simply be regenerated. Skip it for quick prototyping when your serving stack does not yet support grammar constraints and adding that infrastructure isn't worth the setup time for a throwaway script.
Switch to constrained decoding if malformed-output rates from prompting alone are causing pipeline failures, or if the output feeds directly into an automated system with no human in the loop to catch parsing errors.
How to Turn On Constrained Decoding in Ollama, llama.cpp and LM Studio
All three common local runtimes expose constrained decoding through a JSON-schema parameter, so you rarely need to write a grammar by hand. Pass the schema in the request, still describe the expected fields in the prompt, and validate the parsed result in your own code.
- ▸Ollama: set the `format` field of the API request to a JSON schema (or `"json"` for generic JSON). The Ollama docs also recommend passing the schema as text in the prompt and lowering the temperature, for example to 0. Cloud-hosted models were listed as not supporting structured outputs at the last refresh, so check the docs if you use them.
- ▸llama.cpp: use `-j` with a schema on the command line (or `--grammar` / `--grammar-file` for a hand-written GBNF grammar); `llama-server` accepts `json_schema` on completion endpoints and `response_format` on `/chat/completions`. The schema only constrains output and is not added to the prompt, so describe the structure in the prompt too. Only a subset of JSON Schema is supported and unsupported features can be skipped silently, so inspect the generated grammar.
- ▸LM Studio: send `response_format` with `type` set to `json_schema` and a `json_schema` object (with `name` and `schema`) to the OpenAI-compatible `/v1/chat/completions` endpoint. GGUF models use llama.cpp grammar-based sampling and MLX models use Outlines. LM Studio notes that not all models are capable of structured output, particularly models below 7B parameters.
curl http://localhost:11434/api/chat -d '{
"model": "<model-name>",
"messages": [{ "role": "user", "content": "Describe Canada as JSON." }],
"stream": false,
"format": {
"type": "object",
"properties": {
"name": { "type": "string" },
"capital": { "type": "string" },
"languages": { "type": "array", "items": { "type": "string" } }
},
"required": ["name", "capital", "languages"]
},
"options": { "temperature": 0 }
}'Where Constrained Decoding Still Fails
Constrained decoding enforces structure, not completeness or correctness. Three practical limits: (1) A response cut off by a token limit can still end mid-structure, so set a generous output limit and check that the reply parses completely. (2) Tools support only part of JSON Schema — llama.cpp lists gaps such as nested `$ref`, `patternProperties` and bounds on non-integer numbers, and skips unsupported features silently — so test your schema and simplify it if needed. (3) Small models can still fill fields with poor values, and LM Studio warns that models below 7B parameters may not handle structured output well, so validate values in code and keep a retry path.
Sources
- ▸Ollama: Structured outputs — the `format` field, schema examples, tips
- ▸llama.cpp: Grammars and JSON schemas — GBNF, `-j`, supported JSON Schema subset and known gaps
- ▸LM Studio: Structured output — `response_format`, GGUF and MLX engines, model caveats