Calling Whisper API using curl request keeps giving error

I tried to use the Whisper API using JavaScript with a post request but did not work, so proceeded to do a curl request from Windows PowerShell with the following code and still did not work. I attach the error message.

Invoke-RestMethod -Uri "https://api.openai.com/v1/audio/transcriptions" `
  -Method POST `
  -Headers @{
    "Authorization" = "Bearer TOKEN"
    "Content-Type" = "multipart/form-data"
  } `
  -Body @{
    "file" = Get-Item -Path "./audio.mp3"
    "model" = "whisper-1"
  }

Error:

Invoke-RestMethod : {
    "error": {
        "message": "Could not parse multipart form",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
At line:1 char:1
+ Invoke-RestMethod -Uri "https://api.openai.com/v1/audio/transcription ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Note that, I managed to get this file transcribed in python using whisper. And the same curl request for ChatGPT with the same API key works properly.

For full context, here is the js request:

const url = "https://api.openai.com/v1/audio/transcriptions"

const form = new FormData();
form.append("file", new File([audioURL], "audio-transcribe"));
form.append("model", "whisper-1");
form.append("response_format", "text");

const requestOptions = {
    method: "POST",
    headers: {
      "Authorization": "Bearer TOKEN",
      "Content-Type": "multipart/form-data"
    },
    body: form
}

fetch(url, requestOptions)
1 Like

Try removing the "Content-Type" = "multipart/form-data" header from your POST. I ran into a similar issue using the Rust library reqwest and removing that header fixed my issue.

2 Likes

I moved a step further, but now I get
“Invalid file format. Supported formats: [‘m4a’, ‘mp3’, ‘webm’, ‘mp4’, ‘mpga’, ‘wav’, ‘mpeg’]”,

async function callWhisper(file: Blob, name: string) {
  console.log("before call");
  const formData = new FormData();
  formData.append("model", "whisper-1");
  formData.append("file", new File([file], name));
  formData.append("response_format", "text");
  const res = await fetch("https://api.openai.com/v1/audio/transcriptions", {
    method: "POST",
    body: formData,
    headers: {
      Authorization: "Bearer " + Deno.env.get("OPENAPI_API_KEY")!,
    },
  });
  console.log("callChatGPTRes", res.status);
  const json = await res.json();
  console.log("callChatGPTRes", json);
  return json;
}

I think that maybe it is related to be sending the file itself in the wrong format

The reason my requests did not get sent was the wrong encoding, I needed to decode from base64 first, so it was not related to OpenAI API.

Could you share the code you used to do the decoding? Thanks!

so I have a file that is encoded as base64 and is stored as a file in React Native + Expo.

import { Buffer, Blob } from 'buffer';
import * as FileSystem from 'expo-file-system';
const file = Buffer.from(
  await FileSystem.readAsStringAsync(uri, {
    encoding: FileSystem.EncodingType.Base64,
  }),
  'base64',
)

essentially, this line

const file = Buffer.from(
        "string_encoded_as_base_64"
        'base64',
      )
// now it is Buffer

const blob = new Blob([file])

// now it is Blob

Thanks! Unfortunately I am still having trouble getting it work. I am getting my audio data from MediaRecorder chunks and creating a blob like this:

const blob = new Blob(chunks, {
  type: "audio/mp3",
}); 

Then I add it to the form like this:

form.append("file", blob);

However I still get the error: Invalid file format. Supported formats: ['m4a', 'mp3', 'webm', 'mp4', 'mpga', 'wav', 'mpeg'].

Any tips? Thanks!

edit: In the meantime I’ve tried a lot of things, such as passing mimeType: "audio/webm" to the MediaRecorder and/or the Blob, but no luck yet.

I finally got it, based on this, main parts being:

const blob = new Blob(chunks);
const file = new File([blob], "input.wav", { type: "audio/wav" });
form.append("file", file);
3 Likes

I was going insane but this made it work, thanks!

could you please provide more of your code, have been at this for a few days now going crazy, I have something similar but still getting the invalid file format error

Sure, what context do you need exactly? What does your own code look like?

The “chunks” portion. I have a base64 encoded .wav file that works but I’ve tried every which way to feed the blob the data it always ends up corrupted. Would help a alot!

Here’s some demo code that I’m using for Nodejs using the OpenAI Library (version 3.2.1).

const transcription = await openai.createTranscription(
    fs.createReadStream(filePath),
    "whisper-1",
    undefined,
    "verbose_json",
    undefined,
    undefined,
    {
      maxBodyLength: Infinity,
    }
  )

I’m using react-native, new to it but it’s a little more complicated than backend because you can’t use file system. I’ll give the library a try though!