[PR #830] [CLOSED] Add basic JSON Schema support to the API (converts to GBNF grammar) #41599

Closed
opened 2026-04-24 21:27:09 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/830
Author: @Lwrless
Created: 10/18/2023
Status: Closed

Base: mainHead: lwrless/json-schema


📝 Commits (10+)

  • d5335df Add support for GBNF grammar definitions
  • 6dfd296 Add an example GBNF JSON model
  • 6569690 add json schema to grammar converter for llama
  • 9c906fb fix array grammar conversion and add test, format code
  • d9129a4 add proper errors for grammar conversion
  • 3d09991 Merge branch 'main' into lwrless/json-schema
  • 320b99b fix setting grammar
  • 506c1d0 format indents
  • 5157f1b Merge branch 'main' into lwrless/json-schema
  • e9e9cab Merge branch 'jmorganca:main' into lwrless/json-schema

📊 Changes

7 files changed (+450 additions, -29 deletions)

View changed files

📝 api/types.go (+2 -0)
📝 docs/modelfile.md (+1 -0)
examples/json/Modelfile (+33 -0)
format/grammar.go (+260 -0)
format/grammar_test.go (+139 -0)
📝 llm/dyn_ext_server.go (+15 -1)
📝 llm/llama.go (+0 -28)

📄 Description

This PR is based on PR https://github.com/jmorganca/ollama/pull/565 which adds GBNF grammar support.

JSON Schema is now available as an option (schema) in /api/generate API. If provided, it will be converted into GBNF grammar and added into the predict request for llama.cpp. Also, make sure to set the format to json in order for this to function.

Here's a demo:

Request

{
  "model": "llama2",
  "prompt": "Generate a mock user.",
  "format": "json",
  "options": {
      "schema": "{\"type\":\"object\",\"properties\":{\"firstname\":{\"type\":\"string\"},\"lastname\":{\"type\":\"string\"},\"age\":{\"type\":\"integer\"},\"address\":{\"type\":\"string\"}},\"email\":{\"type\":\"string\"},\"isMember\":{\"type\":\"boolean\"}}"
  }
}

Response (extracted)

{"firstname": "John", "lastname": "Doe", "age": 32, "address": "123 Main St, Anytown USA 12345"}

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/830 **Author:** [@Lwrless](https://github.com/Lwrless) **Created:** 10/18/2023 **Status:** ❌ Closed **Base:** `main` ← **Head:** `lwrless/json-schema` --- ### 📝 Commits (10+) - [`d5335df`](https://github.com/ollama/ollama/commit/d5335df9c25ff755408de58337b2ce631f70278a) Add support for GBNF grammar definitions - [`6dfd296`](https://github.com/ollama/ollama/commit/6dfd296e8b2aa84b00daf9c1e3867df1af8bee8d) Add an example GBNF JSON model - [`6569690`](https://github.com/ollama/ollama/commit/656969016f9f6355b1d30bf5e650135e75c5ccbd) add json schema to grammar converter for llama - [`9c906fb`](https://github.com/ollama/ollama/commit/9c906fbcb60d669e03482f3b0a31fa5bc0e2d578) fix array grammar conversion and add test, format code - [`d9129a4`](https://github.com/ollama/ollama/commit/d9129a4af22c6abe652fea81966b9a63127a58b9) add proper errors for grammar conversion - [`3d09991`](https://github.com/ollama/ollama/commit/3d09991cd3f36a7f7257263fd86db42b7e8a3703) Merge branch 'main' into lwrless/json-schema - [`320b99b`](https://github.com/ollama/ollama/commit/320b99bb03d5fcc85d414fa8692784faceebe012) fix setting grammar - [`506c1d0`](https://github.com/ollama/ollama/commit/506c1d05b57a0fbc57a19a401823139ccb633f74) format indents - [`5157f1b`](https://github.com/ollama/ollama/commit/5157f1b7629fbcc574084d65bcb2739128ba7df6) Merge branch 'main' into lwrless/json-schema - [`e9e9cab`](https://github.com/ollama/ollama/commit/e9e9cabde1efc3395c70290f83fa57525ad54593) Merge branch 'jmorganca:main' into lwrless/json-schema ### 📊 Changes **7 files changed** (+450 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `api/types.go` (+2 -0) 📝 `docs/modelfile.md` (+1 -0) ➕ `examples/json/Modelfile` (+33 -0) ➕ `format/grammar.go` (+260 -0) ➕ `format/grammar_test.go` (+139 -0) 📝 `llm/dyn_ext_server.go` (+15 -1) 📝 `llm/llama.go` (+0 -28) </details> ### 📄 Description This PR is based on PR https://github.com/jmorganca/ollama/pull/565 which adds GBNF grammar support. JSON Schema is now available as an option (`schema`) in `/api/generate` API. If provided, it will be converted into GBNF grammar and added into the predict request for llama.cpp. Also, make sure to set the `format` to `json` in order for this to function. Here's a demo: #### Request ```json { "model": "llama2", "prompt": "Generate a mock user.", "format": "json", "options": { "schema": "{\"type\":\"object\",\"properties\":{\"firstname\":{\"type\":\"string\"},\"lastname\":{\"type\":\"string\"},\"age\":{\"type\":\"integer\"},\"address\":{\"type\":\"string\"}},\"email\":{\"type\":\"string\"},\"isMember\":{\"type\":\"boolean\"}}" } } ``` #### Response (extracted) ```json {"firstname": "John", "lastname": "Doe", "age": 32, "address": "123 Main St, Anytown USA 12345"} ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-24 21:27:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#41599