[PR #3785] [CLOSED] Improved json grammar #42526

Closed
opened 2026-04-24 22:16:35 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/3785
Author: @hughescr
Created: 4/20/2024
Status: Closed

Base: mainHead: feature/better-json-grammar


📝 Commits (4)

  • e6c00a9 Critical fix from llama.cpp JSON grammar to forbid un-escaped escape characters inside strings, which breaks parsing.
  • 6523267 Allow any kind of JSON including arrays or straight literals, not just objects at the top level.
  • 604ac83 Allow whitespace within objects and arrays, but remove trailing possibly infinite whitespace
  • 461ee34 Remove all whitespace for generation. Every token is more CO2, less speed, and more $$$ in NVidia's pocket.

📊 Changes

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

View changed files

📝 llm/server.go (+12 -15)

📄 Description

This PR includes #3782 #3783 #3784 but also completely removes all whitespace generation when applying format=json.

Whitespace is completely unnecessary in JSON - it is optionally acceptable, but can be ignored if we're generating and not parsing. Since every token costs time, money, and CO2, removing all whitespace is highly recommended.

It will also fix a buglet, where models could get "stuck" generating infinite whitespace, since there is no limit in the definition of ws for how long it can be.


🔄 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/3785 **Author:** [@hughescr](https://github.com/hughescr) **Created:** 4/20/2024 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feature/better-json-grammar` --- ### 📝 Commits (4) - [`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. - [`6523267`](https://github.com/ollama/ollama/commit/65232672ec953bae7a8dd10c3416678b456c13eb) Allow any kind of JSON including arrays or straight literals, not just objects at the top level. - [`604ac83`](https://github.com/ollama/ollama/commit/604ac833617164f7d402ff17e19afa9b19b380b3) Allow whitespace within objects and arrays, but remove trailing possibly infinite whitespace - [`461ee34`](https://github.com/ollama/ollama/commit/461ee34a558551bdd2326b967eb00dc0ccc8a74a) Remove all whitespace for generation. Every token is more CO2, less speed, and more $$$ in NVidia's pocket. ### 📊 Changes **1 file changed** (+12 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `llm/server.go` (+12 -15) </details> ### 📄 Description This PR includes #3782 #3783 #3784 but also completely removes *all* whitespace generation when applying `format=json`. Whitespace is completely unnecessary in JSON - it is optionally acceptable, but can be ignored if we're *generating* and not parsing. Since every token costs time, money, and CO2, removing all whitespace is highly recommended. It will also fix a buglet, where models could get "stuck" generating infinite whitespace, since there is no limit in the definition of `ws` for how long it can be. --- <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 22:16: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#42526