From 29111304dde348a00fbceca71183bd30703175b8 Mon Sep 17 00:00:00 2001 From: Oleksandr Mykhailenko <58030797+armyhaylenko@users.noreply.github.com> Date: Sat, 19 Jul 2025 18:59:57 +0300 Subject: [PATCH] agent: Fix Mistral tool use error message (#34692) Closes #32675 Exactly the same changes as in #33640 by @sviande The PR has been in WIP state for 3 weeks with no activity, and the issue basically makes Mistral models unusable. I have tested the changes locally, and it does indeed work. Full credit goes to @sviande, I just want this feature to be finished. Release Notes: - agent: Fixed an issue with tool calling with the Mistral provider (thanks [@sviande](https://github.com/sviande) and [@armyhaylenko](https://github.com/armyhaylenko)) Co-authored-by: sviande --- .../language_models/src/provider/mistral.rs | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/crates/language_models/src/provider/mistral.rs b/crates/language_models/src/provider/mistral.rs index 11497fda35..fb385308fa 100644 --- a/crates/language_models/src/provider/mistral.rs +++ b/crates/language_models/src/provider/mistral.rs @@ -410,8 +410,20 @@ pub fn into_mistral( .push_part(mistral::MessagePart::Text { text: text.clone() }); } MessageContent::RedactedThinking(_) => {} - MessageContent::ToolUse(_) | MessageContent::ToolResult(_) => { - // Tool content is not supported in User messages for Mistral + MessageContent::ToolUse(_) => { + // Tool use is not supported in User messages for Mistral + } + MessageContent::ToolResult(tool_result) => { + let tool_content = match &tool_result.content { + LanguageModelToolResultContent::Text(text) => text.to_string(), + LanguageModelToolResultContent::Image(_) => { + "[Tool responded with an image, but Zed doesn't support these in Mistral models yet]".to_string() + } + }; + messages.push(mistral::RequestMessage::Tool { + content: tool_content, + tool_call_id: tool_result.tool_use_id.to_string(), + }); } } } @@ -482,24 +494,6 @@ pub fn into_mistral( } } - for message in &request.messages { - for content in &message.content { - if let MessageContent::ToolResult(tool_result) = content { - let content = match &tool_result.content { - LanguageModelToolResultContent::Text(text) => text.to_string(), - LanguageModelToolResultContent::Image(_) => { - "[Tool responded with an image, but Zed doesn't support these in Mistral models yet]".to_string() - } - }; - - messages.push(mistral::RequestMessage::Tool { - content, - tool_call_id: tool_result.tool_use_id.to_string(), - }); - } - } - } - // The Mistral API requires that tool messages be followed by assistant messages, // not user messages. When we have a tool->user sequence in the conversation, // we need to insert a placeholder assistant message to maintain proper conversation