fix(frontend): return HTTP 400 for out-of-vocabulary token_ids_logprob (#28088)

Signed-off-by: Ting Sun <suntcrick@gmail.com>
This commit is contained in:
Ting SUN
2026-06-12 17:18:26 -07:00
committed by GitHub
parent 82eedd5bd0
commit 335a9c7837
2 changed files with 87 additions and 1 deletions
@@ -1,6 +1,7 @@
import unittest
import openai
import requests
from sglang.srt.utils import kill_process_tree
from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci
@@ -104,6 +105,70 @@ class TestRequestLengthValidation(CustomTestCase):
str(cm.exception),
)
def test_token_ids_logprob_out_of_vocabulary(self):
headers = {"Authorization": f"Bearer {self.api_key}"}
for token_ids_logprob in ([-1], [2_000_000_000]):
response = requests.post(
f"{self.base_url}/generate",
headers=headers,
json={
"text": "hi",
"sampling_params": {"max_new_tokens": 1},
"return_logprob": True,
"token_ids_logprob": token_ids_logprob,
},
)
self.assertEqual(response.status_code, 400)
self.assertIn("out-of-vocabulary", response.text)
def test_token_ids_logprob_rejects_nested_list(self):
# Nested lists are a batch-level wire format; a single request must
# pass a flat list of ints. A ragged nested list with in-vocab ids
# would otherwise crash the scheduler in the sampler gather.
headers = {"Authorization": f"Bearer {self.api_key}"}
for token_ids_logprob in ([[0]], [[0], [1, 2]]):
response = requests.post(
f"{self.base_url}/generate",
headers=headers,
json={
"text": "hi",
"sampling_params": {"max_new_tokens": 1},
"return_logprob": True,
"token_ids_logprob": token_ids_logprob,
},
)
self.assertEqual(response.status_code, 400)
self.assertIn("flat list of integers", response.text)
def test_token_ids_logprob_batch_with_one_oov(self):
headers = {"Authorization": f"Bearer {self.api_key}"}
response = requests.post(
f"{self.base_url}/generate",
headers=headers,
json={
"text": ["hi", "hi"],
"sampling_params": {"max_new_tokens": 1},
"return_logprob": True,
"token_ids_logprob": [[0], [2_000_000_000]],
},
)
self.assertEqual(response.status_code, 400)
self.assertIn("out-of-vocabulary", response.text)
def test_token_ids_logprob_valid(self):
headers = {"Authorization": f"Bearer {self.api_key}"}
response = requests.post(
f"{self.base_url}/generate",
headers=headers,
json={
"text": "hi",
"sampling_params": {"max_new_tokens": 1},
"return_logprob": True,
"token_ids_logprob": [0],
},
)
self.assertEqual(response.status_code, 200)
if __name__ == "__main__":
unittest.main()