Bound generation inputs before expensive work (#51447)

Signed-off-by: Clinton Thomas <1033162+KernelClint@users.noreply.github.com>
Co-authored-by: Lucas Bourtoule <35483370+dhalf@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Clinton Thomas
2026-08-11 22:03:50 +08:00
committed by GitHub
co-authored by Lucas Bourtoule mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 5b184f775a
commit b64a2708b0
11 changed files with 421 additions and 37 deletions
@@ -54,9 +54,23 @@ impl StringOrArray {
}
}
/// Validates stop sequences (non-empty strings)
fn max_stop_strings() -> usize {
std::env::var("VLLM_MAX_STOP_STRINGS")
.ok()
.and_then(|value| value.parse().ok())
.unwrap_or(4)
}
/// Validates stop sequences (at most the configured number of non-empty strings).
pub fn validate_stop(stop: &StringOrArray) -> Result<(), validator::ValidationError> {
if stop.as_slice().iter().any(|s| s.is_empty()) {
let stop = stop.as_slice();
let max_stop_strings = max_stop_strings();
if stop.len() > max_stop_strings {
let mut error = validator::ValidationError::new("too_many_stop_strings");
error.code = format!("stop strings must contain at most {max_stop_strings} items").into();
return Err(error);
}
if stop.iter().any(|s| s.is_empty()) {
return Err(validator::ValidationError::new(
"stop strings cannot be empty",
));
@@ -64,6 +78,37 @@ pub fn validate_stop(stop: &StringOrArray) -> Result<(), validator::ValidationEr
Ok(())
}
#[cfg(test)]
mod tests {
use super::{StringOrArray, validate_stop};
#[test]
fn validate_stop_accepts_four_stop_strings() {
let stop = StringOrArray::Array(
["one", "two", "three", "four"].into_iter().map(str::to_string).collect(),
);
validate_stop(&stop).expect("four stop strings should be accepted");
}
#[test]
fn validate_stop_rejects_more_than_four_stop_strings() {
let stop = StringOrArray::Array(
["one", "two", "three", "four", "five"]
.into_iter()
.map(str::to_string)
.collect(),
);
let error = validate_stop(&stop).expect_err("five stop strings should be rejected");
assert_eq!(
error.code.as_ref(),
"stop strings must contain at most 4 items"
);
}
}
// ============================================================================
// Validation helpers
// ============================================================================