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

Open
opened 2026-04-13 00:24:35 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/ollama/ollama/pull/10747

State: open
Merged: No


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.

**Original Pull Request:** https://github.com/ollama/ollama/pull/10747 **State:** open **Merged:** No --- 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.
GiteaMirror added the pull-request label 2026-04-13 00:24:35 -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#13350