[Fix] Work around xgrammar 0.2.1 negative integer minimum in Kimi-K3 structural tags (#34778)
Co-authored-by: James Liu <jamesl@modal.com>
This commit is contained in:
@@ -274,6 +274,33 @@ def _value_format(
|
||||
) -> Format:
|
||||
if loose_string and json_type == "string":
|
||||
return AnyTextFormat()
|
||||
# XGrammar 0.2.1 miscompiles a one-sided negative integer lower bound:
|
||||
# {"type": "integer", "minimum": -N} accepts the incomplete value "-"
|
||||
# and rejects every valid negative integer. Splitting the range at zero
|
||||
# avoids that converter bug without weakening the schema.
|
||||
if (
|
||||
json_type == "integer"
|
||||
and isinstance(schema, dict)
|
||||
and "maximum" not in schema
|
||||
and "exclusiveMaximum" not in schema
|
||||
and "multipleOf" not in schema
|
||||
):
|
||||
lower_bounds = []
|
||||
minimum = schema.get("minimum")
|
||||
if isinstance(minimum, int) and not isinstance(minimum, bool):
|
||||
lower_bounds.append(minimum)
|
||||
exclusive_minimum = schema.get("exclusiveMinimum")
|
||||
if isinstance(exclusive_minimum, int) and not isinstance(
|
||||
exclusive_minimum, bool
|
||||
):
|
||||
lower_bounds.append(exclusive_minimum + 1)
|
||||
if lower_bounds and max(lower_bounds) < 0:
|
||||
negative = dict(schema)
|
||||
negative["maximum"] = -1
|
||||
nonnegative = dict(schema)
|
||||
nonnegative.pop("exclusiveMinimum", None)
|
||||
nonnegative["minimum"] = 0
|
||||
schema = {"anyOf": [negative, nonnegative]}
|
||||
return JSONSchemaFormat(
|
||||
json_schema=schema,
|
||||
style="qwen_xml" if json_type == "string" else "json",
|
||||
|
||||
Reference in New Issue
Block a user