[Bugfix] Make disable_any_whitespace actually disable whitespace on xgrammar (#58067)
Signed-off-by: Stu Cao <stu@primeintellect.ai> Signed-off-by: Artem Perevedentsev <aperevedents@nvidia.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Artem Perevedentsev <aperevedents@nvidia.com>
This commit is contained in:
co-authored by
Claude Opus 5
Artem Perevedentsev
parent
b49c921a27
commit
73c742b09c
@@ -89,13 +89,36 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
grammar_spec: str,
|
||||
stop_token_ids: set[int] | None = None,
|
||||
) -> StructuredOutputGrammar:
|
||||
# Note(arpera):
|
||||
# Our flag disable_any_whitespace does NOT map directly to
|
||||
# xgrammar's flag any_whitespace
|
||||
# To achieve desired behavior of disable_any_whitespace
|
||||
# we have to set not only any_whitespace
|
||||
# but also specify a list of separators after which
|
||||
# xgrammar must not insert spaces.
|
||||
# This is a requirement of xgrammar's API, so we must comply with it.
|
||||
#
|
||||
# FIXME(arpera):
|
||||
# Currently xgrammar v0.2.8 DOES emit spaces after comma
|
||||
# even if we specify it in separators list.
|
||||
# The bug has been reported to xgrammar team:
|
||||
# https://github.com/mlc-ai/xgrammar/issues/945
|
||||
# Please, track that issue, and once it is resolved remove this comment.
|
||||
# Upd. this bug was fixed in xgrammar main branch on Oct 8, 2026
|
||||
# and will be available in next release.
|
||||
# So, remove this comment once xgrammar updates to v0.2.9
|
||||
separators = (",", ":") if self.disable_any_whitespace else None
|
||||
if request_type == StructuredOutputOptions.JSON:
|
||||
ctx = self.compiler.compile_json_schema(
|
||||
grammar_spec, any_whitespace=not self.disable_any_whitespace
|
||||
grammar_spec,
|
||||
any_whitespace=not self.disable_any_whitespace,
|
||||
separators=separators,
|
||||
)
|
||||
elif request_type == StructuredOutputOptions.JSON_OBJECT:
|
||||
ctx = self.compiler.compile_json_schema(
|
||||
'{"type": "object"}', any_whitespace=not self.disable_any_whitespace
|
||||
'{"type": "object"}',
|
||||
any_whitespace=not self.disable_any_whitespace,
|
||||
separators=separators,
|
||||
)
|
||||
elif request_type == StructuredOutputOptions.GRAMMAR:
|
||||
if grammar_is_likely_lark(grammar_spec):
|
||||
|
||||
Reference in New Issue
Block a user