[Rust Frontend] Fix GLM-5.2 chat template rendering parity (#51426)
Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: OpenAI Codex <codex@openai.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: Woosuk Kwon <woosuk@inferact.ai>
This commit is contained in:
co-authored by
Bugen Zhao
OpenAI Codex
parent
d5f5de7a7d
commit
e0e5a7fb28
Generated
+4
-4
@@ -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",
|
||||
|
||||
+2
-2
@@ -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 }
|
||||
|
||||
@@ -290,6 +290,7 @@ struct TemplateToolDefinition {
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
parameters: JsonValue,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
strict: Option<bool>,
|
||||
}
|
||||
|
||||
@@ -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![
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -185,6 +185,19 @@ impl RoundtripCase {
|
||||
}
|
||||
}
|
||||
|
||||
/// GLM-5.2 XML-like argument format with `<think>` 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],
|
||||
|
||||
Reference in New Issue
Block a user