[PR #3782] [MERGED] Critical fix from llama.cpp JSON grammar to forbid un-escaped escape characters in JSON strings #11275

Closed
opened 2026-04-12 23:26:22 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/3782
Author: @hughescr
Created: 4/20/2024
Status: Merged
Merged: 6/9/2024
Merged by: @jmorganca

Base: mainHead: bugfix/disallow-unescaped-control-characters-in-json-strings


📝 Commits (1)

  • e6c00a9 Critical fix from llama.cpp JSON grammar to forbid un-escaped escape characters inside strings, which breaks parsing.

📊 Changes

1 file changed (+1 additions, -1 deletions)

View changed files

📝 llm/server.go (+1 -1)

📄 Description

JSON generation is broken, as models can insert control characters inside strings, which violates JSON. For example, with the current JSON grammar, models could generate:

{ "key": "value
broken" }

This is incorrect, and if a linebreak is wanted in the middle of the string there, it should be:

{ "key": "value\nbroken" }

The former breaks at least the nodejs JSON parser, and likely many many others, since it's not compliant JSON.

This PR injects the grammar directly from llama.cpp upstream, which prohibits \x00 through \x1f inside JSON strings, fixing the problem.


🔄 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/3782 **Author:** [@hughescr](https://github.com/hughescr) **Created:** 4/20/2024 **Status:** ✅ Merged **Merged:** 6/9/2024 **Merged by:** [@jmorganca](https://github.com/jmorganca) **Base:** `main` ← **Head:** `bugfix/disallow-unescaped-control-characters-in-json-strings` --- ### 📝 Commits (1) - [`e6c00a9`](https://github.com/ollama/ollama/commit/e6c00a9f43c2dd1f1b7698f35d8c0a70faf0cd29) Critical fix from llama.cpp JSON grammar to forbid un-escaped escape characters inside strings, which breaks parsing. ### 📊 Changes **1 file changed** (+1 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `llm/server.go` (+1 -1) </details> ### 📄 Description JSON generation is broken, as models can insert control characters inside strings, which violates JSON. For example, with the current JSON grammar, models could generate: ``` { "key": "value broken" } ``` This is incorrect, and if a linebreak is wanted in the middle of the string there, it should be: ``` { "key": "value\nbroken" } ``` The former breaks at least the nodejs JSON parser, and likely many many others, since it's not compliant JSON. This PR injects the grammar directly from llama.cpp upstream, which prohibits `\x00` through `\x1f` inside JSON strings, fixing the problem. --- <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-12 23:26:22 -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#11275