"""The per-turn schema in the form vLLM 0.17 can decode. spec/validate.py `guided_schema(offered_tools)` is correct JSON Schema, but the grammar engines inside vLLM 0.17.0 (xgrammar 0.1.29, llguidance 1.3.0) do not handle all of it: * `uniqueItems` (on `directives`): the server answers HTTP 400 `Grammar error: Unimplemented keys: ["uniqueItems"]`. * a number with a lower bound and no upper bound (`{"type":"number","exclusiveMinimum":0}`, used by weights, heights, doses, concentrations): xgrammar builds a grammar that cannot write any value between 0 and 1, and does allow 0. Asked for 0.5, the model is forced to write 0. A wrong dose input is worse than an unchecked one. * `maxLength` (39 string fields): correct, but it is what makes the grammar huge. With it, xgrammar needs 7-21 s to compile one schema (13 s for 17 offered tools, 21 s for all 22) and the compiled grammar is 93-663 MB, so vLLM's 512 MB grammar cache holds one schema at most: every change of the offered tool set costs the compile again, on the request path. Without `maxLength` the same schemas compile in 0.2-0.3 s to 7-17 MB. Runaway strings are still stopped by `max_tokens`. `for_vllm` removes exactly those three things and nothing else. The removed rules are not lost: spec/validate.py checks the full schema on every decoded plan (duplicate directives, numbers that must be above zero, string lengths), so ALWAYS validate after decoding. `for_vllm(schema, keep_max_length=True)` keeps the lengths for a caller that sends one fixed schema and can afford the compile at start-up. from serve.vllm_schema import plan_schema, response_format body["response_format"] = response_format(offered_tools) Standard library + spec/validate.py (jsonschema). """ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "spec")) import validate REJECTED_KEYWORDS = {"uniqueItems"} SLOW_KEYWORDS = {"maxLength"} LOWER_BOUNDS = ("minimum", "exclusiveMinimum") UPPER_BOUNDS = ("maximum", "exclusiveMaximum") def strip_keywords(schema, names): """A copy of `schema` without the given keywords. Property NAMES are never touched.""" if isinstance(schema, dict): out = {} for key, value in schema.items(): if key in names: continue if key == "properties" and isinstance(value, dict): out[key] = {prop: strip_keywords(sub, names) for prop, sub in value.items()} else: out[key] = strip_keywords(value, names) return out if isinstance(schema, list): return [strip_keywords(item, names) for item in schema] return schema def _drop_open_lower_bounds(schema): if isinstance(schema, dict): out = {} kinds = schema.get("type") kinds = kinds if isinstance(kinds, list) else [kinds] open_number = "number" in kinds and not any(k in schema for k in UPPER_BOUNDS) for key, value in schema.items(): if open_number and key in LOWER_BOUNDS: continue if key == "properties" and isinstance(value, dict): out[key] = {prop: _drop_open_lower_bounds(sub) for prop, sub in value.items()} else: out[key] = _drop_open_lower_bounds(value) return out if isinstance(schema, list): return [_drop_open_lower_bounds(item) for item in schema] return schema def for_vllm(schema, keep_max_length=False): """`schema` without the keywords vLLM 0.17 rejects, decodes wrongly or compiles slowly.""" drop = REJECTED_KEYWORDS if keep_max_length else REJECTED_KEYWORDS | SLOW_KEYWORDS return _drop_open_lower_bounds(strip_keywords(schema, drop)) def plan_schema(offered_tools): """The schema to send for a turn that offers `offered_tools`.""" return for_vllm(validate.guided_schema(offered_tools)) def response_format(offered_tools): """The `response_format` field of a chat completion request (the method that works).""" return {"type": "json_schema", "json_schema": {"name": "turn_plan", "schema": plan_schema(offered_tools), "strict": True}}