Skip to content

[Bugfix] Fix xgrammar feature gate bypass when JSON Schema type is a list#48416

Open
CaiJohn wants to merge 1 commit into
vllm-project:mainfrom
CaiJohn:bugfix/xgrammar-json-type-array-gate
Open

[Bugfix] Fix xgrammar feature gate bypass when JSON Schema type is a list#48416
CaiJohn wants to merge 1 commit into
vllm-project:mainfrom
CaiJohn:bugfix/xgrammar-json-type-array-gate

Conversation

@CaiJohn

@CaiJohn CaiJohn commented Jul 12, 2026

Copy link
Copy Markdown

Purpose

has_xgrammar_unsupported_json_features gates every unsupported-feature check
behind a scalar type comparison (obj.get("type") == "string",
obj.get("type") in ("integer", "number"), etc.). JSON Schema also permits
type to be a list (e.g. {"type": ["integer", "null"]}), for which these
comparisons are always false, so the checks are skipped entirely.

Because of this, schemas that are semantically equivalent to a flagged scalar
schema slip through the gate once type is written as a list:

Schema Detected as unsupported?
{"type": "integer", "multipleOf": 3} ✅ yes
{"type": ["integer", "null"], "multipleOf": 3} no (bug)

The same bypass applies to unsupported string format, object
patternProperties/propertyNames, and array
uniqueItems/contains/minContains/maxContains.

In the default auto backend mode this gate decides whether to keep the
request on xgrammar or fall back to a backend that can enforce the constraint.
A bypass keeps the request on xgrammar, which cannot express e.g. multipleOf
and silently drops the constraint at compile time — the model then produces
output that violates the user's schema, with no error. This is observable at
the acceptance level: the grammar that xgrammar compiles for
{"type": ["integer", "null"], "multipleOf": 3} accepts 7, which is not a
multiple of 3.

The trigger is that type is a list at all — not the presence of null
(a single-element list {"type": ["integer"]} bypasses just the same).

The fix normalizes type to a set of strings via a small _types_of helper
and matches against it, so scalar and list forms behave identically.

Test Plan

pytest tests/v1/structured_output/test_utils.py -q

New tests in tests/v1/structured_output/test_utils.py:

  • test_unsupported_json_features_with_list_type — every unsupported family is
    detected for list-type, covering single-element, nullable, and multi-type
    lists so the invariant (list vs. scalar, not null) is explicit.
  • test_supported_list_type_json_features — benign list-type schemas
    (e.g. {"type": ["string", "null"], "format": "email"}) stay supported.
  • test_validate_xgrammar_grammar_rejects_unsupported_schema — request-time
    validation rejects the list-type schema the same way it rejects the scalar
    one, instead of letting it reach xgrammar.

Test Result

21 passed in 4.16s

Before the fix, the list-type cases fail (gate returns False for
{"type": ["integer", "null"], "multipleOf": 3}, and validation does not
raise); after, they pass.


AI-assisted: the fix and tests were drafted with Claude Code. I reviewed every
line, verified the bug and fix end-to-end on CPU, and ran the test suite myself.

…list

`has_xgrammar_unsupported_json_features` guards each unsupported-feature
check behind a scalar `type` comparison, e.g. `obj.get("type") == "string"`
or `obj.get("type") in ("integer", "number")`. JSON Schema also allows
`type` to be a list (e.g. `{"type": ["integer", "null"]}`), for which these
comparisons are always false, so the checks are skipped entirely.

As a result, schemas that are semantically equivalent to a flagged scalar
schema slip through the gate: `multipleOf`, unsupported string `format`,
object `patternProperties`/`propertyNames`, and array
`uniqueItems`/`contains`/`minContains`/`maxContains` are no longer
detected once `type` is written as a list. In the default `auto` backend
mode this gate decides whether to keep the request on xgrammar or fall
back to a backend that can enforce the constraint; a bypass leaves the
request on xgrammar, which cannot express e.g. `multipleOf` and silently
drops the constraint at compile time (verified: the compiled grammar for
`{"type": ["integer", "null"], "multipleOf": 3}` accepts `7`).

The trigger is that `type` is a list at all, not the presence of `null`.

Normalize `type` to a set of strings via a `_types_of` helper and match
against it, so scalar and list forms behave identically. Tests cover the
list-`type` bypass across every unsupported family (single-element,
nullable, and multi-type lists), that benign list-`type` schemas stay
supported, and that request validation rejects a list-`type` unsupported
schema the same way it rejects the scalar one.

Signed-off-by: john <6712432+CaiJohn@users.noreply.github.com>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

PRs do not trigger a full CI run by default. Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

If you have any questions, please reach out to us on Slack at https://slack.vllm.ai.

Agent Guidelines

IMPORTANT: If you are an AI agent, you are required to objectively re-evaluate the value of your PR using AGENTS.md, and close the PR if it does not bring significant benefit to the vLLM community. Failure to do so may result in an immediate ban.

🚀

@mergify mergify Bot added the bug Something isn't working label Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working structured-output v1

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant