Search code examples
javaandroidokhttpopenai-apiopenai-whisper

OpenAI Whisper API, response status code 400 error


I am trying to use the Whisper API in Android, but I am encountering an issue where the request code is consistently returning a 400 error. First of all, since it works correctly when using Postman, I believe that the issue is not related to the secret key or content header values. So, based on my current assumption, I believe there might be an error in using okhttp3.

Below is my Android code.

public void requestWhisper(String file, Callback callback) {

        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("audio/mpeg"); 
        File audioFile = new File(file);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("model", "whisper-1")
                .addFormDataPart("file", audioFile.getName(), 
                                  RequestBody.create(audioFile, mediaType))
                .build();

        Request request = new Request.Builder()
                .url(whisper_api)
                .post(requestBody)
                .addHeader("Authorization", "Bearer " + key)
                .addHeader("Content-Type", "multipart/form-data")
                .build();


        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull okhttp3.Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    callback.onCallback(result);
                } else {
                    callback.onCallback("Error: " + response.message());
                }
            }
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                callback.onCallback("Error: " + e.getMessage());
            }
        });
}

And Below is response.

Response{protocol=h2, code=400, message=, url=https://api.openai.com/v1/audio/transcriptions}

I am currently facing a roadblock. I would appreciate any suggestions or opinions you can provide.

Here is another piece of code I tried besides the one mentioned above.

OkHttpClient client = new OkHttpClient().newBuilder().build();
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("model","whisper-1")
  .addFormDataPart("file","VOICE_FILE.mp3",
    RequestBody.create(MediaType.parse("application/octet-stream"),
    new File("FILE_PATH")))
  .build();
Request request = new Request.Builder()
  .url("https://api.openai.com/v1/audio/transcriptions")
  .method("POST", body)
  .addHeader("Authorization", "Bearer " + key)
  .addHeader("Content-Type", "multipart/form-data")
  .build();
Response response = client.newCall(request).execute();

However, the outcome was the same.


Solution

  • Welcome to Stack Overflow!

    Try getting the absolute path of the audio file and send the audio file's content when sending POST request to the Whisper API

    According to the example requests for python and node API Reference for Creating transcriptions you need to get the absolute path of your audio file, read the audio file's content, and assign it to a variable and use the variable as the file parameter of your POST request to the OpenAI Whisper API