Skip to content

json-schema : accept escaped hyphen in regex patterns - #29127

Merged
pwilkin merged 1 commit into
ggml-org:masterfrom
ChihebBENCHEIKH1:json-schema-escaped-hyphen
Sep 19, 2026
Merged

pwilkin merged 1 commit into
ggml-org:masterfrom
ChihebBENCHEIKH1:json-schema-escaped-hyphen

Conversation

@ChihebBENCHEIKH1

Copy link
Copy Markdown
Contributor

The bug
Three places disagree on the GBNF escape -:

  • src/llama-grammar.cpp:175 parse_char() — accepts -
  • common/peg-parser.cpp:1364 gbnf_escape_char_class() — emits -
  • common/json-schema-to-grammar.cpp:324 gbnf_escape_length() — rejected - (the bug)

How it drifted

Effect
A JSON Schema pattern containing - (e.g. ^[A-Za-z0-9_-]+$, ISO dates, phone/uuid shapes) hits unsupported_pattern, so the whole pattern silently falls back to the unconstrained string rule. It's a warning, not an error — easy to miss.

Repro: schema {"type":"string","pattern":"^[a-z\-]+$"} →
pattern ^[a-z-]+$ is not supported (unsupported escape in character class: -), accepting any string

The fix
One case '-': label in gbnf_escape_length(), plus a comment pointing at parse_char(). Two regression tests added. One pre-existing test that used ^[a-z-]+$ as "some unsupported pattern" was switched to a lookahead ^(?=a)a$ (no GBNF equivalent, so it can't silently stop testing anything if this bug class resurfaces).

Verification
Without the fix, with the new tests: exit 134 (assert abort). With the fix: exit 0. test-json-schema-to-grammar, test-grammar-parser, test-grammar-integration, test-chat all pass.

@ChihebBENCHEIKH1
ChihebBENCHEIKH1 requested review from a team and ggerganov as code owners September 19, 2026 11:02
@github-actions github-actions Bot added the testing Everything test related label Sep 19, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Sep 19, 2026

Copy link
Copy Markdown

Hi @ChihebBENCHEIKH1, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Sep 19, 2026
@github-actions
github-actions Bot marked this pull request as draft September 19, 2026 11:08
@github-actions github-actions Bot removed the draft PR will be changed to draft by github-actions bot label Sep 19, 2026
@pwilkin

pwilkin commented Sep 19, 2026

Copy link
Copy Markdown
Member

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Automated code review

Reviewed the diff (gbnf_escape_length whitelist + two regression tests + one test swap). I verified the claim chain rather than taking the description at face value:

  • parse_char() in src/llama-grammar.cpp:162 accepts \- (case '-' at line 173), and this same function is used for both quoted literals and [...] char ranges in the grammar parser (src/llama-grammar.cpp:539,557), so \- copied verbatim into either context parses correctly to a literal '-'.
  • gbnf_escape_char_class() in common/peg-parser.cpp:1355 already emits \-, so json-schema-to-grammar was indeed the odd one out rejecting it, silently degrading the whole pattern to the unconstrained string rule via _visit_pattern's unsupported_pattern handler.
  • The new whitelist set (t r n \ " [ ] - plus x/u/U hex) is now exactly in sync with parse_char's accepted escapes - no case is accepted by one and rejected by the other.

Correctness checks on edge cases around the verbatim copy into char classes:

  • [a\-z]: regex semantics are literal a, -, z (not a range). The emitted GBNF [a\-z] parses as three separate chars because the range check in the parser (pos[0] == '-' && pos[1] != ']') only fires on a raw '-', which \- consumes first. Correct.
  • [\--z] (escaped hyphen as range start) and [\-] (standalone) also round-trip with correct semantics.

The two new tests' expected outputs match what the code actually produces ([a-z\-]+ copied verbatim; a\-b in the literal branch since '-' is not in ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS, so it takes the gbnf_escape_length path). The swap of the pre-existing "unsupported pattern" test from ^[a-z\\-]+$ to ^(?=a)a$ is the right call: the old fixture would have silently become a success case after this fix, while the lookahead still exercises the degrade-to-string path (unsupported group syntax).

Findings:

No blocking or correctness issues.

  • Nit: the comment // keep in sync with parse_char() in src/llama-grammar.cpp is good and concise - consider dropping the file path, since parse_char() is unambiguous in this codebase and the path may drift if the function moves.

Nothing else to flag: minimal, single-purpose fix that reuses the existing whitelist mechanism, tests added to the existing suite rather than a new file, comment style and ASCII conventions respected.

This review was generated automatically by pi coding agent using zai-org/GLM-5.3. It may contain mistakes. Maintainers make the final call.

@pwilkin
pwilkin marked this pull request as ready for review September 19, 2026 11:29
@pwilkin pwilkin added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 19, 2026
@pwilkin
pwilkin merged commit eb1e1f4 into ggml-org:master Sep 19, 2026
25 of 28 checks passed
LadislavSopko pushed a commit to 0ics-srls/llama.cpp that referenced this pull request Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants