From e0e5a7fb2808504ba86c94f7b379e38496002fd0 Mon Sep 17 00:00:00 2001 From: Woosuk Kwon Date: Mon, 17 Aug 2026 20:16:54 -0700 Subject: [PATCH] [Rust Frontend] Fix GLM-5.2 chat template rendering parity (#51426) Co-authored-by: Bugen Zhao Co-authored-by: OpenAI Codex Signed-off-by: Bugen Zhao Signed-off-by: Woosuk Kwon --- rust/Cargo.lock | 8 +-- rust/Cargo.toml | 4 +- rust/src/chat/src/renderer/hf/mod.rs | 66 +++++++++++++++++++++++ rust/src/chat/src/renderer/hf/template.rs | 20 +++++++ rust/src/chat/tests/roundtrip.rs | 14 +++++ 5 files changed, 106 insertions(+), 6 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 092a798431..6262bf2e78 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2402,9 +2402,9 @@ dependencies = [ [[package]] name = "minijinja" -version = "2.22.0" +version = "2.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef84a52be188a1d4124bd717903fdde96ca4705f2b56adfe2d91fcc57fcb6987" +checksum = "86886cf6dbf4e614b19c9a1eec9775f021869d7eadde0fc73921a81b90c9b4c9" dependencies = [ "indexmap 2.13.0", "memo-map", @@ -2414,9 +2414,9 @@ dependencies = [ [[package]] name = "minijinja-contrib" -version = "2.22.0" +version = "2.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6e279dc925840c2d9ebc1e9f85410611d01292561297463ba3e516c3ad94dd" +checksum = "bd3e5f077bc2379f0f7d911e7cfdd921114ed99fc884533dca502944cb355b11" dependencies = [ "minijinja", "serde", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 405d6775bc..723e34806f 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -61,8 +61,8 @@ itertools = "0.14.0" libc = "0.2.177" llm-multimodal = { git = "https://github.com/smg-project/llm-multimodal", rev = "15adba5e025d8636ba4a334fb379b1371f6196a1", default-features = false, features = ["native-tls"] } mimalloc = "0.1.52" -minijinja = { version = "2.22", features = ["unstable_machinery", "json", "builtins", "loader", "loop_controls", "preserve_order"] } -minijinja-contrib = { version = "2.22", features = ["pycompat"] } +minijinja = { version = "2.24", features = ["unstable_machinery", "json", "builtins", "loader", "loop_controls", "preserve_order"] } +minijinja-contrib = { version = "2.24", features = ["pycompat"] } native-tls-vendored = { package = "native-tls", version = "0.2.18", features = ["vendored"] } ndarray = { version = "0.17", features = ["serde"] } openai-harmony = { package = "oss-harmony", git = "https://github.com/oss-harmony/harmony", tag = "v0.0.11", default-features = false } diff --git a/rust/src/chat/src/renderer/hf/mod.rs b/rust/src/chat/src/renderer/hf/mod.rs index df0d94331a..7461acf295 100644 --- a/rust/src/chat/src/renderer/hf/mod.rs +++ b/rust/src/chat/src/renderer/hf/mod.rs @@ -290,6 +290,7 @@ struct TemplateToolDefinition { name: String, description: Option, parameters: JsonValue, + #[serde(skip_serializing_if = "Option::is_none")] strict: Option, } @@ -1280,6 +1281,71 @@ mod tests { assert_eq!(rendered, "get_weather|city"); } + #[test] + fn chat_template_preserves_openai_tool_field_order() { + let mut request = sample_request(vec![ChatMessage::text(ChatRole::User, "hello")]); + let tools = vec![ChatTool { + name: "get_weather".to_string(), + description: Some("Get weather".to_string()), + parameters: serde_json::json!({"type": "object"}), + strict: None, + }]; + request.tool_context = crate::request::ResolvedToolContext::new( + &request.messages, + tools, + Some(ChatToolChoice::Auto), + true, + ) + .expect("tool context should resolve"); + + let rendered = render( + Some("{% for key, value in tools[0].function.items() %}{{ key }}|{% endfor %}"), + &request, + ) + .unwrap(); + + assert_eq!(rendered, "name|description|parameters|"); + } + + #[test] + fn chat_template_preserves_python_optional_tool_fields() { + let mut request = sample_request(vec![ChatMessage::text(ChatRole::User, "hello")]); + let tools = vec![ + ChatTool { + name: "without_strict".to_string(), + description: None, + parameters: Value::Null, + strict: None, + }, + ChatTool { + name: "with_strict".to_string(), + description: Some("description".to_string()), + parameters: serde_json::json!({"type": "object"}), + strict: Some(false), + }, + ]; + request.tool_context = crate::request::ResolvedToolContext::new( + &request.messages, + tools, + Some(ChatToolChoice::Auto), + true, + ) + .expect("tool context should resolve"); + + let rendered = render( + Some( + "{% for tool in tools %}{% for key, value in tool.function.items() %}{{ key }}={{ value|tojson }}|{% endfor %};{% endfor %}", + ), + &request, + ) + .unwrap(); + + assert_eq!( + rendered, + "name=\"without_strict\"|description=null|parameters=null|;name=\"with_strict\"|description=\"description\"|parameters={\"type\": \"object\"}|strict=false|;" + ); + } + #[test] fn chat_template_exposes_assistant_tool_calls_and_tool_messages() { let request = sample_request(vec![ diff --git a/rust/src/chat/src/renderer/hf/template.rs b/rust/src/chat/src/renderer/hf/template.rs index 5a609621cc..b7de0172f3 100644 --- a/rust/src/chat/src/renderer/hf/template.rs +++ b/rust/src/chat/src/renderer/hf/template.rs @@ -152,6 +152,26 @@ mod tests { assert_eq!(result, "[]"); } + #[test] + fn test_midchain_dotted_integer_lookup() { + let template = CompiledChatTemplate::new( + "{{ values.0.name }}".to_string(), + ChatTemplateContentFormatOption::Auto, + ) + .unwrap(); + let mut kwargs = HashMap::new(); + kwargs.insert("values".to_string(), serde_json::json!([{"name": "first"}])); + + let result = template + .apply(TemplateContext { + template_kwargs: Some(&kwargs), + ..Default::default() + }) + .unwrap(); + + assert_eq!(result, "first"); + } + #[test] fn test_chat_template_state_invalid_template() { let result = CompiledChatTemplate::new( diff --git a/rust/src/chat/tests/roundtrip.rs b/rust/src/chat/tests/roundtrip.rs index f8c3519bbf..049dc6a403 100644 --- a/rust/src/chat/tests/roundtrip.rs +++ b/rust/src/chat/tests/roundtrip.rs @@ -185,6 +185,19 @@ impl RoundtripCase { } } + /// GLM-5.2 XML-like argument format with `` reasoning tags. + fn glm52() -> Self { + Self { + model_id: "zai-org/GLM-5.2-FP8", + assistant_stop_suffix: "", + tool_call_parser: ParserSelection::Auto, + reasoning_parser: ParserSelection::Auto, + thinking_behavior: ThinkingBehavior::Toggleable { default: true }, + json_fmt: compact_json_fmt(), + sort_json_keys: false, + } + } + /// Gemma4 channel reasoning with custom function-call arguments. fn gemma4() -> Self { Self { @@ -324,6 +337,7 @@ roundtrip_tests! { deepseek_v32 => [tool_call_mix], glm45 => [reasoning_and_content, tool_call_mix], glm47 => [reasoning_and_content, tool_call_mix], + glm52 => [reasoning_and_content, tool_call_mix], seed_oss => [reasoning_and_content, tool_call_mix], step3p5 => [reasoning_and_content], nemotron_v3 => [reasoning_and_content],