[GH-ISSUE #2623] JSON without newline grammar #27309

Closed
opened 2026-04-22 04:32:19 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @Xe on GitHub (Feb 21, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/2623

I'd like to have a variant of the JSON grammar that forbids newlines from the output. Sometimes when you activate JSON grammar, the model will spit out an endless series of newlines. It'd be nice to have a version of the JSON grammar that forbids newlines (and probably whitespace outside of strings) to prevent this from happening.

Originally created by @Xe on GitHub (Feb 21, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/2623 I'd like to have a variant of the JSON grammar that forbids newlines from the output. Sometimes when you activate JSON grammar, the model will spit out an endless series of newlines. It'd be nice to have a version of the JSON grammar that forbids newlines (and probably whitespace outside of strings) to prevent this from happening.
GiteaMirror added the feature request label 2026-04-22 04:32:19 -05:00
Author
Owner

@josharian commented on GitHub (Mar 1, 2024):

hello, @Xe! i just hit this too. possible dups: https://github.com/ollama/ollama/issues/2351 and https://github.com/ollama/ollama/issues/2577. I think the suggestion in https://github.com/ollama/ollama/issues/2577 is probably the cleanest fix:

[force the] message to terminate immediately after the closing } of the top-level JSON object

<!-- gh-comment-id:1973307816 --> @josharian commented on GitHub (Mar 1, 2024): hello, @Xe! i just hit this too. possible dups: https://github.com/ollama/ollama/issues/2351 and https://github.com/ollama/ollama/issues/2577. I think the suggestion in https://github.com/ollama/ollama/issues/2577 is probably the cleanest fix: > [force the] message to terminate immediately after the closing } of the top-level JSON object
Author
Owner

@josharian commented on GitHub (Mar 3, 2024):

It looks like this might be as simple as removing ws from the end of:

object ::=
  "{" ws (
            string ":" ws value
    ("," ws string ":" ws value)*
  )? "}" ws

in jsonGrammar. Would need to do some testing. It's interesting that upstream llama.cpp has a json_arr.gbnf that restricts whitespace at the end of a root array, but doesn't have the same for objects.

<!-- gh-comment-id:1974978525 --> @josharian commented on GitHub (Mar 3, 2024): It looks like this might be as simple as removing ` ws` from the end of: ``` object ::= "{" ws ( string ":" ws value ("," ws string ":" ws value)* )? "}" ws ``` in jsonGrammar. Would need to do some testing. It's interesting that upstream llama.cpp has a json_arr.gbnf that restricts whitespace at the end of a root array, but doesn't have the same for objects.
Author
Owner

@josharian commented on GitHub (Mar 3, 2024):

I'd prefer to remove ws from the grammar entirely. Why waste cycles on pretty-printing tokens? That can be using encoding/json afterwards, if we care.

Another, perhaps better, approach to this is to expose a "gbnf grammar" parameter, so folks can mess around with this entirely as they desire.

<!-- gh-comment-id:1974978868 --> @josharian commented on GitHub (Mar 3, 2024): I'd prefer to remove `ws` from the grammar entirely. Why waste cycles on pretty-printing tokens? That can be using encoding/json afterwards, if we care. Another, perhaps better, approach to this is to expose a "gbnf grammar" parameter, so folks can mess around with this entirely as they desire.
Author
Owner

@Xe commented on GitHub (Mar 3, 2024):

I managed to solve it in a somewhat roundabout way. I made a function called Hallucinate that returns a new value T with custom "validation" logic. I think 590a15a0c3 will fix it for my needs.

I'll PR removing the whitespace from the grammar entirely.

<!-- gh-comment-id:1975002994 --> @Xe commented on GitHub (Mar 3, 2024): I managed to solve it in a somewhat roundabout way. I made a function called `Hallucinate` that returns a new value T with custom "validation" logic. I think https://github.com/Xe/x/commit/590a15a0c3658abf13db39bd3c52021a7ec6ba82 will fix it for my needs. I'll PR removing the whitespace from the grammar entirely.
Author
Owner

@josharian commented on GitHub (Mar 3, 2024):

Filed #2895 for BYO GBNF support.

<!-- gh-comment-id:1975202341 --> @josharian commented on GitHub (Mar 3, 2024): Filed #2895 for BYO GBNF support.
Author
Owner

@josharian commented on GitHub (Mar 21, 2024):

@Xe do you still plan to send a PR fixing up the JSON grammar?

While working on other things, I noticed that the JSON grammar can generate invalid json: it doesn't forbid unescaped \t, \n, or \r in its definition of string. Something like this would help:

-    [^"\\] |
+    [^"\\\n\t\r] |

Maybe you could add this to your JSON cleanup PR? (And if not, I can cobble one together.)

<!-- gh-comment-id:2010986107 --> @josharian commented on GitHub (Mar 21, 2024): @Xe do you still plan to send a PR fixing up the JSON grammar? While working on other things, I noticed that the JSON grammar can generate invalid json: it doesn't forbid unescaped \t, \n, or \r in its definition of string. Something like this would help: ```diff - [^"\\] | + [^"\\\n\t\r] | ``` Maybe you could add this to your JSON cleanup PR? (And if not, I can cobble one together.)
Author
Owner

@ParthSareen commented on GitHub (Dec 5, 2024):

Hey! with the rollout of #7900 we're supporting structured outputs - hope that is helpful!

<!-- gh-comment-id:2518854596 --> @ParthSareen commented on GitHub (Dec 5, 2024): Hey! with the rollout of #7900 we're supporting structured outputs - hope that is helpful!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#27309