From fea0e96bed5ef66f2a1ffccaf26e9160f2d6d589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=80=80=E9=91=AB=28Huang=20Yaoxin=29?= <174019195+YaoxinHuang@users.noreply.github.com> Date: Mon, 21 Sep 2026 11:25:31 +0800 Subject: [PATCH 1/4] fix(grammar): decode JSON Pointer escapes in schema references --- llama_cpp/llama_grammar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/llama_cpp/llama_grammar.py b/llama_cpp/llama_grammar.py index ba34dda831..93f7b7d8ab 100644 --- a/llama_cpp/llama_grammar.py +++ b/llama_cpp/llama_grammar.py @@ -475,6 +475,7 @@ def visit(n: dict): raise ValueError(f"Unsupported ref {ref}") for sel in ref.split("#")[-1].split("/")[1:]: + sel = sel.replace("~1", "/").replace("~0", "~") assert target is not None and sel in target, ( f"Error resolving ref {ref}: {sel} not in {target}" ) From c94dfa3dbd8286e0e09b780c3031401c2dfd2279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=80=80=E9=91=AB=28Huang=20Yaoxin=29?= <174019195+YaoxinHuang@users.noreply.github.com> Date: Mon, 21 Sep 2026 11:26:38 +0800 Subject: [PATCH 2/4] test(grammar): cover escaped JSON Pointer reference tokens Added parameterized test for grammar JSON pointer escapes. --- tests/test_llama_grammar.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_llama_grammar.py b/tests/test_llama_grammar.py index 34ef2874df..65859dfa81 100644 --- a/tests/test_llama_grammar.py +++ b/tests/test_llama_grammar.py @@ -1,5 +1,6 @@ import llama_cpp import json +import pytest tree = """ leaf ::= "." @@ -76,3 +77,23 @@ def test_grammar_anyof(): grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(sch)) # assert grammar.grammar is not None + + +@pytest.mark.parametrize( + ("definition_name", "reference_token"), + [ + ("a/b", "a~1b"), + ("a~b", "a~0b"), + ("a~1b", "a~01b"), + ("a~/b", "a~0~1b"), + ], +) +def test_grammar_json_pointer_escapes(definition_name, reference_token): + schema = { + "$defs": {definition_name: {"const": 42}}, + "$ref": f"#/$defs/{reference_token}", + } + + grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(schema)) + + assert '::= "42"' in grammar._grammar From a4cb366db0fdeb87d82d79090d2f464ccf35ef6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=80=80=E9=91=AB=28Huang=20Yaoxin=29?= <174019195+YaoxinHuang@users.noreply.github.com> Date: Mon, 21 Sep 2026 11:27:15 +0800 Subject: [PATCH 3/4] docs: note JSON Pointer escape fix in changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e13a4b5cd..a2ab384733 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix(grammar): decode JSON Pointer escapes in schema references by @YaoxinHuang - feat: update llama.cpp to ggml-org/llama.cpp@v0.4.0 ## [0.3.35] From 23f67e63cdf71844150120dae142c3d35aefeb18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=80=80=E9=91=AB=28Huang=20Yaoxin=29?= <174019195+YaoxinHuang@users.noreply.github.com> Date: Mon, 21 Sep 2026 11:28:58 +0800 Subject: [PATCH 4/4] docs: link JSON Pointer changelog entry to PR #2368 Record the pull request number required by the contribution guidelines. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ab384733..1fd6132292 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- fix(grammar): decode JSON Pointer escapes in schema references by @YaoxinHuang +- fix(grammar): decode JSON Pointer escapes in schema references by @YaoxinHuang in #2368 - feat: update llama.cpp to ggml-org/llama.cpp@v0.4.0 ## [0.3.35]