Skip to content

Commit 41a645f

Browse files
ravi03071991Ravi Theja Desetty
andauthored
Handle empty input string for embedding models (#5621)
Co-authored-by: Ravi Theja Desetty <[email protected]>
1 parent 2301063 commit 41a645f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

python/sglang/srt/openai_api/adapter.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ def guess_chat_template_name_from_model_path(model_path):
175175
)
176176

177177

178+
def _validate_prompt(prompt: str):
179+
"""Validate that the prompt is not empty or whitespace only."""
180+
is_invalid = False
181+
182+
# Check for empty/whitespace string
183+
if isinstance(prompt, str):
184+
is_invalid = not prompt.strip()
185+
# Check for various invalid list cases: [], [""], [" "], [[]]
186+
elif isinstance(prompt, list):
187+
is_invalid = not prompt or (
188+
len(prompt) == 1
189+
and (
190+
(isinstance(prompt[0], str) and not prompt[0].strip())
191+
or (isinstance(prompt[0], list) and not prompt[0])
192+
)
193+
)
194+
195+
if is_invalid:
196+
raise HTTPException(
197+
status_code=400,
198+
detail="Input cannot be empty or contain only whitespace.",
199+
)
200+
201+
return prompt
202+
203+
178204
async def v1_files_create(
179205
file: UploadFile, purpose: str, file_storage_path: str = None
180206
):
@@ -1753,6 +1779,8 @@ def v1_embedding_request(all_requests, tokenizer_manager):
17531779

17541780
for request in all_requests:
17551781
prompt = request.input
1782+
# Check for empty/whitespace string
1783+
prompt = _validate_prompt(request.input)
17561784
assert (
17571785
type(prompt) is first_prompt_type
17581786
), "All prompts must be of the same type in file input settings"

test/srt/test_openai_server.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,22 @@ def test_embedding_batch(self):
676676
self.assertTrue(len(response.data[0].embedding) > 0)
677677
self.assertTrue(len(response.data[1].embedding) > 0)
678678

679+
def test_empty_string_embedding(self):
680+
"""Test embedding an empty string."""
681+
682+
client = openai.Client(api_key=self.api_key, base_url=self.base_url)
683+
684+
# Text embedding example with empty string
685+
text = ""
686+
# Expect a BadRequestError for empty input
687+
with self.assertRaises(openai.BadRequestError) as cm:
688+
client.embeddings.create(
689+
model=self.model,
690+
input=text,
691+
)
692+
# check the status code
693+
self.assertEqual(cm.exception.status_code, 400)
694+
679695

680696
class TestOpenAIServerIgnoreEOS(CustomTestCase):
681697
@classmethod

0 commit comments

Comments
 (0)