diff --git a/rust/src/engine-core-client/src/protocol/structured_outputs.rs b/rust/src/engine-core-client/src/protocol/structured_outputs.rs index 745ca3150b..99fa66ae2b 100644 --- a/rust/src/engine-core-client/src/protocol/structured_outputs.rs +++ b/rust/src/engine-core-client/src/protocol/structured_outputs.rs @@ -104,6 +104,41 @@ impl StructuredOutputsParams { backend: StructuredOutputBackend::default(), } } + + pub fn validate(&self) -> Result<()> { + match &self.constraint { + StructuredOutputConstraint::Grammar(grammar) if grammar.trim().is_empty() => { + Err(Error::InvalidStructuredOutputsParams { + message: "structured_outputs.grammar cannot be an empty string".to_string(), + }) + } + StructuredOutputConstraint::Json(Value::String(value)) => { + if value.trim().is_empty() { + return Err(Error::InvalidStructuredOutputsParams { + message: "structured_outputs.json cannot be an empty string".to_string(), + }); + } + serde_json::from_str::(value).map_err(|_| { + Error::InvalidStructuredOutputsParams { + message: "Invalid JSON grammar specification.".to_string(), + } + })?; + Ok(()) + } + StructuredOutputConstraint::Choice(choice) if choice.is_empty() => { + Err(Error::InvalidStructuredOutputsParams { + message: "structured_outputs.choice cannot be empty".to_string(), + }) + } + StructuredOutputConstraint::StructuralTag(tag) if tag.trim().is_empty() => { + Err(Error::InvalidStructuredOutputsParams { + message: "structured_outputs.structural_tag cannot be an empty string" + .to_string(), + }) + } + _ => Ok(()), + } + } } /// Wire-compatible structured-output payload used by Python engine-core. @@ -176,7 +211,7 @@ impl TryFrom for StructuredOutputsParams { } insert_constraint!("structural_tag", raw.structural_tag.map(StructuralTag)); - Ok(Self { + let params = Self { constraint: constraint.map(|(_, c)| c).ok_or_else(|| { Error::InvalidStructuredOutputsParams { message: "missing structured output constraint".to_string(), @@ -188,7 +223,9 @@ impl TryFrom for StructuredOutputsParams { whitespace_pattern: raw.whitespace_pattern, }, backend: raw.backend, - }) + }; + params.validate()?; + Ok(params) } } @@ -289,6 +326,55 @@ mod tests { assert!(error.to_string().contains("json_object must be true")); } + #[test] + fn structured_outputs_rejects_empty_json_string() { + let error = serde_json::from_value::(serde_json::json!({ + "json": " ", + })) + .unwrap_err(); + + assert!(error.to_string().contains("json cannot be an empty string")); + } + + #[test] + fn validate_rejects_empty_grammar_from_constructor() { + let error = StructuredOutputsParams::grammar(" ").validate().unwrap_err(); + assert!(error.to_string().contains("grammar cannot be an empty string")); + } + + #[test] + fn validate_rejects_empty_json_string_from_constructor() { + let error = StructuredOutputsParams::json(Value::String(" ".to_string())) + .validate() + .unwrap_err(); + assert!(error.to_string().contains("json cannot be an empty string")); + } + + #[test] + fn validate_rejects_malformed_json_string() { + let error = StructuredOutputsParams::json(Value::String("not json".to_string())) + .validate() + .unwrap_err(); + assert!(error.to_string().contains("Invalid JSON grammar specification.")); + } + + #[test] + fn validate_rejects_empty_choice() { + let error = StructuredOutputsParams::choice(vec![]).validate().unwrap_err(); + assert!(error.to_string().contains("choice cannot be empty")); + } + + #[test] + fn validate_rejects_empty_structural_tag() { + let error = StructuredOutputsParams::structural_tag("\n\t").validate().unwrap_err(); + assert!(error.to_string().contains("structural_tag cannot be an empty string")); + } + + #[test] + fn validate_accepts_non_empty_grammar() { + assert!(StructuredOutputsParams::grammar("root ::= \"a\"").validate().is_ok()); + } + #[test] fn structured_outputs_serializes_through_raw_shape() { let params = StructuredOutputsParams { diff --git a/rust/src/server/src/grpc/convert.rs b/rust/src/server/src/grpc/convert.rs index cf0045f714..61eb21ef70 100644 --- a/rust/src/server/src/grpc/convert.rs +++ b/rust/src/server/src/grpc/convert.rs @@ -4,6 +4,7 @@ //! Conversion between gRPC protobuf types and internal `vllm-text` //! request/response types. +use thiserror_ext::AsReport as _; use tonic::Status; use uuid::Uuid; use vllm_engine_core_client::protocol::output::StopReason; @@ -259,6 +260,9 @@ fn convert_structured_output( StructuredOutputsParams::structural_tag(tag.clone()) } }; + params + .validate() + .map_err(|error| Status::invalid_argument(error.to_report_string()))?; Ok(Some(params)) } @@ -546,6 +550,20 @@ mod tests { assert_eq!(text.sampling_params.temperature, Some(0.0)); } + #[test] + fn grpc_rejects_empty_grammar_before_engine() { + use super::pb::decoding_parameters::StructuredOutput; + let req = pb::GenerateRequest { + decoding: Some(pb::DecodingParameters { + structured_output: Some(StructuredOutput::Grammar(" ".to_string())), + ..Default::default() + }), + ..base_request() + }; + let err = to_text_request(req, false, &["test-model".to_string()]).unwrap_err(); + assert!(err.message().contains("grammar cannot be an empty string")); + } + #[test] fn absent_seed_is_none() { let req = pb::GenerateRequest { diff --git a/rust/src/server/src/routes/openai/chat_completions/convert.rs b/rust/src/server/src/routes/openai/chat_completions/convert.rs index 3aaba8df28..2c64ffbef4 100644 --- a/rust/src/server/src/routes/openai/chat_completions/convert.rs +++ b/rust/src/server/src/routes/openai/chat_completions/convert.rs @@ -437,7 +437,7 @@ mod tests { use std::collections::HashMap; use std::sync::Arc; - use axum::http::HeaderMap; + use axum::http::{HeaderMap, StatusCode}; use expect_test::expect; use llm_multimodal::ImageDetail; use serde_json::json; @@ -583,6 +583,61 @@ mod tests { assert_eq!(prepared.chat_request.decode_options.min_tokens, 0); } + #[test] + fn prepare_chat_request_rejects_empty_json_schema() { + let mut request = base_request(); + request.response_format = Some(ResponseFormat::JsonSchema { + json_schema: JsonSchemaFormat { + name: "answer".to_string(), + description: None, + schema: json!(" "), + strict: None, + }, + }); + + let error = prepare_chat_request( + request, + &served(&["Qwen/Qwen1.5-0.5B-Chat"]), + ResolvedRequestContext::default(), + ) + .unwrap_err(); + + assert_eq!(error.status_code(), StatusCode::BAD_REQUEST); + assert!( + error + .to_error_response() + .error + .message + .contains("json cannot be an empty string") + ); + } + + #[test] + fn prepare_chat_request_rejects_whitespace_only_structured_outputs_grammar() { + let request: ChatCompletionRequest = serde_json::from_value(json!({ + "model": "Qwen/Qwen1.5-0.5B-Chat", + "messages": [{"role": "user", "content": "hello"}], + "structured_outputs": {"grammar": " \t\n"}, + })) + .expect("parse structured_outputs"); + + let error = prepare_chat_request( + request, + &served(&["Qwen/Qwen1.5-0.5B-Chat"]), + ResolvedRequestContext::default(), + ) + .unwrap_err(); + + assert_eq!(error.status_code(), StatusCode::BAD_REQUEST); + assert!( + error + .to_error_response() + .error + .message + .contains("grammar cannot be an empty string") + ); + } + #[test] fn prepare_chat_request_defaults_function_tool_fields() { let request: ChatCompletionRequest = serde_json::from_value(json!({ diff --git a/rust/src/server/src/routes/openai/utils/structured_outputs.rs b/rust/src/server/src/routes/openai/utils/structured_outputs.rs index 849870f32a..864e00818b 100644 --- a/rust/src/server/src/routes/openai/utils/structured_outputs.rs +++ b/rust/src/server/src/routes/openai/utils/structured_outputs.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; +use thiserror_ext::AsReport as _; use vllm_engine_core_client::protocol::structured_outputs::StructuredOutputsParams; use crate::error::ApiError; @@ -80,12 +81,12 @@ pub fn convert_from_response_format( let Some(fmt) = response_format else { return Ok(None); }; - match fmt { - ResponseFormat::Text => Ok(None), - ResponseFormat::JsonObject => Ok(Some(StructuredOutputsParams::json_object())), - ResponseFormat::JsonSchema { json_schema } => Ok(Some(StructuredOutputsParams::json( - json_schema.schema.clone(), - ))), + let params = match fmt { + ResponseFormat::Text => return Ok(None), + ResponseFormat::JsonObject => StructuredOutputsParams::json_object(), + ResponseFormat::JsonSchema { json_schema } => { + StructuredOutputsParams::json(json_schema.schema.clone()) + } ResponseFormat::StructuralTag { .. } => { // The Python frontend dumps the entire response_format object (including the // `type` field) as a JSON string for the engine-core backend. @@ -95,9 +96,13 @@ pub fn convert_from_response_format( Some("response_format"), ) })?; - Ok(Some(StructuredOutputsParams::structural_tag(tag_json))) + StructuredOutputsParams::structural_tag(tag_json) } - } + }; + params.validate().map_err(|error| { + ApiError::invalid_request(error.to_report_string(), Some("response_format")) + })?; + Ok(Some(params)) } /// Convert raw `response_format` and/or `structured_outputs` JSON blobs into