Support for array `minItems` and `maxItems` constraints
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
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
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!
This issue is stale because it has been open for 30 days with no activity.
This is still an issue and shouldn't become stale.
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
Please also add uniqueItems as a possible constraint.
@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.
@Jason-CKY @JulianOestreich90 @saattrupdan I believe
minItems,maxItems,minLength,maxLength,minProperties,maxPropertieshave been supported now. Please try that out.
uniqueItemsis 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 😃