From 0b1c151cbcacd12e207f2a91e4ca9f5b6961ec8a Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Tue, 4 Aug 2026 16:03:46 +0800 Subject: [PATCH] [Rust Frontend] Align tool rendering for Kimi K3 (#50540) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Signed-off-by: Bugen Zhao --- .../src/chat/src/renderer/kimi_k3/encoding.rs | 13 ++-- .../dynamic_system_tool_declare_input.json | 9 ++- .../dynamic_system_tool_declare_output.txt | 4 +- rust/src/chat/src/renderer/kimi_k3/tests.rs | 68 ++++++++++++++++++- 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/rust/src/chat/src/renderer/kimi_k3/encoding.rs b/rust/src/chat/src/renderer/kimi_k3/encoding.rs index 242c1e84c2..b2958b3d84 100644 --- a/rust/src/chat/src/renderer/kimi_k3/encoding.rs +++ b/rust/src/chat/src/renderer/kimi_k3/encoding.rs @@ -291,12 +291,17 @@ fn write_tool_declare( let mut specs = Vec::with_capacity(tools.len()); for tool in tools { let mut function = Map::new(); - function.insert( - "description".to_string(), - Value::String(tool.description.clone().unwrap_or_default()), - ); + if let Some(description) = &tool.description { + function.insert( + "description".to_string(), + Value::String(description.clone()), + ); + } function.insert("name".to_string(), Value::String(tool.name.clone())); function.insert("parameters".to_string(), sort_json(&tool.parameters)); + if let Some(strict) = tool.strict { + function.insert("strict".to_string(), Value::Bool(strict)); + } specs.push(json!({ "function": Value::Object(function), "type": "function", diff --git a/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_input.json b/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_input.json index 61ecebc268..fc0a5aa1c5 100644 --- a/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_input.json +++ b/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_input.json @@ -5,6 +5,7 @@ "function": { "name": "get_weather", "description": "weather", + "strict": true, "parameters": { "type": "object", "properties": { @@ -34,12 +35,16 @@ "type": "function", "function": { "name": "calc", - "description": "calculator", + "strict": false, "parameters": { "type": "object", "properties": { "x": { - "type": "number" + "type": [ + "number", + "null" + ], + "default": null } }, "required": [ diff --git a/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_output.txt b/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_output.txt index edec2bebf2..f82ff9f7f1 100644 --- a/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_output.txt +++ b/rust/src/chat/src/renderer/kimi_k3/fixtures/dynamic_system_tool_declare_output.txt @@ -2,7 +2,7 @@ Here are the available tools, described in JSONSchema. ```json -[{"function":{"description":"weather","name":"get_weather","parameters":{"properties":{"city":{"type":"string"},"days":{"type":"number"}},"required":["city"],"type":"object"}},"type":"function"}] +[{"function":{"description":"weather","name":"get_weather","parameters":{"properties":{"city":{"type":"string"},"days":{"type":"number"}},"required":["city"],"type":"object"},"strict":true},"type":"function"}] ```<|close|>message<|sep|><|end_of_msg|><|open|>message role="system" type="thinking-effort"<|sep|>`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low`, `medium`, `high`, and `max`. Now the system is invoked with `thinking_effort=max`.<|close|>message<|sep|><|end_of_msg|><|open|>message role="user"<|sep|>hi<|close|>message<|sep|><|end_of_msg|><|open|>message role="system" type="tool-declare"<|sep|>## New Tools Available The system dynamically extends the toolset via lazy-loading. @@ -10,5 +10,5 @@ You have access to all existing and extended tools. Here are the specs for the extended tools. ```json -[{"function":{"description":"calculator","name":"calc","parameters":{"properties":{"x":{"type":"number"}},"required":["x"],"type":"object"}},"type":"function"}] +[{"function":{"name":"calc","parameters":{"properties":{"x":{"default":null,"type":["number","null"]}},"required":["x"],"type":"object"},"strict":false},"type":"function"}] ```<|close|>message<|sep|><|end_of_msg|><|open|>message role="user"<|sep|>use calc<|close|>message<|sep|><|end_of_msg|><|open|>message role="assistant"<|sep|><|open|>think<|sep|> \ No newline at end of file diff --git a/rust/src/chat/src/renderer/kimi_k3/tests.rs b/rust/src/chat/src/renderer/kimi_k3/tests.rs index d1fd6581f5..8b2799cdb3 100644 --- a/rust/src/chat/src/renderer/kimi_k3/tests.rs +++ b/rust/src/chat/src/renderer/kimi_k3/tests.rs @@ -17,7 +17,9 @@ use super::KimiK3ChatRenderer; use crate::ChatRenderer; use crate::renderer::kimi_k3::encoding::{CLOSE, END_OF_MSG, IMAGE_PLACEHOLDER, OPEN, SEP}; use crate::renderer::test_utils::{FixtureRequestOptions, fixture_chat_request}; -use crate::request::{ChatContentPart, ChatMessage, GenerationPromptMode, ReasoningEffort}; +use crate::request::{ + ChatContentPart, ChatMessage, ChatTool, GenerationPromptMode, ReasoningEffort, +}; use crate::{AssistantContentBlock, AssistantToolCall}; const OPEN_ID: u32 = 256; @@ -90,6 +92,70 @@ fn golden_dynamic_system_tool_declare() { assert_golden("dynamic_system_tool_declare"); } +#[test] +fn tool_declare_omits_absent_fields_and_preserves_parameter_nulls() { + let mut request = crate::request::ChatRequest::for_test(); + request.tools = vec![ChatTool { + name: "lookup".to_string(), + description: None, + parameters: json!({ + "type": "object", + "properties": { + "limit": { + "type": ["integer", "null"], + "default": null + } + } + }), + strict: None, + }]; + + let rendered = render_request(&request); + + assert!(rendered.contains( + r#"[{"function":{"name":"lookup","parameters":{"properties":{"limit":{"default":null,"type":["integer","null"]}},"type":"object"}},"type":"function"}]"# + )); + assert!(!rendered.contains(r#""description":"#)); + assert!(!rendered.contains(r#""strict":"#)); +} + +#[test] +fn tool_declare_preserves_present_optional_fields() { + let mut request = crate::request::ChatRequest::for_test(); + request.tools = vec![ChatTool { + name: "lookup".to_string(), + description: Some("Look up a record".to_string()), + parameters: json!({"type": "object"}), + strict: Some(false), + }]; + + let rendered = render_request(&request); + + assert!(rendered.contains( + r#"[{"function":{"description":"Look up a record","name":"lookup","parameters":{"type":"object"},"strict":false},"type":"function"}]"# + )); +} + +#[test] +fn malformed_tool_arguments_render_as_raw_text() { + let malformed = " {\"city\":\"Beijing\" "; + let mut request = crate::request::ChatRequest::for_test(); + request.messages = vec![ChatMessage::assistant_blocks(vec![ + AssistantContentBlock::ToolCall(AssistantToolCall { + id: "call-1".to_string(), + name: "weather".to_string(), + arguments: malformed.to_string(), + }), + ])]; + request.chat_options.generation_prompt_mode = GenerationPromptMode::NoGenerationPrompt; + + let rendered = render_request(&request); + + assert!(rendered.contains(&format!( + "<|open|>json type=\"object\"<|sep|>{malformed}<|close|>json<|sep|>" + ))); +} + #[test] fn token_writer_protects_literal_control_and_media_markers() { let tokenizer = Arc::new(test_tokenizer());