Skip to content

Commit a4fc218

Browse files
[Bugfix] Fixed when return_token_ids=False, the first event still contains prompt_token_ids. (vllm-project#27561)
Signed-off-by: chaunceyjiang <[email protected]>
1 parent a3e8611 commit a4fc218

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

tests/entrypoints/openai/test_return_token_ids.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ def server():
2727

2828

2929
@pytest.mark.asyncio
30-
async def test_basic_completion_with_emoji(server):
30+
@pytest.mark.parametrize("return_token_ids", [True, False, None])
31+
async def test_basic_completion_with_emoji(server, return_token_ids: bool | None):
3132
"""Test basic completion with emoji to verify token_ids field."""
33+
extra_body = None
34+
if return_token_ids is not None:
35+
extra_body = {"return_token_ids": return_token_ids}
3236
async with server.get_async_client() as client:
3337
# Test with return_token_ids enabled
3438
completion = await client.completions.create(
@@ -37,14 +41,20 @@ async def test_basic_completion_with_emoji(server):
3741
max_tokens=10,
3842
temperature=0,
3943
logprobs=1,
40-
extra_body={"return_token_ids": True},
44+
extra_body=extra_body,
4145
)
4246

4347
# Check the raw response to see the structure
4448
completion_dict = completion.model_dump()
4549

4650
# Verify prompt_token_ids field is present in the completion response
4751
assert "prompt_token_ids" in completion_dict["choices"][0]
52+
if not return_token_ids:
53+
# If return_token_ids is False, token_ids should not be present
54+
assert completion_dict["choices"][0].get("token_ids") is None
55+
assert completion_dict["choices"][0].get("prompt_token_ids") is None
56+
# Skip further checks
57+
return
4858
assert isinstance(completion.choices[0].prompt_token_ids, list)
4959

5060
# Check against the expected prompt token IDs

vllm/entrypoints/openai/serving_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ async def completion_stream_generator(
399399

400400
# has_echoed[i] is reused here to indicate whether
401401
# we have already returned the prompt token IDs.
402-
if not has_echoed[i]:
402+
if not has_echoed[i] and request.return_token_ids:
403403
prompt_token_ids_to_return = prompt_token_ids
404404
has_echoed[i] = True
405405

0 commit comments

Comments
 (0)