Repository navigation
Conversation
A JSON-schema pattern that uses lookbehind, a backreference, \b, \p{...},
a group flag other than (?i), an atomic group or a{,n} made the whole
schema fail to compile, so one such pattern in one tool broke a request
with many tools (mlc-ai#1001). The XML tool-calling converter also failed on
lookaheads, which it compiles through the CFG regex parser, while the JSON
converter's FSM engine already ignores them with a warning.
Add JSONSchemaConverter::PatternExpression. When the regex engine on the
converter's path cannot parse a pattern (the parser reports it through
RegexConverter::RaiseError), it logs a warning and the string compiles
without the pattern, still bounded by minLength/maxLength. A relaxed
grammar only accepts more strings, as for the skipped lookaheads and for
an unsupported format or multipleOf. Other errors still propagate.
adimalkar
requested review from
DarkSharpness,
Seven-Streams and
Ubospica
as code owners
October 21, 2026 08:38
Contributor
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
1 task done
This branch has not been deployed
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 #1001
What
A JSON-schema
patternthat the regex engine cannot parse made the whole schema fail to compile, so one such pattern in one tool took down a request with many tools. Onmain(614ab01):qwen_xml)(?=…),(?!…)lookaheadLookahead is not supported yet.(?<=…),(?<!…)lookbehind\1backreference\bword boundary\p{L}(?s),(?>…)a{,3}The issue's own example,
^(?!\.\.?(?:\/|$))[^\s]+$from the Claude Code tool set, still breaks Qwen-style XML tool calling onmain.With this PR, each of these compiles and logs
and the string is generated as if the pattern were absent, still bounded by
minLength/maxLength. This is request 2 of the issue. It's the same policy the FSM engine already applies to lookaheads, and xgrammar already applies to an unsupportedmultipleOf(and, with #969, an unknownformat): a relaxed grammar only accepts more strings, and the caller can validate after decoding. Details and the full table are in #1001 (comment).How
JSONSchemaConverter::PatternExpressionwrapsRegexExpressionfor a string schema'spattern, in bothJSONSchemaConverter::GenerateStringandXMLToolCallingConverter::GenerateString. If the parser reports a syntax it cannot handle, it returnsstd::nulloptafter logging, and the caller falls through to the plain or length-bounded branch.Why at the point of use and not in
ParseString: the two converters compile patterns through different engines. The JSON path tries the FSM engine first, which skips lookaheads; the XML path forces the CFG parser, which rejects them. So "unsupported" depends on the converter. Every syntax the parser can't handle comes out ofRegexConverter::RaiseError("Regex parsing error …"). The FSM engine returns an error instead, andRegexExpressionthen falls back to that parser. So catching exactly that error means "the engine on this path can't parse it", and nothing has been added to the grammar when it throws. Other failures still propagate (a pattern containing a null character still raises, and is tested).This does not overlap with #986. That PR rejects patterns that parse but whose JSON-safe language is empty; those errors don't come from
RaiseError, so they still fail as intended. Key regexes inpatternPropertiesand the EBNFRegex(...)/ Lark paths are unchanged.One thing to flag: when a pattern with
minLength/maxLengthis ignored, the existing #896 warning fromParseString("pattern combined with minLength/maxLength is not supported; ignoring minLength/maxLength") still fires, although the bounds are then enforced.ParseStringcan't know which engine will compile the pattern, so I left #896's warning alone and made the new warning say that the bounds still apply. Happy to rework that if you prefer.Tests
In
tests/python/test_json_schema_converter.py, next to the #896 tests and using the same_compile_and_capturehelper:json/qwen_xml: the schema compiles and warnsqwen_xmlminLength/maxLength24 of the 25 new tests fail on
mainand pass with the change; the null-character one guards existing behaviour.Locally,
tests/pythongives 4661 passed. The only 26 failures are intest_builtin_structural_tag_alignment.pyand come from the optionalcohere_melodypackage, which isn't installed here.pre-commitandruff checkpass. The C++ tests aren't built by default (XGRAMMAR_BUILD_CXX_TESTS=OFF), and none oftests/cpp/test_json*.cccovers pattern errors.