#How to pass a Buffer to the methods that require image uploads.

20 messages · Page 1 of 1 (latest)

mortal quartz
#

The Configuration lets you set a formDataCtor parameter to fix this.
So, add this to your code:

import FormData from "form-data";

class FData extends FormData {
    append(key: string, value: any, options?: FormData.AppendOptions | string): void {
        if (key === 'image') {
            return super.append('image', value, {
                contentType: 'image/png',
                filename: 'img.png',
            });
        } else {
            return super.append(key, value, options);
        }
    }
}

On your Configuration declaration, add a parameter formDataCtor: FData,
Should look like this:

const configuration = new Configuration({
  apiKey: your_api_key,
  formDataCtor: FData,
});

And done. Now you are able to simply pass a Buffer to the createImageVariation and createImageEdit methods like this: const ```TS
response = await openai.createImageVariation(imageBuffer, 1, '1024x1024');

random quarry
#

The one thing about this is you have to hardcode the argument name

#

You also need this for mask on edit

#

I think a better way might be to provide a custom buffer wrapper that provides the file name so the caller can use it when they want to

#

I’ll share your solution with the sdk engineer

#

Oh oh you could check if value is a buffer

#

Oh but buffers could be anything, I wonder if you can check if it’s png data

mortal quartz
#

yea, this is just an example on how to make it work

random quarry
#

I’m just trying to figure out what to add to our guide

mortal quartz
random quarry
#

I think the cleanest is still to change our methods to require file type

random quarry
#

Was just googling

mortal quartz
#

BUT in fact, yo udo'nt even need to pass the contentType

#

this parameter could very well be omitted and it still work

random quarry
#

Yeah I just did buffer.name = ‘image.png’ and it works

mortal quartz
#

ok, here is a even simpler workaround

#
(fileBuf as any).path = 'temp.png';
// @ts-ignore
const response = await openai.createImageVariation(fileBuf as any, 1, '256x256');
#

"simpler", it is actually shorter and messier