From a581838082d8b11f66d42f6518be793a403c77d7 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 16 Aug 2026 00:23:47 +0800 Subject: [PATCH] fix(gemini): simplify OpenAI chat response content and choices serialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Emit plain text directly for Gemini→OpenAI chat responses when the message contains only one text part and no reasoning/tool/image content. - Replace `choices` assignment via `JoinRawArray` with `translatorcommon.SetRawArrayItems` for direct raw-array insertion. --- .../openai/chat-completions/gemini_openai_response.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go b/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go index f9a9808f..476e5dab 100644 --- a/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go +++ b/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go @@ -409,7 +409,11 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina } if hasTextContent { - choiceTemplate, _ = sjson.SetBytes(choiceTemplate, "message.content", textContent.String()) + if !hasReasoningContent && len(partsResults) == 1 && len(toolCalls) == 0 && len(images) == 0 { + choiceTemplate, _ = sjson.SetBytes(choiceTemplate, "message.content", partsResults[0].Get("text").String()) + } else { + choiceTemplate, _ = sjson.SetBytes(choiceTemplate, "message.content", textContent.String()) + } } if hasReasoningContent { choiceTemplate, _ = sjson.SetBytes(choiceTemplate, "message.reasoning_content", reasoningContent.String()) @@ -432,7 +436,7 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina return true }) if len(choicesList) > 0 { - template, _ = sjson.SetRawBytes(template, "choices", translatorcommon.JoinRawArray(choicesList)) + template = translatorcommon.SetRawArrayItems(template, "choices", choicesList) } } -- 2.51.2