Repository navigation
Conversation
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.
|
Hi @oalterg, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
Contributor
Author
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #26930
Problem
_visit_patternrequires apatternto carry both^and$, and errors otherwise. JSON Schema definespatternas 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/completionsrequest, 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.pyas 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:
Full
test-json-schema-to-grammarsuite 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 yieldsroot ::= "\"" ("\S") "\""on master. That pre-existing limitation is untouched here; this only makes unanchored patterns behave consistently with anchored ones instead of erroring.Requirements