-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: handle plain string content in OpenAI Responses → Gemini translation #1529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle plain string content in OpenAI Responses → Gemini translation #1529
Conversation
Summary of ChangesHello @happycastle114, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a bug where the translation from OpenAI Responses to Gemini requests failed when message content was provided as a plain string rather than an array. The changes enhance the translator to correctly interpret and convert both string and array content formats for system and regular messages, ensuring successful request translation and preventing errors in the Gemini payload. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request successfully addresses a bug by adding support for plain string content in OpenAI Responses to Gemini request translations, covering both system and regular messages. The inclusion of new tests to validate both string and array formats is a great addition. My review includes suggestions to refactor some duplicated code and remove redundant logic, which will improve the overall maintainability and clarity of the code.
| if content.IsArray() { | ||
| var builder strings.Builder | ||
| content.ForEach(func(_, contentItem gjson.Result) bool { | ||
| text := contentItem.Get("text").String() | ||
| if builder.Len() > 0 && text != "" { | ||
| builder.WriteByte('\n') | ||
| } | ||
| builder.WriteString(text) | ||
| return true | ||
| }) | ||
| if !gjson.Get(out, "system_instruction").Exists() { | ||
| systemInstr := `{"parts":[{"text":""}]}` | ||
| systemInstr, _ = sjson.Set(systemInstr, "parts.0.text", builder.String()) | ||
| out, _ = sjson.SetRaw(out, "system_instruction", systemInstr) | ||
| } | ||
| } else if content.Type == gjson.String { | ||
| if !gjson.Get(out, "system_instruction").Exists() { | ||
| systemInstr := `{"parts":[{"text":""}]}` | ||
| systemInstr, _ = sjson.Set(systemInstr, "parts.0.text", content.String()) | ||
| out, _ = sjson.SetRaw(out, "system_instruction", systemInstr) | ||
| } | ||
| builder.WriteString(text) | ||
| return true | ||
| }) | ||
| if !gjson.Get(out, "system_instruction").Exists() { | ||
| systemInstr := `{"parts":[{"text":""}]}` | ||
| systemInstr, _ = sjson.Set(systemInstr, "parts.0.text", builder.String()) | ||
| out, _ = sjson.SetRaw(out, "system_instruction", systemInstr) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some duplicated logic for setting the system_instruction. You can refactor this to be more DRY by extracting the text from either the string or array content first, and then using a single block to set the JSON value. This will make the code easier to read and maintain.
var systemText string
isSystemTextPresent := false
if content.IsArray() {
var builder strings.Builder
content.ForEach(func(_, contentItem gjson.Result) bool {
text := contentItem.Get("text").String()
if builder.Len() > 0 && text != "" {
builder.WriteByte('\n')
}
builder.WriteString(text)
return true
})
systemText = builder.String()
isSystemTextPresent = true
} else if content.Type == gjson.String {
systemText = content.String()
isSystemTextPresent = true
}
if isSystemTextPresent && !gjson.Get(out, "system_instruction").Exists() {
systemInstr := `{"parts":[{"text":""}]}`
systemInstr, _ = sjson.Set(systemInstr, "parts.0.text", systemText)
out, _ = sjson.SetRaw(out, "system_instruction", systemInstr)
}| if effRole == "assistant" { | ||
| effRole = "model" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if effRole == "assistant" { | ||
| effRole = "model" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
I fixed the issue in v6.8.11. Thank you for your contribution. This PR has been closed. |
Summary
Fix OpenAI Responses → Gemini request translation when message
contentis sent as a plain string (common in OpenAI SDKs, including Python), not just as an array.Bug
ConvertOpenAIResponsesRequestToGeminionly processed message content whencontentwas an array.For inputs like:
{"role":"system","content":"You are helpful"} {"role":"user","content":"Hello"}content was skipped, producing
contents: []in the Gemini payload and causing:Fix
1) System message handling
contentin system messages (in addition to array handling).contentis a string, it now populatessystem_instruction.parts[0].text.2) Regular message handling
contenthandling before existing array logic.contentis a string, it now creates a Geminicontentsitem directly with mapped role and a text part.3) Tests
gemini_openai-responses_request_test.gowith coverage for both formats:content(system+user)content(existing structured format)Reproduction
{ "model": "gpt-5", "input": [ {"role": "system", "content": "You are helpful"}, {"role": "user", "content": "Hello"} ] }contentsand request fails.system_instructionandcontentswith user text, and succeeds.Notes