Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for array minItems and maxItems constraints #160

Open
Jason-CKY opened this issue Jan 20, 2025 · 1 comment
Open

Support for array minItems and maxItems constraints #160

Jason-CKY opened this issue Jan 20, 2025 · 1 comment

Comments

@Jason-CKY
Copy link

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

std::string JSONSchemaConverter::VisitArray(
const picojson::object& schema, const std::string& rule_name
) {
XGRAMMAR_CHECK(
(schema.count("type") && schema.at("type").get<std::string>() == "array") ||
schema.count("items") || schema.count("prefixItems") || schema.count("unevaluatedItems")
);
WarnUnsupportedKeywords(
schema,
{
"uniqueItems",
"contains",
"minContains",
"maxContains",
"minItems",
"maxItems",
}
);

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
@Ubospica
Copy link
Collaborator

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants