Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/llm/src/protocols/openai/chat_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ impl OpenAIOutputOptionsProvider for NvCreateChatCompletionRequest {
impl ValidateRequest for NvCreateChatCompletionRequest {
fn validate(&self) -> Result<(), anyhow::Error> {
validate::validate_no_unsupported_fields(&self.unsupported_fields)?;
validate::validate_chat_template_args(self.chat_template_args.as_ref())?;
validate::validate_messages(&self.inner.messages)?;
validate::validate_model(&self.inner.model)?;
// none for store
Expand Down
30 changes: 30 additions & 0 deletions lib/llm/src/protocols/openai/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,19 @@ where
Ok(Some(value))
}

/// A nested `chat_template` bypasses Dynamo's top-level rejection and is
/// promoted into the rendered template, so block it for every chat processor.
pub fn validate_chat_template_args(
chat_template_args: Option<&std::collections::HashMap<String, serde_json::Value>>,
) -> Result<(), anyhow::Error> {
if let Some(args) = chat_template_args
&& args.contains_key("chat_template")
{
anyhow::bail!("`chat_template` is not supported inside `chat_template_args`");
}
Ok(())
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
Expand All @@ -816,6 +829,23 @@ mod tests {
HashMap::from([("experimental_field".to_string(), json!("value"))])
}

#[test]
fn validate_chat_template_args_rejects_nested_chat_template() {
let args = HashMap::from([(
"chat_template".to_string(),
json!("{% for _ in range(10**9) %}x{% endfor %}"),
)]);
let err = validate_chat_template_args(Some(&args)).unwrap_err();
assert!(err.to_string().contains("chat_template"));
}

#[test]
fn validate_chat_template_args_accepts_other_keys() {
let args = HashMap::from([("enable_thinking".to_string(), json!(false))]);
validate_chat_template_args(Some(&args)).unwrap();
validate_chat_template_args(None).unwrap();
}

#[test]
fn validate_no_unsupported_fields_rejects_unknown_fields_by_default() {
let err = validate_no_unsupported_fields_with_ignore(&unknown_fields(), false).unwrap_err();
Expand Down
Loading