xgrammar icon indicating copy to clipboard operation
xgrammar copied to clipboard

Support for array `minItems` and `maxItems` constraints

Open Jason-CKY opened this issue 1 year ago • 7 comments

currently xgrammar does not support json schema with the following constraints:

https://github.com/mlc-ai/xgrammar/blob/c1b64920cad24f44f235778c1c00bb52d57da01a/cpp/json_schema_converter.cc#L975-L992

relevant PR from vllm

reproducing the issue

with vllm/vllm-openai:v0.6.6.post1 docker image:

from openai import OpenAI
from pydantic import BaseModel

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="-",
)

class Person(BaseModel):
  names: list[str]: Field(..., min_length=2, max_length=2)

print(Person.model_json_schema())  # {'properties': {'names': {'items': {'type': 'string'}, 'maxItems': 2, 'minItems': 2, 'title': 'Names', 'type': 'array'}}, 'required': ['names'], 'title': 'Person', 'type': 'object'}

response = client.chat.completions.create(
  model='aya-23-35b',
  messages=[
    {
      'role': 'user',
      'content': 'Generate 4 names. Respond in json format.'
    }
  ],
  extra_body={"guided_json": Person.model_json_schema()}
)
print(response.choices[0].message.content)
  • 2 names should be generated due to JSON constraint, but 4 names is still generated

Jason-CKY avatar Jan 20 '25 16:01 Jason-CKY

Hi @Jason-CKY, thanks for raising the issue about supporting JSON Schema! I apologize for the late response—I was occupied with some paper-related work.

The minItems and maxItems are indeed not supported yet. We have plans to enhance the JSON Schema converter soon, aiming to cover most features in JSON Schema, including these. Please stay tuned for that!

Ubospica avatar Feb 12 '25 02:02 Ubospica

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Mar 18 '25 00:03 github-actions[bot]

This is still an issue and shouldn't become stale.

saattrupdan avatar Mar 18 '25 08:03 saattrupdan

similar issue for minLength and maxLength in strings as @saattrupdan mentioned definitely not a stale issue and would be amazing if it got resolved

https://github.com/mlc-ai/xgrammar/blob/c1b64920cad24f44f235778c1c00bb52d57da01a/cpp/json_schema_converter.cc#L938-L950

JakubCerven avatar Mar 24 '25 08:03 JakubCerven

Please also add uniqueItems as a possible constraint.

JulianOestreich90 avatar Jun 03 '25 14:06 JulianOestreich90

@Jason-CKY @JulianOestreich90 @saattrupdan I believe minItems, maxItems, minLength, maxLength, minProperties, maxProperties have been supported now. Please try that out.

uniqueItems is hard to implement due to the internal constraint of context-free grammar. We will think about ways to offer support.

Ubospica avatar Jul 27 '25 23:07 Ubospica

@Jason-CKY @JulianOestreich90 @saattrupdan I believe minItems, maxItems, minLength, maxLength, minProperties, maxProperties have been supported now. Please try that out.

uniqueItems is hard to implement due to the internal constraint of context-free grammar. We will think about ways to offer support.

It works now! Thanks a bunch 😃

saattrupdan avatar Aug 04 '25 16:08 saattrupdan