[V1] Add regex structured output support with xgrammar (#14590)

Signed-off-by: Russell Bryant <rbryant@redhat.com>
This commit is contained in:
Russell Bryant
2025-03-11 23:03:44 +08:00
committed by GitHub
parent 9ab326713f
commit 4bf82d4b90
4 changed files with 25 additions and 19 deletions
+2
View File
@@ -112,6 +112,8 @@ class StructuredOutputManager:
ctx = self.compiler.compile_builtin_json_grammar()
elif request_type == StructuredOutputOptions.GRAMMAR:
ctx = self.compiler.compile_grammar(grammar_spec)
elif request_type == StructuredOutputOptions.REGEX:
ctx = self.compiler.compile_regex(grammar_spec)
else:
logger.error("Validation should have already occurred. "
"Please file an issue.")
+5 -1
View File
@@ -251,7 +251,11 @@ def validate_structured_output_request(
gd_params = sampling_params.guided_decoding
if gd_params.regex:
raise ValueError("Regex structured output is not supported.")
try:
xgr.Grammar.from_regex(gd_params.regex)
except Exception as err:
raise ValueError("Failed to transform regex into a grammar: "
f"{err}") from err
if gd_params.choice:
choice_grammar = choice_as_grammar(gd_params.choice)