Fix chat completion 500 on non-object JSON bodies (#51654)
Signed-off-by: Tarun Kumar <takumar@redhat.com>
This commit is contained in:
@@ -745,6 +745,8 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_response_format(cls, data):
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
response_format = data.get("response_format")
|
||||
if response_format is None:
|
||||
return data
|
||||
@@ -776,6 +778,8 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_stream_options(cls, data):
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
if data.get("stream_options") and not data.get("stream"):
|
||||
raise VLLMValidationError(
|
||||
"Stream options can only be defined when `stream=True`.",
|
||||
@@ -787,6 +791,8 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def check_logprobs(cls, data):
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
if data.get("logprob_token_ids") and data.get("use_beam_search"):
|
||||
raise VLLMValidationError(
|
||||
"`logprob_token_ids` is not supported with beam search.",
|
||||
@@ -845,6 +851,8 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
||||
def check_structured_outputs_count(cls, data):
|
||||
if isinstance(data, ValueError):
|
||||
raise data
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
|
||||
if data.get("structured_outputs", None) is None:
|
||||
return data
|
||||
@@ -970,6 +978,8 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def check_generation_prompt(cls, data):
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
if data.get("continue_final_message") and data.get("add_generation_prompt"):
|
||||
raise VLLMValidationError(
|
||||
"Cannot set both `continue_final_message` and "
|
||||
@@ -1105,6 +1115,8 @@ class BatchChatCompletionRequest(OpenAIBaseModel):
|
||||
def check_batch_mode(cls, data: Any) -> Any:
|
||||
if isinstance(data, BatchChatCompletionRequest):
|
||||
data = data.model_dump(exclude_unset=True)
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
if data.get("use_beam_search"):
|
||||
raise VLLMValidationError(
|
||||
"Batch chat completions do not support beam search. "
|
||||
@@ -1117,13 +1129,14 @@ class BatchChatCompletionRequest(OpenAIBaseModel):
|
||||
parameter="logprob_token_ids",
|
||||
)
|
||||
response_format = data.get("response_format")
|
||||
rf_type = (
|
||||
response_format.get("type")
|
||||
if isinstance(response_format, dict)
|
||||
else getattr(response_format, "type", None)
|
||||
)
|
||||
if rf_type == "structural_tag":
|
||||
validate_structural_tag_response_format(response_format)
|
||||
if response_format is not None:
|
||||
rf_type = (
|
||||
response_format.get("type")
|
||||
if isinstance(response_format, dict)
|
||||
else getattr(response_format, "type", None)
|
||||
)
|
||||
if rf_type == "structural_tag":
|
||||
validate_structural_tag_response_format(response_format)
|
||||
if (structured_outputs := data.get("structured_outputs")) is not None:
|
||||
validate_structured_outputs_structural_tag(structured_outputs)
|
||||
n = data.get("n", 1)
|
||||
|
||||
Reference in New Issue
Block a user