Skip to content

fix(codex): preserve image content in tool outputs#2349

Open
DarkNoah wants to merge 2 commits intorouter-for-me:mainfrom
DarkNoah:fix/codex-tool-image-output-clean
Open

fix(codex): preserve image content in tool outputs#2349
DarkNoah wants to merge 2 commits intorouter-for-me:mainfrom
DarkNoah:fix/codex-tool-image-output-clean

Conversation

@DarkNoah
Copy link
Copy Markdown

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.

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.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 image_url items, within tool-role messages was not being correctly processed. The changes ensure that these image URLs are now properly recognized and translated into input_image parts within the function_call_output payload, enhancing the robustness and completeness of the translation process for complex tool outputs.

Highlights

  • Enhanced Tool Message Content Parsing: The ConvertOpenAIRequestToCodex function now correctly handles tool-role messages where the content field is a structured array, rather than just a simple string.
  • Preservation of Image Content: image_url items within structured content arrays in tool-role messages are now properly translated into input_image parts in the function_call_output payload, preventing their loss.
  • New Test Case for Image Content: A new unit test, TestToolCallOutputWithImageContent, was added to ensure the accurate translation of tool outputs containing both text and image content.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 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.

Comment on lines 128 to 160
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown

@xkonjin xkonjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +132 to +134
if content.Type == gjson.String {
funcOutput, _ = sjson.SetBytes(funcOutput, "output", content.String())
} else if content.IsArray() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Collaborator

@luispater luispater left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 sets output only for string or array content. If content is null (or other non-array/non-string), output is omitted entirely. Previously this path always emitted output via 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: null should still produce a valid function_call_output with output present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants