[BugFix] Add modalities and logprobs validation for /v1/chat/completions (#4743)
Signed-off-by: Shaun Walsh <shaunwalsh24@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Alex Brooks <albrooks@redhat.com>
This commit is contained in:
3 files changed
+62
-5
No files matched your search
@@ -1198,6 +1198,22 @@ async def omni_init_app_state(
|
||||
state.server_load_metrics = 0
|
||||
|
||||
|
||||
def _validate_chat_completion_raw_body(raw_body: dict[str, Any]) -> None:
|
||||
"""Reject values that upstream ChatCompletionRequest coerces too broadly."""
|
||||
if "modalities" in raw_body and raw_body["modalities"] is not None:
|
||||
modalities = raw_body["modalities"]
|
||||
if not isinstance(modalities, list) or not all(isinstance(m, str) for m in modalities):
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST.value,
|
||||
detail='modalities must be a list of strings, e.g. ["text", "audio", "image"]',
|
||||
)
|
||||
if "logprobs" in raw_body and raw_body["logprobs"] is not None and not isinstance(raw_body["logprobs"], bool):
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST.value,
|
||||
detail="logprobs must be a boolean (true or false)",
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/v1/chat/completions",
|
||||
dependencies=[Depends(validate_json_request)],
|
||||
@@ -1211,6 +1227,8 @@ async def omni_init_app_state(
|
||||
@with_cancellation
|
||||
@load_aware_call
|
||||
async def create_chat_completion(request: ChatCompletionRequest, raw_request: Request):
|
||||
raw_body = await raw_request.json()
|
||||
_validate_chat_completion_raw_body(raw_body)
|
||||
metrics_header_format = raw_request.headers.get(ENDPOINT_LOAD_METRICS_FORMAT_HEADER_LABEL, "")
|
||||
handler = Omnichat(raw_request)
|
||||
if handler is None:
|
||||
@@ -1287,6 +1305,8 @@ async def create_chat_completion(request: ChatCompletionRequest, raw_request: Re
|
||||
@with_cancellation
|
||||
@load_aware_call
|
||||
async def create_batch_chat_completion(request: BatchChatCompletionRequest, raw_request: Request):
|
||||
raw_body = await raw_request.json()
|
||||
_validate_chat_completion_raw_body(raw_body)
|
||||
handler = OmniBatchChat(raw_request)
|
||||
if handler is None:
|
||||
base_server = getattr(raw_request.app.state, "serving_tokenization", None)
|
||||
|
||||
Reference in new issue
Block a user