fix(codex): preserve image content in tool outputs#2349
fix(codex): preserve image content in tool outputs#2349DarkNoah wants to merge 2 commits intorouter-for-me:mainfrom
Conversation
Ensure tool-role message content can be translated from structured arrays so image_url items are emitted as input_image parts instead of being dropped from function_call_output payloads.
Summary of ChangesHello, 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 resolves an issue in the Codex OpenAI request translation logic where structured array content, particularly 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. 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request enhances the ConvertOpenAIRequestToCodex function to support richer content in tool messages. Previously, tool message content was treated as a simple string; now, it can be an array containing text and image_url items, allowing for structured outputs. A new test case TestToolCallOutputWithImageContent was added to validate this functionality. A review comment suggests adding error handling for sjson.SetBytes and sjson.SetRawBytes calls to prevent silent failures and improve robustness.
| funcOutput, _ = sjson.SetBytes(funcOutput, "type", "function_call_output") | ||
| funcOutput, _ = sjson.SetBytes(funcOutput, "call_id", toolCallID) | ||
| funcOutput, _ = sjson.SetBytes(funcOutput, "output", content) | ||
|
|
||
| // Handle content: can be string or array (e.g., with image_url items) | ||
| if content.Type == gjson.String { | ||
| funcOutput, _ = sjson.SetBytes(funcOutput, "output", content.String()) | ||
| } else if content.IsArray() { | ||
| // Build output array from content items | ||
| outputArr := []byte(`[]`) | ||
| items := content.Array() | ||
| for j := 0; j < len(items); j++ { | ||
| it := items[j] | ||
| t := it.Get("type").String() | ||
| switch t { | ||
| case "text": | ||
| outputPart := []byte(`{}`) | ||
| outputPart, _ = sjson.SetBytes(outputPart, "type", "input_text") | ||
| outputPart, _ = sjson.SetBytes(outputPart, "text", it.Get("text").String()) | ||
| outputArr, _ = sjson.SetRawBytes(outputArr, "-1", outputPart) | ||
| case "image_url": | ||
| // Handle image_url content in tool message | ||
| if u := it.Get("image_url.url"); u.Exists() { | ||
| outputPart := []byte(`{}`) | ||
| outputPart, _ = sjson.SetBytes(outputPart, "type", "input_image") | ||
| outputPart, _ = sjson.SetBytes(outputPart, "image_url", u.String()) | ||
| outputArr, _ = sjson.SetRawBytes(outputArr, "-1", outputPart) | ||
| } | ||
| } | ||
| } | ||
| funcOutput, _ = sjson.SetRawBytes(funcOutput, "output", outputArr) | ||
| } | ||
|
|
||
| out, _ = sjson.SetRawBytes(out, "input.-1", funcOutput) |
There was a problem hiding this comment.
Throughout this switch block, errors returned by sjson.SetBytes and sjson.SetRawBytes are consistently ignored. While sjson is designed to be robust, silently discarding errors can lead to malformed JSON output if unexpected input data or paths are encountered. This can result in incorrect API requests being sent without any indication of a problem.
It is highly recommended to implement error handling for these operations, at least by logging the errors. For example, you could change funcOutput, _ = sjson.SetBytes(...) to funcOutput, err := sjson.SetBytes(...); if err != nil { log.Printf("sjson error: %v", err) } to ensure that any issues during JSON construction are visible and can be debugged. This improves the robustness and maintainability of the translation logic.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 38cf661329
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } | ||
| } | ||
| } | ||
| funcOutput, _ = sjson.SetRawBytes(funcOutput, "output", outputArr) |
There was a problem hiding this comment.
Fall back when tool content array has unknown part types
This branch always rewrites array-based tool content into outputArr and then assigns it to output, but the loop only maps text and image_url parts. Any other part type is silently dropped, and if none match the resulting function_call_output.output becomes [], losing the tool payload entirely. That is a regression from the previous behavior (which preserved the original content as a string) and will break tool calls that return other structured parts (for example file/audio/custom parts) because the model no longer receives that output.
Useful? React with 👍 / 👎.
xkonjin
left a comment
There was a problem hiding this comment.
Nice fix overall, especially the new coverage for mixed text + image tool outputs. One correctness edge case still worries me: when a tool message comes through as an array with a content item type other than text or image_url, this translation now silently drops that part and can emit an empty/partial output array. That is a lossy behavior change compared to the old string passthrough path, and it could break providers/tools that return richer structured parts in content arrays.
I would either preserve unknown parts in a fallback form, or add a regression test proving we intentionally reject them. Right now the failure mode is quiet data loss, which is the risky version.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 37cfef3d88
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if content.Type == gjson.String { | ||
| funcOutput, _ = sjson.SetBytes(funcOutput, "output", content.String()) | ||
| } else if content.IsArray() { |
There was a problem hiding this comment.
Keep tool output when content is not string or array
This branch now sets function_call_output.output only when tool content is a string or an array. If a caller sends tool content as an object, number, boolean, or null, the generated function_call_output has no output field at all, whereas the previous code always serialized content with .String(). In those inputs, tool results are dropped (or the request is rejected as malformed), so this should include an else fallback that preserves non-array payloads.
Useful? React with 👍 / 👎.
luispater
left a comment
There was a problem hiding this comment.
Summary:
This PR significantly improves multimodal translation for Codex tool outputs (image/file content in structured arrays) and adds solid coverage for the new paths. The main risk is a compatibility regression in function_call_output construction for non-string/non-array tool content.
Blocking issue:
- In
ConvertOpenAIRequestToCodex, tool-role handling now setsoutputonly for string or array content. Ifcontentisnull(or other non-array/non-string),outputis omitted entirely. Previously this path always emittedoutputvia string coercion. - Please add a fallback branch that always sets
output(for example empty string or serialized raw content), then add regression tests.
Test plan:
- Ran:
go test ./internal/translator/codex/openai/chat-completions(pass) - Requested extra test coverage:
- tool message with
content: nullshould still produce a validfunction_call_outputwithoutputpresent.
- tool message with
Ensure tool-role message content can be translated from structured arrays so image_url items are emitted as input_image parts instead of being dropped from function_call_output payloads.