Skip to main content
PromptQuorumBuilt for humans. Structured for AI.

Best Local LLM for Reliable Structured JSON Output?

By · 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
Best Local LLM for Reliable Structured JSON Output?

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.
Technique & Concept ExplainersIntermediate

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

Frequently Asked Questions

Does constrained decoding slow down generation?▾
It adds a small overhead per token to check grammar validity, but the overhead is generally minor compared to the reliability gained. The exact cost depends on the serving tool and grammar complexity.
Does constrained decoding guarantee the JSON values are correct, not just valid syntax?▾
No — constrained decoding only guarantees the output parses as valid JSON matching your schema's structure. It does not guarantee the field values themselves are factually or semantically correct. Always add a separate validation step for value correctness.
Do I need a specific model to use constrained decoding, or does it work with any local model?▾
Constrained decoding is a property of the serving tool, not the model — it works with essentially any local model, since it operates by restricting which tokens are eligible at each step regardless of which model generated the underlying probabilities.
Can I define a nested or complex schema for constrained generation?▾
Yes — most grammar-based constrained decoding implementations support nested objects, arrays, and typed fields, though very deeply nested or highly conditional schemas may need to be simplified for reliable grammar compilation depending on the specific tool.
How do I request JSON-schema output in Ollama?▾
Set the `format` field of the chat or generate request to your JSON schema (or to `"json"` for any valid JSON), also describe the fields in the prompt, and consider a low temperature such as 0. Parse and validate the reply in your own code, for example with a Pydantic or Zod schema as the Ollama docs show.
Does the same schema work in Ollama, llama.cpp and LM Studio?▾
Often, but not always. Each tool converts JSON Schema into its own grammar and supports a subset, and llama.cpp skips unsupported features silently. Keep schemas simple (flat objects, arrays, enums, typed fields), test them on the tool you deploy with, and avoid depending on advanced keywords.
MCP