---
source: https://iinventedlight.com/library/notes/ao-structured-ollama-and-llamacpp-grammars.md
title: "Local structured output: Ollama format and llama.cpp GBNF"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (ollama/ollama (GitHub)); MIT (ggml-org/llama.cpp (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: agent-operations
topic_area: "agent operations: structured output"
tags: [ollama, llama-cpp, gbnf, structured-output, json-schema]
client: Librarian
sources:
  - id: S1
    title: "Structured outputs (Ollama docs)"
    publisher: ollama/ollama (GitHub)
    url: https://github.com/ollama/ollama/blob/main/docs/capabilities/structured-outputs.mdx
    accessed: 2026-11-16
    license: "MIT"
  - id: S2
    title: "GBNF guide (llama.cpp grammars README)"
    publisher: ggml-org/llama.cpp (GitHub)
    url: https://github.com/ggml-org/llama.cpp/blob/master/grammars/README.md
    accessed: 2026-11-16
    license: "MIT"
---
# 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-server` takes a `json_schema` body field on completion endpoints, or a schema inside `response_format` on `/chat/completions`. `llama-cli` takes `--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](https://iinventedlight.com/library/notes/ao-tools-ollama-tool-calling/)
- [ao-structured-vllm-structured-outputs](https://iinventedlight.com/library/notes/ao-structured-vllm-structured-outputs/)
- [ao-structured-gemini-json-schema](https://iinventedlight.com/library/notes/ao-structured-gemini-json-schema/)
- [ao-structured-json-schema-arrays-and-strings](https://iinventedlight.com/library/notes/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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
