Skip to content

Fix for Edit Image content type requirement #979

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Static-Flow
Copy link

Describe the change
PR to fix #967 and #601 The OpenAI Edit Image endpoint /v1/images/edits only accepts specific image formats as seen in error responses like so:
status: 400 Bad Request, message: Invalid file 'image': unsupported mimetype ('image/jpeg'). Supported file formats are 'image/png'. Even though the API docs say other types are supported https://platform.openai.com/docs/api-reference/images/createEdit:

For gpt-image-1, each image should be a png, webp, or jpg file less than 25MB.

This is not true as of 4-28-25 I think because it is still stuck on DALLE mode which only allows PNGs.

Describe your solution
Pretty simple fix really, replaced the call to the default helper method which only does application/octet-stream with the code the helper method calls and set the content-type to image/png

Tests
I ran a modified version of the image example which calls the edit image endpoint instead using my forked version and it worked successfully.

Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.46%. Comparing base (774fc9d) to head (2780acb).
Report is 97 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #979       +/-   ##
===========================================
- Coverage   98.46%   85.46%   -13.00%     
===========================================
  Files          24       43       +19     
  Lines        1364     2270      +906     
===========================================
+ Hits         1343     1940      +597     
- Misses         15      308      +293     
- Partials        6       22       +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Static-Flow
Copy link
Author

@sashabaranov I'm not sure how to fix this codecov failing CI check. It says there's a 13% drop in testing coverage but nothing I changed materially impacted tests. It seems to think there's more changes than what my PR made, maybe due to BASE commit is 91 commits behind HEAD on master? Please advise, the edit image api's are currently broken without this fix.

@sashabaranov sashabaranov requested a review from Copilot May 3, 2025 20:50
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the image content type issue for the OpenAI Edit Image endpoint by setting the content-type explicitly to "image/png" in the form builder.

  • Replaces the default helper call with manual MIME header construction.
  • Hardcodes "image/png" as the content type to address the endpoint's requirements.

func (fb *DefaultFormBuilder) createFormFile(fieldname string, r io.Reader, filename string) error {
if filename == "" {
return fmt.Errorf("filename cannot be empty")
}

fieldWriter, err := fb.writer.CreateFormFile(fieldname, filename)
h := make(textproto.MIMEHeader)
Copy link
Preview

Copilot AI May 3, 2025

Choose a reason for hiding this comment

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

Consider adding a comment to explain the manual MIME header construction with the hardcoded 'image/png' content type, so future maintainers understand this is specifically for the Edit Image endpoint fix.

Suggested change
h := make(textproto.MIMEHeader)
h := make(textproto.MIMEHeader)
// The "Content-Type" is hardcoded to "image/png" because this method is specifically
// used for the "Edit Image" endpoint, which requires PNG files.

Copilot uses AI. Check for mistakes.

Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! Could I kindly ask for an integration test here? Without them it's really hard for me to verify multitude of coming changes

@@ -34,12 +36,22 @@ func (fb *DefaultFormBuilder) CreateFormFileReader(fieldname string, r io.Reader
return fb.createFormFile(fieldname, r, path.Base(filename))
}

var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
Copy link
Owner

Choose a reason for hiding this comment

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

Might be better to use backtics here!

@sashabaranov
Copy link
Owner

@Static-Flow on the coverage note, could you try rebasing your branch on the latest master branch?

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.

CreateEditImage method upload file report Supported file formats are 'image/png'
2 participants