Local structured output: Ollama format and llama.cpp GBNF
Answer: Ollama can return JSON with format='json', or you can use Pydantic models and pass model_json_schema() to format, then validate the response.
In llama.cpp, GBNF grammars constrain model outputs, and llama.cpp can also convert JSON schemas to grammars.
Ollama tips
- Define schemas with Pydantic (Python) or Zod (JavaScript) so they can be reused for validation, then validate with
model_validate_json. - Lower the temperature (for example to
0) for more deterministic completions.
llama.cpp
- GBNF (GGML BNF) is a format for defining formal grammars.
llama-servertakes ajson_schemabody field on completion endpoints, or a schema insideresponse_formaton/chat/completions.llama-clitakes--json/-j.- The JSON schema is only used to constrain the output and is not injected into the prompt, so describe the expected structure in your prompt.
See also
- ao-tools-ollama-tool-calling
- ao-structured-vllm-structured-outputs
- ao-structured-gemini-json-schema
- ao-structured-json-schema-arrays-and-strings
Claims
- id: C1 claim: Ollama accepts format json. source: S1 evidence: "format='json'"
- id: C2 claim: GBNF constrains llama.cpp outputs. source: S2 evidence: "GBNF (GGML BNF) is a format for defining"
- id: C3 claim: The schema is not injected into the prompt. source: S2 evidence: "is not injected into the prompt"
Sources
- [S1] Structured outputs (Ollama docs). ollama/ollama (GitHub). https://github.com/ollama/ollama/blob/main/docs/capabilities/structured-outputs.mdx . License: MIT. Accessed 2026-11-16.
- [S2] GBNF guide (llama.cpp grammars README). ggml-org/llama.cpp (GitHub). https://github.com/ggml-org/llama.cpp/blob/master/grammars/README.md . License: MIT. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: ollama/ollama (GitHub), Structured outputs (Ollama docs), MIT (https://raw.githubusercontent.com/ollama/ollama/main/LICENSE). Copyright (c) Ollama; ggml-org/llama.cpp (GitHub), GBNF guide (llama.cpp grammars README), MIT (https://raw.githubusercontent.com/ggml-org/llama.cpp/master/LICENSE). Copyright (c) 2023-2026 The ggml authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: ollama/ollama (GitHub), Structured outputs (Ollama docs), MIT (https://raw.githubusercontent.com/ollama/ollama/main/LICENSE). Copyright (c) Ollama; ggml-org/llama.cpp (GitHub), GBNF guide (llama.cpp grammars README), MIT (https://raw.githubusercontent.com/ggml-org/llama.cpp/master/LICENSE). Copyright (c) 2023-2026 The ggml authors.
Cite this
iinventedlight. 2026. "Local structured output: Ollama format and llama.cpp GBNF." iinventedlight Library, iil:note:71py3ce2, version of 2026-11-16. https://iinventedlight.com/library/notes/ao-structured-ollama-and-llamacpp-grammars/. License: CC-BY-4.0.
@misc{iil_note_71py3ce2,
author = {{iinventedlight}},
title = {{Local structured output: Ollama format and llama.cpp GBNF}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/ao-structured-ollama-and-llamacpp-grammars/},
note = {Permanent ID iil:note:71py3ce2. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/ao-structured-ollama-and-llamacpp-grammars.md.