Skip to content

Commit 160bf91

Browse files
Maokaman1bennetbo
andauthored
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: <img width="300" alt="Image" src="https://github.com/user-attachments/assets/c55e5272-3f03-42c0-b412-fa24be2b0043" /> Request JSON (causing the error): ``` { "model": "glm-4.6", "messages": [ { "role": "system", "content": "<<CUT>>" }, { "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 <[email protected]>
1 parent aff4c25 commit 160bf91

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

crates/language_models/src/provider/open_ai.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,13 @@ pub fn into_open_ai(
356356
for content in message.content {
357357
match content {
358358
MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
359-
add_message_content_part(
360-
open_ai::MessagePart::Text { text },
361-
message.role,
362-
&mut messages,
363-
)
359+
if !text.trim().is_empty() {
360+
add_message_content_part(
361+
open_ai::MessagePart::Text { text },
362+
message.role,
363+
&mut messages,
364+
);
365+
}
364366
}
365367
MessageContent::RedactedThinking(_) => {}
366368
MessageContent::Image(image) => {

0 commit comments

Comments
 (0)