400 BAD_REQUEST error when passing audio to Server before passing to OpenAI

I have a frontend that allows users to upload a file which then gets sent to my Node/Express server in a multipart form.

I am using multer to parse the file from the request but I can’t pass it to OpenAI without error. I have it set to save the file in storage and then I pass the file path into the request and it returns with an error.

The error I get is "Request failed with status code 400"

const express = require("express");
const multer = require("multer");
const { Configuration, OpenAIApi } = require("openai");
const upload = multer({ dest: "uploads/" });
const fs = require("fs");
const whisperConfiguration = new Configuration({
  apiKey: "API_KEY",
});
const app = express();

const port = 3000;
app.use(express.static("static"));


app.post("/whisper", upload.single("audio_file"), async (req, res) => {
  const whisperTime = process.hrtime();
  const openai = new OpenAIApi(whisperConfiguration);
  console.log("whisper", req.file);

  try {
    const resp = await openai.createTranscription(
      fs.createReadStream(req.file.path),
      "whisper-1"
    );
    res.send({ apiCall: resp.data, time: process.hrtime(whisperTime) });
  } catch (e) {
    res.send(e);
  }
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

I have tried this same code without reading the file from the request, but reading it from a locally stored file and it works fine. So I know there isn’t an authentication issue.

I have only got it to work by sending it the bytes, either from local disc or local memory.

@curt.kennedy I can get the Buffer from the multer middleware. I’ve tried sending just the buffer but that didn’t work either. Is there something else I could do with the Buffer?

Here is a quick simple Python version I coded after the announcement. Works out of the gate in an AWS Lambda environment (just add a requests layer and path to the actual file). The memory version using a BytesIO object would be similar. Haven’t tried any JavaScript versions myself.

Okay I got it to work. The issue is with the openai npm package. I did it straight through an axios request and it works fine.

app.post("/whisper", whisperUpload.single("audio_file"), async (req, res) => {
  const whisperTime = process.hrtime();
  console.log("whisper", req.file);

  try {
    const formData = new FormData();
    formData.append("model", "whisper-1");
    formData.append("file", fs.createReadStream(req.file.path), {
      filename: req.file.originalname,
    });
    const resp = await axios.post(
      "https://api.openai.com/v1/audio/transcriptions",
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
          Authorization:
            "Bearer API_KEY",
        },
      }
    );

    res.send({ apiCall: resp.data, time: process.hrtime(whisperTime) });
  } catch (e) {
    console.log("error", e);
    res.send(new Error(e));
  }
});
1 Like

Interesting. That’s why I created the standalone Python version. The OpenAI library didn’t work for me at the time.

1 Like