Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/transformer/convert/openai_gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
92 changes: 92 additions & 0 deletions internal/transformer/convert/openai_gemini_system_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading