Skip to content

Conversation

chaunceyjiang
Copy link
Collaborator

@chaunceyjiang chaunceyjiang commented Mar 27, 2025

FIX #15236

Structured output supports Enum.

Copy link

👋 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.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

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.

🚀

@mergify mergify bot added v1 tpu Related to Google TPUs labels Mar 27, 2025
@chaunceyjiang chaunceyjiang changed the title [Bugfix] Structured output supports Enum. [Bugfix][v1] Structured output supports Enum. Mar 27, 2025
@chaunceyjiang chaunceyjiang changed the title [Bugfix][v1] Structured output supports Enum. [Bugfix][v1] xgrammar structured output supports Enum. Mar 27, 2025
@chaunceyjiang
Copy link
Collaborator Author

chaunceyjiang commented Mar 27, 2025

Test whether xgrammar supports enum.

import xgrammar as xgr
from xgrammar.testing import  _is_grammar_accept_string

import json

# Guided decoding by JSON using Pydantic schema
class CarType(str, Enum):    # test Enum
    sedan = "sedan"
    suv = "SUV"
    truck = "Truck"
    coupe = "Coupe"


class CarDescription(BaseModel):
    brand: str
    model: str
    car_type: CarType



json_schema = CarDescription.model_json_schema()

instance = CarDescription(brand="Toyota", model="Corolla", car_type="sedan")

json_schema_grammar = xgr.Grammar.from_json_schema(json.dumps(json_schema))

# instance: pydantic model, json string, or any other object (dumped to json string)
if isinstance(instance, BaseModel):
    instance = json.dumps(instance.model_dump(mode="json", round_trip=True))



assert _is_grammar_accept_string(json_schema_grammar, instance)

@chaunceyjiang
Copy link
Collaborator Author

test

vllm serve Qwen/Qwen2.5-3B-Instruct
client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="-",
)


class CarType(str, Enum):
    sedan = "sedan"
    suv = "SUV"
    truck = "Truck"
    coupe = "Coupe"


class CarDescription(BaseModel):
    brand: str
    model: str
    car_type: CarType
json_schema = CarDescription.model_json_schema()
prompt = ("Generate a JSON with the brand, model and car_type of"
          "the most iconic car from the 90's")
completion = client.chat.completions.create(
    model="Qwen/Qwen2.5-3B-Instruct",
    messages=[{
        "role": "user",
        "content": prompt,
    }],
    extra_body={"guided_json": json_schema},
)
print(completion.choices[0].message.content)
# python test.py 
[2025-03-27 03:24:41] INFO _client.py:1026: HTTP Request: POST http://localhost:8000/v1/chat/completions "HTTP/1.1 200 OK"
{
  "brand": "Ford",
  "model": "Mustang",
  "car_type": "SUV"
}

@chaunceyjiang chaunceyjiang marked this pull request as ready for review March 27, 2025 03:26
@chaunceyjiang
Copy link
Collaborator Author

@ItzAmirreza
Copy link

Much love

Copy link
Collaborator

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

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

Can you add a test for this. Thanks.

@chaunceyjiang
Copy link
Collaborator Author

chaunceyjiang commented Mar 27, 2025

@DarkLight1337 Hi, Can you help add a ready label for e2e?

Copy link
Collaborator

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

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

Please fix the tests accordingly first.

@mergify mergify bot removed the tpu Related to Google TPUs label Mar 28, 2025
Copy link
Member

@DarkLight1337 DarkLight1337 left a comment

Choose a reason for hiding this comment

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

Approving to unblock merge once the tests pass

@DarkLight1337
Copy link
Member

Test passes

@vllm-bot vllm-bot merged commit 3b00ff9 into vllm-project:main Mar 28, 2025
12 checks passed
@russellb
Copy link
Member

It looks like full CI didn’t run on this PR for some reason before it was merged. It broke a structured output test for V1. See the failure in #15718

@DarkLight1337
Copy link
Member

Oh, my bad. I saw the entrypoints tests pass but forgot about V1.

@chaunceyjiang chaunceyjiang deleted the xgrammar branch March 29, 2025 11:06
@chaunceyjiang
Copy link
Collaborator Author

@russellb Thanks~

@DarkLight1337
Copy link
Member

Are you able to fix this quickly?

@chaunceyjiang
Copy link
Collaborator Author

Okay, I'll check it out right away.

Alex4210987 pushed a commit to LeiWang1999/vllm-bitblas that referenced this pull request Apr 5, 2025
lulmer pushed a commit to lulmer/vllm that referenced this pull request Apr 7, 2025
lk-chen pushed a commit to lk-chen/vllm that referenced this pull request Apr 29, 2025
shreyankg pushed a commit to shreyankg/vllm that referenced this pull request May 3, 2025
RichardoMrMu pushed a commit to RichardoMrMu/vllm that referenced this pull request May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

6 participants