OpenAI Node lib error on Audio Transcription

Im trying to record audio from user and send to whisper to transcribe, but the openai node lib is returning Axios error with 400 bad request.

response: {
    status: 400,
    statusText: 'Bad Request',
    headers: {
      date: 'Sat, 15 Apr 2023 16:08:50 GMT',
      'content-type': 'application/json',
      'content-length': '204',
      connection: 'close',
      'openai-organization': 'user-',
      'openai-processing-ms': '117',
      'openai-version': '2020-10-01',
      'strict-transport-security': 'max-age=15724800; includeSubDomains',
      'x-ratelimit-limit-requests': '50',
      'x-ratelimit-remaining-requests': '49',
      'x-ratelimit-reset-requests': '1.2s',
      'x-request-id': '36c8e2b87112c8e80dc44ee652344c25',
      'cf-cache-status': 'DYNAMIC',
      server: 'cloudflare',
      'cf-ray': '7b85730edf68a5d4-GRU',
      'alt-svc': 'h3=":443"; ma=86400, h3-29=":443"; ma=86400'
    },
    config: {
      transitional: [Object],
      adapter: [Function: httpAdapter],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      validateStatus: [Function: validateStatus],
      headers: [Object],
      method: 'post',
      data: [FormData],
      url: 'https://api.openai.com/v1/audio/transcriptions'
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: true,
      shouldKeepAlive: false,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      strictContentLength: false,
      _contentLength: null,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      socket: [TLSSocket],
      _header: 'POST /v1/audio/transcriptions HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: multipart/form-data; boundary=--------------------------5047590565482031\r\n' +
        'User-Agent: OpenAI/NodeJS/3.2.1\r\n' +
        'Authorization: Bearer sk-\r\n' +
        'Host: api.openai.com\r\n' +
        'Connection: close\r\n' +
        'Transfer-Encoding: chunked\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],

This is how im requesting:

import FormData from 'form-data';
import { withFileUpload } from 'next-multiparty';
import { createReadStream, read } from 'fs';

const fs = require('fs');

export const config = {
  api: {
    bodyParser: false,
  },
};

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

export default withFileUpload(async (req, res) => {

  const file:any= fs.createReadStream(req.file?.filepath);

  const response = await openai.createTranscription(file,"whisper-1").then((res) => console.log(res.data)).catch((err) => console.log(err));

  if (response) {
    return response.text;
  }else{
    return "Speech to text failed"
}

});

I tried looking on github, but this error keeps happening.

1 Like

I think that it is not problem with your code.
I tried to do that with curl as described here: OpenAI API
curl --request POST --url https://api.openai.com/v1/audio/transcriptions --header “Authorization: Bearer APIKEY” --header “Content-Type: multipart/form-data” --form file=@out000.mp3 --form model=whisper-1
and it also returns:

400 Bad Request

400 Bad Request


cloudflare

Any update on this? I am getting the same issue trying to call the transcriptions API from a node application. At first I was getting the issue of unsupported file format (I’m trying to send an mp3), now all I get is a 400 Bad Request. I have tried using openai library and axios to make the request.

I made it work by sending to my backend the audio in base64 and then converting it back into a mp3 file and sending the entire file to whisper

1 Like

Hey @thegen, can I please see your code? I tried to do the same with a m4a file but it didn’t work for me.

1 Like