Skip to content

json-schema-to-grammar : accept unanchored patterns - #26931

Closed
oalterg wants to merge 2 commits into
ggml-org:masterfrom
oalterg:fix/unanchored-json-schema-pattern
Closed

oalterg wants to merge 2 commits into
ggml-org:masterfrom
oalterg:fix/unanchored-json-schema-pattern

Conversation

@oalterg

@oalterg oalterg commented Aug 11, 2026

Copy link
Copy Markdown

Fixes #26930

Problem

_visit_pattern requires a pattern to carry both ^ and $, and errors otherwise. JSON Schema defines pattern as an unanchored partial match — the regex only has to match somewhere in the string, and anchors are optional (§6.3.3). So a valid schema like {"type": "string", "pattern": "\\S"} cannot be converted.

It bites hardest in tool calling, where the schema is not the caller's to fix: one tool with an unanchored pattern 400s the whole /v1/chat/completions request, killing every request that offers that tool rather than only the calls that use it.

Change

Synthesise the missing anchors with .* instead of rejecting. A pattern that already carries both anchors is stripped exactly as before, so existing behaviour and emitted grammars are unchanged.

Applied to examples/json_schema_to_grammar.py as well, so the two implementations stay in lockstep and the parity test keeps passing.

Tests

Three cases added — unanchored, start-anchored-only, end-anchored-only:

"pattern": "abc"   ->  root ::= "\"" (dot* "abc" dot*) "\""
"pattern": "^abc"  ->  root ::= "\"" ("abc" dot*) "\""
"pattern": "abc$"  ->  root ::= "\"" (dot* "abc") "\""

Full test-json-schema-to-grammar suite passes, for both the C++ and Python implementations (verified with Python on PATH so the parity pass actually runs, not skipped).

Also checked against the real-world schema from the linked issue: it converts cleanly with this patch and fails on master.

Scope

Regex class shorthands (\S, \d, \w) are emitted as literals by the converter today — ^\S$ already yields root ::= "\"" ("\S") "\"" on master. That pre-existing limitation is untouched here; this only makes unanchored patterns behave consistently with anchored ones instead of erroring.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: yes — written with AI assistance (Claude). The change was compiled and the full test suite run against both implementations on real hardware rather than assumed; the human author is responsible for all submitted changes.

JSON Schema defines "pattern" as an unanchored partial match: the regex only
has to match somewhere inside the string, and "^"/"$" are optional. The
converter required both anchors and rejected anything else with

  Pattern must start with '^' and end with '$'

so a valid schema such as {"type": "string", "pattern": "\\S"} could not be
converted. In practice this surfaces through tool calling: one tool whose
parameter schema uses an unanchored pattern makes the whole /v1/chat/completions
request fail with a 400, taking down every request that offers that tool rather
than only the calls that use it.

Synthesise the missing anchors with ".*" instead of rejecting. A pattern that
already carries both anchors is stripped exactly as before, so existing
behaviour and output grammars are unchanged.

Applied to the Python implementation as well, so the two stay in lockstep and
the parity test keeps passing.

Tests: three cases covering unanchored, start-anchored-only and
end-anchored-only patterns. Full suite passes for both C++ and Python.
@oalterg
oalterg requested review from a team and ggerganov as code owners August 11, 2026 21:28
@github-actions github-actions Bot added testing Everything test related examples labels Aug 11, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

Hi @oalterg, thanks for your contribution!

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

  • 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.

@aldehir

aldehir commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

#26915 (comment)

@oalterg

oalterg commented Aug 12, 2026

Copy link
Copy Markdown
Author

Hi @oalterg, thanks for your contribution!

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

  • 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.

Thanks for this outdated rule, will consider giving it a human touch :D

Assisted-by: Cursor Grok 4.6

Co-authored-by: oalterg <[email protected]>
@oalterg oalterg closed this Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

json-schema-to-grammar: valid unanchored pattern is rejected, failing the entire tool-calling request

3 participants