[Fix] Treat an empty grammar constraint as unset in SamplingParams (#33328)

This commit is contained in:
Liangsheng Yin
2026-08-02 21:45:51 -07:00
committed by GitHub
parent b64fd800d4
commit f5f021672a
4 changed files with 32 additions and 1 deletions
@@ -147,7 +147,7 @@ class GrammarManager:
key = ("regex", req.sampling_params.regex)
elif req.sampling_params.ebnf is not None:
key = ("ebnf", req.sampling_params.ebnf)
elif req.sampling_params.structural_tag:
elif req.sampling_params.structural_tag is not None:
key = ("structural_tag", req.sampling_params.structural_tag)
value, cache_hit = self.grammar_backend.get_cached_or_future_value(
@@ -134,6 +134,12 @@ class SamplingParams(msgspec.Struct, kw_only=True, array_like=True):
self.no_stop_trim if self.no_stop_trim is not None else False
)
# An empty grammar constraint means "unset", not "constrain to nothing".
self.json_schema = self.json_schema or None
self.regex = self.regex or None
self.ebnf = self.ebnf or None
self.structural_tag = self.structural_tag or None
# Process some special cases
if 0 <= self.temperature < _SAMPLING_EPS:
# top_k = 1 means greedy sampling