[Frontend][Rust] Reject empty structured-output values (#54821)

Signed-off-by: Clinton Thomas <1033162+KernelClint@users.noreply.github.com>
Co-authored-by: Lucas Bourtoule <35483370+dhalf@users.noreply.github.com>
This commit is contained in:
Clinton Thomas
2026-09-12 19:12:25 +04:00
committed by GitHub
co-authored by Lucas Bourtoule
parent 658c8131c7
commit 22f6e4eccb
4 changed files with 175 additions and 11 deletions
@@ -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>(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<WireStructuredOutputsParams> 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<WireStructuredOutputsParams> 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::<StructuredOutputsParams>(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 {
+18
View File
@@ -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 {
@@ -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!({
@@ -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