Skip to content

Invalid file 'image': unsupported mimetype ('application/octet-stream'). Supported file formats are 'image/png'. #1468

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
1 task done
ChiefGnome opened this issue Apr 17, 2025 · 20 comments
Labels
bug Something isn't working

Comments

@ChiefGnome
Copy link

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

Since yesterday, a call to the "openai.images.edit" API throws an error:
BadRequestError: 400 Invalid file 'image': unsupported mimetype ('application/octet-stream'). Supported file formats are 'image/png'.
The same API call worked for that last year without this error.

To Reproduce

  • Provide to images, and set the filenames to patchFn and maskFn, both files are PNGs.
  • Call the openai.images.edit API with the following code

Code snippets

const response = await openai.images.edit({
        image: fs.createReadStream(patchFn),
        prompt,
        mask: fs.createReadStream(maskFn),
        n: 1,
        size: "1024x1024",
        response_format: "b64_json",
    }
);

OS

macOS 15.4

Node version

Node v22.14.0

Library version

openai 4.95.0

@ChiefGnome ChiefGnome added the bug Something isn't working label Apr 17, 2025
@aaronkopplin
Copy link

getting the same error

@SlicoHP
Copy link

SlicoHP commented Apr 18, 2025

Same error here with:

Node version
v20.9.0

Openai
4.93.0

@vemiliogp
Copy link

Getting the same error

@iMcreAtive
Copy link

Same error:
Node: v20.18.1
Openai: 4.95.1

@akinoavx13
Copy link

Hi,
Is there a fix available for this issue?
Thanks!

@jt55401
Copy link

jt55401 commented Apr 23, 2025

same issue
node: v23.9.0
openai: 4.95.1

@kennethhutw
Copy link

Hi,
same issue
Is there a fix available for this issue? or sample code
Thanks!

@joeydotdev
Copy link

Seeing this across both go + node.js clients. Seems like a regression on OpenAI's side

@akinncar
Copy link

same issue

@devPablo
Copy link

Having the same issue on my end.
"openai": "^4.96.0"

@taufiq-husain
Copy link

Getting the same issue with this:

"engines": {
    "node": "22"
  },
"openai": "^4.96.0",

Might have to resort to CURL requests for now.

@dested
Copy link

dested commented Apr 23, 2025

Same error.

@kgmodi
Copy link

kgmodi commented Apr 23, 2025

I am facing the same issue.

@Vadko
Copy link

Vadko commented Apr 24, 2025

temporary workaround if you are looking for immediate fix:

import { fileFromPath } from 'formdata-node/file-from-path';

const file = await fileFromPath(cachedFile, { type: 'image/jpeg' });

const response = await this.client.images.edit({
  model: 'gpt-image-1',
  prompt,
  n: 1,
  size: '1024x1024',
  image: file,
});

upd: I would recommend method below since it doesnt require any external library #1468 (comment)

@taufiq-husain
Copy link

temporary workaround if you are looking for immediate fix:

import { fileFromPath } from 'formdata-node/file-from-path';

const file = await fileFromPath(cachedFile, { type: 'image/jpeg' });

const response = await this.client.images.edit({
model: 'gpt-image-1',
prompt,
n: 1,
size: '1024x1024',
image: file,
});

This worked like a charm. Thank you very much!

@kennethhutw
Copy link

temporary workaround if you are looking for immediate fix:

import { fileFromPath } from 'formdata-node/file-from-path';

const file = await fileFromPath(cachedFile, { type: 'image/jpeg' });

const response = await this.client.images.edit({
model: 'gpt-image-1',
prompt,
n: 1,
size: '1024x1024',
image: file,
});

My files are on a remote host not locally
What should I do?

@Vadko
Copy link

Vadko commented Apr 25, 2025

temporary workaround if you are looking for immediate fix:
import { fileFromPath } from 'formdata-node/file-from-path';
const file = await fileFromPath(cachedFile, { type: 'image/jpeg' });
const response = await this.client.images.edit({
model: 'gpt-image-1',
prompt,
n: 1,
size: '1024x1024',
image: file,
});

My files are on a remote host not locally What should I do?

you can download file from remote host first using axios:

const response = await this.httpService.axiosRef<Readable>({
  url,
  method: 'GET',
  responseType: 'stream',
});

await new Promise<void>((resolve, reject) => {
  const writeStream = fs.createWriteStream(
    filepath
  );

  response.data.pipe(writeStream);

  writeStream.on('finish', resolve);
  writeStream.on('error', reject);
});

and after that you can pass filepath variable to fileFromPath function from comment above. dont forget to make cleanup after uploading image to openai with fs.unlinkSync(filepath) :)

@kennethhutw
Copy link

temporary workaround if you are looking for immediate fix:
import { fileFromPath } from 'formdata-node/file-from-path';
const file = await fileFromPath(cachedFile, { type: 'image/jpeg' });
const response = await this.client.images.edit({
model: 'gpt-image-1',
prompt,
n: 1,
size: '1024x1024',
image: file,
});

My files are on a remote host not locally What should I do?

you can download file from remote host first using axios:

const response = await this.httpService.axiosRef<Readable>({
  url,
  method: 'GET',
  responseType: 'stream',
});

await new Promise<void>((resolve, reject) => {
  const writeStream = fs.createWriteStream(
    filepath
  );

  response.data.pipe(writeStream);

  writeStream.on('finish', resolve);
  writeStream.on('error', reject);
});

and after that you can pass filepath variable to fileFromPath function from comment above. dont forget to make cleanup after uploading image to openai with fs.unlinkSync(filepath) :)

Sorry I didn't make myself clear, but in my case I can't save files locally. Thank for your answer.

 const response = await axios.get(imagePath, { responseType: 'arraybuffer' });
   const buffer = Buffer.from(response.data, 'binary');
 const file = new File([response], filename, { type: 'image/png' }); 

but not work.

@95gabor
Copy link

95gabor commented Apr 25, 2025

Here is my solution based on the official documentation:

import OpenAI, { toFile } from "openai";
import { createReadStream } from 'fs';
import { resolve } from 'path';

const prompt = 'something';

const result = await this.openAI.images.edit({
  model: 'gpt-image-1',
  image: await toFile(
    createReadStream(resolve(__dirname, './test/sample.png')),
    null,
    { type: 'image/png' },
  ),
  prompt,
  n: 1,
  size: '1024x1024',
  // response_format: 'b64_json',
});

console.log(result.data[0].b64_json);

@LeonMueller-OneAndOnly
Copy link

LeonMueller-OneAndOnly commented Apr 30, 2025

For people who have the same issue and want to send user uploaded images images (that dont live on the file system of your server) to the openai editing enpoint you can use following utilities from node-fetch:

import { Blob, File } from "node-fetch"

// referenceImages: { dataurl: string; mimeType: string }[]

const image: Uploadable[] = input.referenceImages.map((aImg, aIndex) => {
    return new File([dataURLtoBlob(aImg.dataurl)], `reference-image-${aIndex}`, {
      type: aImg.mimeType,
    })
 })

client_openAISdk.images.edit({
      model: "gpt-image-1",
      prompt: input.prompt,
      // @ts-ignore
      size: input.size,
      user: userIdentifier,
      quality: input.quality,
      n: input.countImagesToGenerate,
      background: input.enableTransparency ? "transparent" : "opaque",
      moderation: "low",
      image: image,
})

function dataURLtoBlob(dataurl: string) {
  // DataURL parsen
  const arr = dataurl.split(",")
  const mimeMatch = arr[0]!.match(/:(.*?);/)
  const mime = mimeMatch ? mimeMatch[1] : ""
  const bstr = atob(arr[1]!)
  let n = bstr.length
  const u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }

  return new Blob([u8arr], { type: mime })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests