[PR #10747] Fix SchemaToGrammar having an arbitrary buffer limit breaking some JSON schemas #23890

Open
opened 2026-04-19 17:16:25 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/10747
Author: @SchoofsKelvin
Created: 5/16/2025
Status: 🔄 Open

Base: mainHead: feature/json-schema


📝 Commits (2)

  • 7acad7e llama: fix SchemaToGrammar having an arbitrary buffer limit breaking some JSON schemas
  • d7c486c llama: make TestGrowingSchema in llama_test.go more deterministic

📊 Changes

4 files changed (+74 additions, -19 deletions)

View changed files

📝 llama/llama.go (+10 -8)
📝 llama/llama_test.go (+51 -0)
📝 llama/sampling_ext.cpp (+8 -10)
📝 llama/sampling_ext.h (+5 -1)

📄 Description

Fixes #10646

Problem

I ran into a weird issue when trying to use this (and similar) structured output:

{
    "type": "object",
    "properties": {
        "execSchema": { "type": "object", "additionalProperties": true },
        "execInstructions": { "type": "string" },
        "combineSchema": { "type": "object", "additionalProperties": true },
        "combineInstructions": { "type": "string" },
        "question": { "type": "string" }
    }
}

This logged errors similar to:

parse: error parsing grammar: expecting ')' at

root ::= "{" space  (execSchema-kv execSchema-rest | execInstructions-kv execInstructions-rest | combineSchema-kv combineSchema-rest | combineInstructions-kv combineInstructions-rest | question-kv )? "}" space
<omitted>
object ::= "{" space ( str
llama_grammar_init_impl: failed to parse grammar

After looking a bit around and seeing #10649, I did notice that my serialized JSON schema would be ~280 bytes while the logged grammar seems to be cut off after 280*4 bytes almost spot on.

Fix

I've added a struct to sampling_ext.h containing a char* and length which sampling_ext.cpp returns a heap-allocated instance of, while llama.go uses this (and makes sure to free it) to get a []byte from without needing to preallocate any buffer.

I also added my JSON schema as TestGrowingSchema in llama_test.go which fails before this commit, succeeds after.


🔄 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/10747 **Author:** [@SchoofsKelvin](https://github.com/SchoofsKelvin) **Created:** 5/16/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feature/json-schema` --- ### 📝 Commits (2) - [`7acad7e`](https://github.com/ollama/ollama/commit/7acad7ea1b7be9f95faf7b876fa3f82918e09be7) llama: fix SchemaToGrammar having an arbitrary buffer limit breaking some JSON schemas - [`d7c486c`](https://github.com/ollama/ollama/commit/d7c486cdb8735a59d18b9757f2138c9bed643423) llama: make TestGrowingSchema in llama_test.go more deterministic ### 📊 Changes **4 files changed** (+74 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `llama/llama.go` (+10 -8) 📝 `llama/llama_test.go` (+51 -0) 📝 `llama/sampling_ext.cpp` (+8 -10) 📝 `llama/sampling_ext.h` (+5 -1) </details> ### 📄 Description Fixes #10646 ### Problem I ran into a weird issue when trying to use this (and similar) structured output: ```json { "type": "object", "properties": { "execSchema": { "type": "object", "additionalProperties": true }, "execInstructions": { "type": "string" }, "combineSchema": { "type": "object", "additionalProperties": true }, "combineInstructions": { "type": "string" }, "question": { "type": "string" } } } ``` This logged errors similar to: ``` parse: error parsing grammar: expecting ')' at root ::= "{" space (execSchema-kv execSchema-rest | execInstructions-kv execInstructions-rest | combineSchema-kv combineSchema-rest | combineInstructions-kv combineInstructions-rest | question-kv )? "}" space <omitted> object ::= "{" space ( str llama_grammar_init_impl: failed to parse grammar ``` After looking a bit around and seeing #10649, I did notice that my serialized JSON schema would be ~280 bytes while the logged grammar seems to be cut off after `280*4` bytes almost spot on. ### Fix I've added a struct to `sampling_ext.h` containing a `char*` and length which `sampling_ext.cpp` returns a heap-allocated instance of, while `llama.go` uses this (and makes sure to free it) to get a `[]byte` from without needing to preallocate any buffer. I also added my JSON schema as `TestGrowingSchema` in `llama_test.go` which fails before this commit, succeeds after. --- <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-19 17:16:25 -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#23890