Skip to main content

dynamo_protocols/validation/
openai.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! OpenAI baseline contract.
5//!
6//! Sources: [Chat Completions](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create)
7//! and [Completions](https://developers.openai.com/api/reference/resources/completions/methods/create),
8//! read on 2026-10-21. These are documented bounds, not hosted-server probes.
9//! All bounds are inclusive.
10
11use super::{ValidationError, check};
12use crate::types::{CreateChatCompletionRequest, CreateCompletionRequest};
13
14/// Lower bound for `temperature`.
15pub const MIN_TEMPERATURE: f32 = 0.0;
16/// Upper bound for `temperature`.
17pub const MAX_TEMPERATURE: f32 = 2.0;
18/// Lower bound for `top_p`.
19pub const MIN_TOP_P: f32 = 0.0;
20/// Upper bound for `top_p`.
21pub const MAX_TOP_P: f32 = 1.0;
22/// Lower bound for `frequency_penalty`.
23pub const MIN_FREQUENCY_PENALTY: f32 = -2.0;
24/// Upper bound for `frequency_penalty`.
25pub const MAX_FREQUENCY_PENALTY: f32 = 2.0;
26/// Lower bound for `presence_penalty`.
27pub const MIN_PRESENCE_PENALTY: f32 = -2.0;
28/// Upper bound for `presence_penalty`.
29pub const MAX_PRESENCE_PENALTY: f32 = 2.0;
30/// Lower bound for the integer completion `logprobs`.
31pub const MIN_LOGPROBS: u8 = 0;
32/// Upper bound for the integer completion `logprobs`.
33pub const MAX_LOGPROBS: u8 = 5;
34/// Lower bound for chat `top_logprobs`.
35pub const MIN_TOP_LOGPROBS: u8 = 0;
36/// Upper bound for chat `top_logprobs`.
37pub const MAX_TOP_LOGPROBS: u8 = 20;
38/// Lower bound for `n`.
39pub const MIN_N: u8 = 1;
40/// Upper bound for `n`.
41pub const MAX_N: u8 = 128;
42/// Lower bound for completion `best_of`.
43pub const MIN_BEST_OF: u8 = 0;
44/// Upper bound for completion `best_of`.
45pub const MAX_BEST_OF: u8 = 20;
46/// Lower bound for one `logit_bias` value.
47pub const MIN_LOGIT_BIAS: f32 = -100.0;
48/// Upper bound for one `logit_bias` value.
49pub const MAX_LOGIT_BIAS: f32 = 100.0;
50
51/// Accepts `None` or `temperature` in [0, 2].
52pub fn validate_temperature(value: Option<f32>) -> Result<(), ValidationError> {
53    check(
54        value,
55        |v| (MIN_TEMPERATURE..=MAX_TEMPERATURE).contains(&v),
56        "temperature",
57        || format!("Temperature must be between {MIN_TEMPERATURE} and {MAX_TEMPERATURE}"),
58    )
59}
60
61/// Accepts `None` or `top_p` in [0, 1].
62pub fn validate_top_p(value: Option<f32>) -> Result<(), ValidationError> {
63    check(
64        value,
65        |v| (MIN_TOP_P..=MAX_TOP_P).contains(&v),
66        "top_p",
67        || format!("Top_p must be between {MIN_TOP_P} and {MAX_TOP_P}"),
68    )
69}
70
71/// Accepts `None` or `frequency_penalty` in [-2, 2].
72pub fn validate_frequency_penalty(value: Option<f32>) -> Result<(), ValidationError> {
73    check(
74        value,
75        |v| (MIN_FREQUENCY_PENALTY..=MAX_FREQUENCY_PENALTY).contains(&v),
76        "frequency_penalty",
77        || {
78            format!(
79                "Frequency penalty must be between {MIN_FREQUENCY_PENALTY} and {MAX_FREQUENCY_PENALTY}"
80            )
81        },
82    )
83}
84
85/// Accepts `None` or `presence_penalty` in [-2, 2].
86pub fn validate_presence_penalty(value: Option<f32>) -> Result<(), ValidationError> {
87    check(
88        value,
89        |v| (MIN_PRESENCE_PENALTY..=MAX_PRESENCE_PENALTY).contains(&v),
90        "presence_penalty",
91        || {
92            format!(
93                "Presence penalty must be between {MIN_PRESENCE_PENALTY} and {MAX_PRESENCE_PENALTY}"
94            )
95        },
96    )
97}
98
99/// Accepts `None` or the integer `/v1/completions` `logprobs` in 0..=5.
100///
101/// This is not chat's boolean `logprobs` field.
102pub fn validate_completion_logprobs(value: Option<u8>) -> Result<(), ValidationError> {
103    check(
104        value,
105        |v| (MIN_LOGPROBS..=MAX_LOGPROBS).contains(&v),
106        "logprobs",
107        || format!("Logprobs must be between {MIN_LOGPROBS} and {MAX_LOGPROBS}"),
108    )
109}
110
111/// Accepts `None` or chat `top_logprobs` in 0..=20.
112///
113/// The relationship to chat's boolean `logprobs` field is not checked.
114pub fn validate_top_logprobs(value: Option<u8>) -> Result<(), ValidationError> {
115    check(
116        value,
117        |v| (MIN_TOP_LOGPROBS..=MAX_TOP_LOGPROBS).contains(&v),
118        "top_logprobs",
119        || format!("Top_logprobs must be between {MIN_TOP_LOGPROBS} and {MAX_TOP_LOGPROBS}"),
120    )
121}
122
123/// Accepts `None` or `n` in 1..=128.
124///
125/// Batch totals and sampling interactions are not checked.
126pub fn validate_n(value: Option<u8>) -> Result<(), ValidationError> {
127    check(
128        value,
129        |v| (MIN_N..=MAX_N).contains(&v),
130        "n",
131        || format!("n must be between {MIN_N} and {MAX_N}"),
132    )
133}
134
135/// Accepts `None` or completion `best_of` in 0..=20.
136///
137/// Checks only the count. Relationships to `n` and streaming are not checked.
138pub fn validate_best_of(value: Option<u8>) -> Result<(), ValidationError> {
139    check(
140        value,
141        |v| (MIN_BEST_OF..=MAX_BEST_OF).contains(&v),
142        "best_of",
143        || format!("Best_of must be between {MIN_BEST_OF} and {MAX_BEST_OF}"),
144    )
145}
146
147/// Accepts one parsed `logit_bias` value in [-100, 100], without clamping.
148///
149/// Unlike the other helpers, this takes one map entry's value, not an `Option`.
150/// The caller owns map traversal, token-ID policy, JSON number admission, and
151/// precision conversion. The error value is this `f32`'s `Display`, not the JSON text.
152pub fn validate_logit_bias_value(value: f32) -> Result<(), ValidationError> {
153    check(
154        Some(value),
155        |v| (MIN_LOGIT_BIAS..=MAX_LOGIT_BIAS).contains(&v),
156        "logit_bias",
157        || format!("Logit bias must be between {MIN_LOGIT_BIAS} and {MAX_LOGIT_BIAS}"),
158    )
159}
160
161/// Covered OpenAI request checks.
162///
163/// Order: `temperature`, `top_p`, `frequency_penalty`, `presence_penalty`,
164/// completion `logprobs` (completions only), `n`, then chat `top_logprobs` or
165/// completion `best_of`. All other fields and cross-field rules are uncovered.
166pub trait OpenAIValidation {
167    /// Returns the first covered failure, without mutation.
168    fn validate(&self) -> Result<(), ValidationError>;
169}
170
171impl OpenAIValidation for CreateChatCompletionRequest {
172    fn validate(&self) -> Result<(), ValidationError> {
173        validate_temperature(self.temperature)?;
174        validate_top_p(self.top_p)?;
175        validate_frequency_penalty(self.frequency_penalty)?;
176        validate_presence_penalty(self.presence_penalty)?;
177        validate_n(self.n)?;
178        validate_top_logprobs(self.top_logprobs)
179    }
180}
181
182impl OpenAIValidation for CreateCompletionRequest {
183    fn validate(&self) -> Result<(), ValidationError> {
184        validate_temperature(self.temperature)?;
185        validate_top_p(self.top_p)?;
186        validate_frequency_penalty(self.frequency_penalty)?;
187        validate_presence_penalty(self.presence_penalty)?;
188        validate_completion_logprobs(self.logprobs)?;
189        validate_n(self.n)?;
190        validate_best_of(self.best_of)
191    }
192}