Skip to main content

dynamo_protocols/validation/
sglang.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! SGLang contract: OpenAI reuse, shared engine operations, and SGLang overrides.
5//!
6//! Source: SGLang `5569b14d` [sampling checks](https://github.com/sgl-project/sglang/blob/5569b14d26ffd94a8326889eca15a153e8f1b68b/python/sglang/srt/sampling/sampling_params.py),
7//! [request conversion](https://github.com/sgl-project/sglang/blob/5569b14d26ffd94a8326889eca15a153e8f1b68b/python/sglang/srt/entrypoints/openai/protocol.py),
8//! and [verification](https://github.com/sgl-project/sglang/blob/5569b14d26ffd94a8326889eca15a153e8f1b68b/python/sglang/srt/managers/tokenizer_manager.py).
9//! This pin does not establish later revisions or HTTP conformance.
10//! The engine's pre-validation greedy rewrite is not implemented.
11
12use super::{ValidationError, check};
13use crate::types::{CreateChatCompletionRequest, CreateCompletionRequest};
14
15pub use super::openai::{
16    MAX_FREQUENCY_PENALTY, MAX_PRESENCE_PENALTY, MAX_TOP_P, MIN_FREQUENCY_PENALTY,
17    MIN_PRESENCE_PENALTY, MIN_TEMPERATURE, validate_frequency_penalty, validate_presence_penalty,
18};
19// Same as vLLM; differs from OpenAI or is outside it.
20pub use super::shared::{
21    MAX_MIN_P, MIN_MIN_P, MIN_TOP_P_EXCLUSIVE, validate_min_p, validate_top_p,
22};
23
24/// Accepts `None` or a finite `temperature` >= 0. SGLang has no upper bound.
25pub fn validate_temperature(value: Option<f32>) -> Result<(), ValidationError> {
26    check(
27        value,
28        |v| v.is_finite() && v >= MIN_TEMPERATURE,
29        "temperature",
30        || format!("Temperature must be finite and at least {MIN_TEMPERATURE}"),
31    )
32}
33
34/// Covered SGLang request checks.
35///
36/// Order: `temperature`, `top_p`, `frequency_penalty`, `presence_penalty`.
37/// All other fields and cross-field rules are uncovered.
38pub trait SglangValidation {
39    /// Returns the first covered failure, without mutation.
40    fn validate(&self) -> Result<(), ValidationError>;
41}
42
43impl SglangValidation for CreateChatCompletionRequest {
44    fn validate(&self) -> Result<(), ValidationError> {
45        validate_temperature(self.temperature)?;
46        validate_top_p(self.top_p)?;
47        validate_frequency_penalty(self.frequency_penalty)?;
48        validate_presence_penalty(self.presence_penalty)
49    }
50}
51
52impl SglangValidation for CreateCompletionRequest {
53    fn validate(&self) -> Result<(), ValidationError> {
54        validate_temperature(self.temperature)?;
55        validate_top_p(self.top_p)?;
56        validate_frequency_penalty(self.frequency_penalty)?;
57        validate_presence_penalty(self.presence_penalty)
58    }
59}