Skip to content

Commit 4504e80

Browse files
authored
[Bugfix] Prevent crash on empty grammar string (vllm-project#28210)
Signed-off-by: tjandy98 <[email protected]>
1 parent ca00b1b commit 4504e80

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

tests/v1/entrypoints/openai/test_chat_completion.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,23 @@ async def test_invalid_grammar(client: openai.AsyncOpenAI, model_name: str):
138138
"structured_outputs": {"grammar": invalid_simplified_sql_grammar}
139139
},
140140
)
141+
142+
143+
@pytest.mark.asyncio
144+
@pytest.mark.parametrize(
145+
"model_name",
146+
[MODEL_NAME],
147+
)
148+
async def test_empty_grammar(client: openai.AsyncOpenAI, model_name: str) -> None:
149+
prompt = "Say hello"
150+
with pytest.raises((openai.BadRequestError, openai.APIError)):
151+
await client.chat.completions.create(
152+
model=model_name,
153+
messages=[
154+
{
155+
"role": "user",
156+
"content": prompt,
157+
}
158+
],
159+
extra_body={"structured_outputs": {"grammar": ""}},
160+
)

vllm/v1/engine/processor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ def _validate_structured_output(self, params: SamplingParams) -> None:
270270
raise ValueError(
271271
f"Choice '{params.structured_outputs.choice}' cannot be an empty list" # noqa: E501
272272
)
273+
# Reject empty string grammar early to avoid engine-side crashes
274+
if (
275+
isinstance(params.structured_outputs.grammar, str)
276+
and params.structured_outputs.grammar.strip() == ""
277+
):
278+
raise ValueError("structured_outputs.grammar cannot be an empty string")
273279

274280
if backend.startswith("xgrammar"):
275281
# xgrammar with no fallback

0 commit comments

Comments
 (0)