← All notes

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

llama.cpp

See also

Claims

Sources

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

Permanent ID: iil:note:71py3ce2

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.},
}

Markdown

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.