[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
@@ -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