diff --git a/rust/src/chat/src/output/default/tool.rs b/rust/src/chat/src/output/default/tool.rs index 9774f64381..8969667530 100644 --- a/rust/src/chat/src/output/default/tool.rs +++ b/rust/src/chat/src/output/default/tool.rs @@ -121,7 +121,11 @@ impl ToolState { None => true, }; if is_new_tool { - let id = generate_tool_call_id(); + let id = self + .parser + .tool_call_id(item.tool_index) + .map(str::to_string) + .unwrap_or_else(generate_tool_call_id); self.open_call_index = Some(item.tool_index); events.push(AssistantEvent::ToolCallStart { id, name }); } @@ -291,6 +295,11 @@ mod tests { buffered: String, } + struct IdScriptedParser { + output: ToolParserOutput, + tool_call_id: Option, + } + impl ToolParser for FailingParser { fn create(_tools: &[ChatTool]) -> vllm_tool_parser::Result> where @@ -351,6 +360,35 @@ mod tests { } } + impl ToolParser for IdScriptedParser { + fn create(_tools: &[ChatTool]) -> vllm_tool_parser::Result> + where + Self: Sized + 'static, + { + Ok(Box::new(Self { + output: ToolParserOutput::default(), + tool_call_id: None, + })) + } + + fn tool_call_id(&self, tool_index: usize) -> Option<&str> { + (tool_index == 0).then_some(self.tool_call_id.as_deref()).flatten() + } + + fn parse_into(&mut self, _chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + output.append(std::mem::take(&mut self.output)); + Ok(()) + } + + fn finish(&mut self) -> Result { + Ok(ToolParserOutput::default()) + } + + fn reset(&mut self) -> String { + String::new() + } + } + impl ToolParser for PartialThenFailParser { fn create(_tools: &[ChatTool]) -> vllm_tool_parser::Result> where @@ -505,6 +543,70 @@ mod tests { assert!(matches!(events[3], AssistantEvent::Done { .. })); } + #[tokio::test] + async fn tool_stream_preserves_parser_provided_tool_call_id() { + let events = stream::iter(vec![Ok(ContentEvent::TextDelta { + kind: AssistantBlockKind::Text, + delta: "ignored".to_string(), + })]); + let parser = IdScriptedParser { + output: ToolParserOutput { + normal_text: String::new(), + calls: vec![crate::parser::tool::ToolCallDelta { + tool_index: 0, + name: Some("get_weather".to_string()), + arguments: "{}".to_string(), + }], + }, + tool_call_id: Some("functions.get_weather:0".to_string()), + }; + + let events = tool_event_stream(events, Some(Box::new(parser))) + .collect::>() + .await + .into_iter() + .collect::>>() + .unwrap(); + + assert!(matches!( + &events[0], + AssistantEvent::ToolCallStart { id, name } + if id == "functions.get_weather:0" && name == "get_weather" + )); + } + + #[tokio::test] + async fn tool_stream_generates_tool_call_id_when_parser_omits_one() { + let events = stream::iter(vec![Ok(ContentEvent::TextDelta { + kind: AssistantBlockKind::Text, + delta: "ignored".to_string(), + })]); + let parser = IdScriptedParser { + output: ToolParserOutput { + normal_text: String::new(), + calls: vec![crate::parser::tool::ToolCallDelta { + tool_index: 0, + name: Some("get_weather".to_string()), + arguments: "{}".to_string(), + }], + }, + tool_call_id: None, + }; + + let events = tool_event_stream(events, Some(Box::new(parser))) + .collect::>() + .await + .into_iter() + .collect::>>() + .unwrap(); + + assert!(matches!( + &events[0], + AssistantEvent::ToolCallStart { id, name } + if id.starts_with("call_") && name == "get_weather" + )); + } + #[tokio::test] async fn real_buffered_parser_error_matches_streaming_and_non_streaming() { let prefix = "I will check both.\n"; diff --git a/rust/src/chat/src/output/mod.rs b/rust/src/chat/src/output/mod.rs index 81ec124fbc..6dda8ba0da 100644 --- a/rust/src/chat/src/output/mod.rs +++ b/rust/src/chat/src/output/mod.rs @@ -128,8 +128,6 @@ trait_set! { /// Generate the northbound tool-call ID using the OpenAI-style `call_` /// format. -// TODO: support other ID scheme like Kimi-K2's -// `functions.{name}:{global_index}`. pub(crate) fn generate_tool_call_id() -> String { format!("call_{}", &Uuid::new_v4().simple().to_string()[..24]) } diff --git a/rust/src/chat/tests/roundtrip.rs b/rust/src/chat/tests/roundtrip.rs index ab2ca06cb3..110114cdf9 100644 --- a/rust/src/chat/tests/roundtrip.rs +++ b/rust/src/chat/tests/roundtrip.rs @@ -170,8 +170,7 @@ roundtrip_tests! { glm47 => [reasoning_and_content, tool_call_mix], // Note: Kimi K2.5 strips the reasoning content in history. - // TODO: we don't respect model-generated tool call id now so `tool_call_mix` cannot pass. - // kimi_k25 => [tool_call_mix], + kimi_k25 => [tool_call_mix], } /// Run the fixed reasoning+content fixture for one model/parser case. diff --git a/rust/src/tool-parser/src/kimi_k2.rs b/rust/src/tool-parser/src/kimi_k2.rs index e43ff4afc6..b639fdd5c3 100644 --- a/rust/src/tool-parser/src/kimi_k2.rs +++ b/rust/src/tool-parser/src/kimi_k2.rs @@ -1,3 +1,5 @@ +use std::collections::BTreeMap; + use winnow::ascii::{digit1, multispace0 as ws0}; use winnow::combinator::{alt, eof, repeat, seq}; use winnow::prelude::*; @@ -33,6 +35,7 @@ enum KimiK2Event { ToolCallsStart, ToolCallStart, ToolCallHeader { + tool_call_id: String, function_name: String, function_index: usize, }, @@ -60,6 +63,7 @@ pub struct KimiK2ToolParser { buffer: String, mode: KimiK2Mode, active_tool_index: Option, + call_ids: BTreeMap, } impl KimiK2ToolParser { @@ -69,6 +73,7 @@ impl KimiK2ToolParser { buffer: String::new(), mode: KimiK2Mode::Text, active_tool_index: None, + call_ids: BTreeMap::new(), } } @@ -81,6 +86,7 @@ impl KimiK2ToolParser { KimiK2Event::ToolCallsStart => self.mode = KimiK2Mode::ToolBlock, KimiK2Event::ToolCallStart => self.mode = KimiK2Mode::Header, KimiK2Event::ToolCallHeader { + tool_call_id, function_name, function_index, } => { @@ -89,6 +95,7 @@ impl KimiK2ToolParser { self.mode = KimiK2Mode::Arguments { json_scan: JsonObjectScanState::default(), }; + self.call_ids.insert(tool_index, tool_call_id); output.calls.push(ToolCallDelta { tool_index, name: Some(function_name), @@ -123,6 +130,7 @@ impl KimiK2ToolParser { fn reset(&mut self) -> String { self.mode = KimiK2Mode::Text; self.active_tool_index = None; + self.call_ids.clear(); std::mem::take(&mut self.buffer) } } @@ -139,6 +147,10 @@ impl ToolParser for KimiK2ToolParser { true } + fn tool_call_id(&self, tool_index: usize) -> Option<&str> { + self.call_ids.get(&tool_index).map(String::as_str) + } + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); @@ -232,16 +244,18 @@ fn tool_call_end_event(input: &mut KimiK2Input<'_>) -> ModalResult /// Parse a Kimi K2 tool-call header before the argument marker. fn tool_call_header_event(input: &mut KimiK2Input<'_>) -> ModalResult { - let (header, _) = ( + let (raw_header, _) = ( take_until(1.., TOOL_CALL_ARGUMENT_START), literal(TOOL_CALL_ARGUMENT_START), ) .parse_next(input)?; - let mut header_input = header; + let tool_call_id = raw_header.trim().to_string(); + let mut header_input = raw_header; let (header, _, _) = (tool_header, ws0, eof).parse_next(&mut header_input)?; Ok(KimiK2Event::ToolCallHeader { + tool_call_id, function_name: header.function_name, function_index: header.function_index, }) @@ -502,6 +516,25 @@ mod tests { .assert_debug_eq(&output); } + #[test] + fn kimi_k2_preserves_model_generated_tool_call_ids() { + let mut parser = KimiK2ToolParser::new(&test_tools()); + let input = build_tool_section(&[ + build_tool_call("get_weather", 0, r#"{"location":"Shanghai"}"#), + build_tool_call("add", 1, r#"{"x":1,"y":2}"#), + ]); + + for chunk in split_by_chars(&input, 7) { + parser.parse_chunk(chunk).unwrap(); + } + + // IDs are available after parsing but before finish(), which calls reset(). + assert_eq!(parser.tool_call_id(0), Some("functions.get_weather:0")); + assert_eq!(parser.tool_call_id(1), Some("functions.add:1")); + parser.finish().unwrap(); + assert_eq!(parser.tool_call_id(0), None); + } + #[test] fn kimi_k2_accepts_non_functions_header_prefix() { let mut parser = KimiK2ToolParser::new(&test_tools()); @@ -509,9 +542,10 @@ mod tests { "{TOOL_CALLS_START}{TOOL_CALL_START}api.tools.search:42{TOOL_CALL_ARGUMENT_START}{{}}{TOOL_CALL_END}{TOOL_CALLS_END}" ); - let output = parser.parse_complete(&input).unwrap(); + let output = parser.parse_chunk(&input).unwrap().coalesce_calls(); assert_eq!(output.calls[0].tool_index, 42); + assert_eq!(parser.tool_call_id(42), Some("api.tools.search:42")); assert_eq!(output.calls[0].name.as_deref(), Some("search")); assert_eq!(output.calls[0].arguments, "{}"); } diff --git a/rust/src/tool-parser/src/lib.rs b/rust/src/tool-parser/src/lib.rs index 5f9d6daaf4..6e77af7bcf 100644 --- a/rust/src/tool-parser/src/lib.rs +++ b/rust/src/tool-parser/src/lib.rs @@ -121,6 +121,12 @@ pub trait ToolParser: Send { false } + /// Return the parser-provided ID for a tool call by index, if the model + /// emitted one. + fn tool_call_id(&self, _tool_index: usize) -> Option<&str> { + None + } + /// Feed one decoded text delta into the parser, appending committed output /// into `output`. ///