From 65107c90b10d2719b4739277ed5c06612d3180a4 Mon Sep 17 00:00:00 2001 From: marius851000 Date: Wed, 6 May 2026 15:03:00 +0200 Subject: [PATCH] ollama: Fix thinking being also sent as content (#55540) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "msg.string_contents();" return more than just content. It also return tool result (which need special handling, and should be emitted from the tool role) and thinking (which need already implemented special handling). This resulted in thinking being sent as content, which apparently ollama’s gemma4 parser didn’t handled well, and caused a number of issue that manifested by 1. outputting end of though token where unappropriate and 2. repeating things endlessly Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [ ] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) (no UI/UX impact) - [ ] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable (a quick review, without benchmark, show that this should be at least as fast as the previous code. This is called only exceptionally anyway.) Closes #55537 Release Notes: - Fixed "thinking" text being badly formatted when sent to Ollama --- crates/language_models/src/provider/ollama.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/language_models/src/provider/ollama.rs b/crates/language_models/src/provider/ollama.rs index d117bce3784b6e375e77945cb336d93b01d43336..df9f8f383e0a28a15bd9dec03f0297a3146e7f74 100644 --- a/crates/language_models/src/provider/ollama.rs +++ b/crates/language_models/src/provider/ollama.rs @@ -381,11 +381,14 @@ impl OllamaLanguageModel { } } Role::Assistant => { - let content = msg.string_contents(); + let mut text_content = String::new(); let mut thinking = None; let mut tool_calls = Vec::new(); for content in msg.content.into_iter() { match content { + MessageContent::Text(text) => { + text_content.push_str(&text); + } MessageContent::Thinking { text, .. } if !text.is_empty() => { thinking = Some(text) } @@ -402,7 +405,7 @@ impl OllamaLanguageModel { } } messages.push(ChatMessage::Assistant { - content, + content: text_content, tool_calls: Some(tool_calls), images: if images.is_empty() { None