From 160bf915aa4661dd3f5a0991be09bfb9cb2223df Mon Sep 17 00:00:00 2001 From: Maokaman1 Date: Fri, 7 Nov 2025 17:43:07 +0400 Subject: [PATCH] language_models: Filter out whitespace-only text content parts for OpenAI and OpenAI compatible providers (#40316) Closes #40097 When multiple files are added sequentially to the agent panel, the request JSON incorrectly includes "text" elements containing only spaces. These empty elements cause the Zhipu AI API to return a "text cannot be empty" error. The fix filters out any "text" elements that are empty or contain only whitespaces. UI state when the error occurs: Image Request JSON (causing the error): ``` { "model": "glm-4.6", "messages": [ { "role": "system", "content": "<>" }, { "role": "user", "content": [ { "type": "text", "text": "[@1.txt](zed:///agent/file?path=C%3A%5CTemp%5CTest%5C1.txt)" }, { "type": "text", "text": " " }, { "type": "text", "text": "[@2.txt](zed:///agent/file?path=C%3A%5CTemp%5CTest%5C2.txt)" }, { "type": "text", "text": " describe" }, ``` Release Notes: - Fixed an issue when an OpenAI request contained whitespace-only text content Co-authored-by: Bennet Bo Fenner --- crates/language_models/src/provider/open_ai.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs index 627c7aaea35f0af4deda409130d9ce217975115d..38bf373c6f0de19362560f2c906c3e24a0833cae 100644 --- a/crates/language_models/src/provider/open_ai.rs +++ b/crates/language_models/src/provider/open_ai.rs @@ -356,11 +356,13 @@ pub fn into_open_ai( for content in message.content { match content { MessageContent::Text(text) | MessageContent::Thinking { text, .. } => { - add_message_content_part( - open_ai::MessagePart::Text { text }, - message.role, - &mut messages, - ) + if !text.trim().is_empty() { + add_message_content_part( + open_ai::MessagePart::Text { text }, + message.role, + &mut messages, + ); + } } MessageContent::RedactedThinking(_) => {} MessageContent::Image(image) => {