fix: filter whitespace-only text in Claude to OpenAI translation

Skip text content blocks that are empty or contain only whitespace
when translating Claude messages to OpenAI format. This fixes GLM-4.6
and other strict OpenAI-compatible providers that reject empty text
with error 'text cannot be empty'.
This commit is contained in:
huynguyen03.dev
2025-12-07 15:39:58 +07:00
parent 1b638b3629
commit f092801b61

View File

@@ -8,6 +8,7 @@ package claude
import (
"bytes"
"encoding/json"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
@@ -245,8 +246,12 @@ func convertClaudeContentPart(part gjson.Result) (string, bool) {
if !part.Get("text").Exists() {
return "", false
}
text := part.Get("text").String()
if strings.TrimSpace(text) == "" {
return "", false
}
textContent := `{"type":"text","text":""}`
textContent, _ = sjson.Set(textContent, "text", part.Get("text").String())
textContent, _ = sjson.Set(textContent, "text", text)
return textContent, true
case "image":