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 <bennetbo@gmx.de>
This commit is contained in:
Maokaman1
2025-11-07 14:43:07 +01:00
committed by GitHub
co-authored by Bennet Bo Fenner
parent aff4c25a47
commit 160bf915aa
@@ -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) => {