From 325fb870254938a4d78a2764fa00f21b52ef5005 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Mon, 26 Oct 2026 20:21:34 -0400 Subject: [PATCH] fix: send OpenAI developer and list-content system messages as Gemini systemInstruction Co-Authored-By: Claude Sonnet 5.5 --- internal/transformer/convert/openai_gemini.go | 8 +- .../convert/openai_gemini_system_test.go | 92 +++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 internal/transformer/convert/openai_gemini_system_test.go diff --git a/internal/transformer/convert/openai_gemini.go b/internal/transformer/convert/openai_gemini.go index b0b85f8..6d9e5ad 100644 --- a/internal/transformer/convert/openai_gemini.go +++ b/internal/transformer/convert/openai_gemini.go @@ -23,9 +23,11 @@ func OpenAIReqToGemini(openaiReq []byte, model string) ([]byte, error) { toolCallIDToName := make(map[string]string) // Map tool_call_id to function name for _, msg := range req.Messages { - if msg.Role == "system" { - if content, ok := msg.Content.(string); ok { - systemInstruction += content + "\n" + // Gemini only takes user and model turns in contents; instructions (OpenAI's + // "developer" role included, with a string or a list of text parts) go to systemInstruction. + if msg.Role == "system" || msg.Role == "developer" { + if text := extractSystemText(msg.Content); text != "" { + systemInstruction += text + "\n" } continue } diff --git a/internal/transformer/convert/openai_gemini_system_test.go b/internal/transformer/convert/openai_gemini_system_test.go new file mode 100644 index 0000000..0f67992 --- /dev/null +++ b/internal/transformer/convert/openai_gemini_system_test.go @@ -0,0 +1,92 @@ +package convert + +import ( + "encoding/json" + "testing" +) + +func openAIReqToGeminiBody(t *testing.T, req string) map[string]interface{} { + t.Helper() + out, err := OpenAIReqToGemini([]byte(req), "gemini-test") + if err != nil { + t.Fatalf("OpenAIReqToGemini failed: %v", err) + } + var body map[string]interface{} + if err := json.Unmarshal(out, &body); err != nil { + t.Fatalf("converted request is not JSON: %v", err) + } + return body +} + +func systemInstructionText(t *testing.T, body map[string]interface{}) string { + t.Helper() + si, ok := body["systemInstruction"].(map[string]interface{}) + if !ok { + t.Fatalf("expected a systemInstruction, got %v", body["systemInstruction"]) + } + parts, _ := si["parts"].([]interface{}) + if len(parts) != 1 { + t.Fatalf("expected one systemInstruction part, got %v", parts) + } + return parts[0].(map[string]interface{})["text"].(string) +} + +// Gemini only accepts the roles user and model in contents. +func assertOnlyGeminiRoles(t *testing.T, body map[string]interface{}) { + t.Helper() + contents, _ := body["contents"].([]interface{}) + for _, c := range contents { + role := c.(map[string]interface{})["role"] + if role != "user" && role != "model" { + t.Errorf("Gemini contents carry the unsupported role %v", role) + } + } +} + +func TestOpenAIReqToGeminiDeveloperMessageBecomesSystemInstruction(t *testing.T) { + body := openAIReqToGeminiBody(t, `{ + "model": "gpt-test", + "messages": [ + {"role": "developer", "content": "Answer in French."}, + {"role": "user", "content": "hi"} + ] + }`) + + if got := systemInstructionText(t, body); got != "Answer in French." { + t.Errorf("expected the developer message as systemInstruction, got %q", got) + } + assertOnlyGeminiRoles(t, body) + if contents := body["contents"].([]interface{}); len(contents) != 1 { + t.Errorf("expected only the user message in contents, got %v", contents) + } +} + +func TestOpenAIReqToGeminiSystemMessageWithContentPartsIsKept(t *testing.T) { + body := openAIReqToGeminiBody(t, `{ + "model": "gpt-test", + "messages": [ + {"role": "system", "content": [{"type": "text", "text": "Be brief."}, {"type": "text", "text": "No emojis."}]}, + {"role": "user", "content": "hi"} + ] + }`) + + if got := systemInstructionText(t, body); got != "Be brief.\nNo emojis." { + t.Errorf("expected the system parts joined, got %q", got) + } + assertOnlyGeminiRoles(t, body) +} + +func TestOpenAIReqToGeminiJoinsSystemAndDeveloperMessages(t *testing.T) { + body := openAIReqToGeminiBody(t, `{ + "model": "gpt-test", + "messages": [ + {"role": "system", "content": "Be brief."}, + {"role": "developer", "content": [{"type": "text", "text": "No emojis."}]}, + {"role": "user", "content": "hi"} + ] + }`) + + if got := systemInstructionText(t, body); got != "Be brief.\nNo emojis." { + t.Errorf("expected both instructions in order, got %q", got) + } +}