Whisper throws a HTTP code: 400 with message: Bad Request even when audio .mp3 file is <25MB

i have been using whisper api for quite some time and i’m very happy with it, except that it seems to struggle with handling longer clips. for example, i have a very simple jUnit test in which i’m sending it an .mp3 file of size 4.1MB, and it throws an exception:

java.lang.Exception: Connection returned HTTP code: 400 with message: Bad Request

however if i keep my uploaded files smaller (about under 1.5minutes), then everything works as designed. but the documentation states that the file can be as large as 25MB… so not sure why it is failing on much smaller files…

Can you post your whisper API calling code and any setup code it relies on please.

here’s the quick jUnit which shows the code, pretty straight-forward:

    public void testWhisp2() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost uploadFile = new HttpPost(POST_URL);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        uploadFile.setHeader("Authorization", "Bearer " + CommonVariables.OPEN_AI_API_KEY);
        builder.addTextBody("model", "whisper-1", ContentType.TEXT_PLAIN);
        File f = new File("/tmp/coco.mp3");
        HttpEntity multipart = builder.build();
        CloseableHttpResponse response = httpClient.execute(uploadFile);
        StatusLine statusLine = response.getStatusLine();
        int httpResponseCode = statusLine.getStatusCode();
        if (httpResponseCode == 200) {
            HttpEntity responseEntity = response.getEntity();
            InputStream content = responseEntity.getContent();
            StringBuilder sb;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(content))) {
                String inputLine;
                sb = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
            String responseJson = sb.toString();
            Map<String, String> map = gson.fromJson(responseJson, Map.class);
            String text = map.get("text");
            logger.info("text: " + text);
        } else {
            String reasonPhrase = statusLine.getReasonPhrase();
            String error = String.format("Connection returned HTTP code: %d with message: %s", httpResponseCode, reasonPhrase);
            if (error != null && error.contains("Connection returned HTTP code: 403 with message: Forbidden")) {
                logger.error("Got error: " + error);
            throw new Exception(error);

I’m having a similar issue with similar code: I keep getting “Bad request”, no matter in which format I send the (small) audio file to the service. The same (?) call via curl goes through. I’m guessing that there is something wrong with the way I include the audio file in the request. But I’ve gone through several options (including the one from here), with no success. Is there an example of how to correctly call the Whisper API via a Java MultipartEntityBuilder?

Found the/a culprit: I had an explicit
Content-Type" = “multipart/form-data”` header in my POST
Removing that solved my problem (no idea why)

1 Like