sashabaranov / go-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALL·E, Whisper API wrapper for Go
Apache License 2.0
8.6k stars 1.29k forks source link

Unclear error messages for failure errors (x < 200 and x >= 400), 413 in this case #777

Open arshamalh opened 6 days ago

arshamalh commented 6 days ago

Describe the bug Trying to transcribe an mp3 file which happens to be larger than expected, using the snippet below:

config := openai.DefaultConfig("<token>")
client := openai.NewClientWithConfig(config)
req := openai.AudioRequest{
    Model:    openai.Whisper1,
    FilePath: "music.mp3",
}
resp, err := client.CreateTranscription(context.TODO(), req)
if err != nil {
    fmt.Printf("Transcription error: %v\n", err)
    return
}
fmt.Println(resp.Text)

the code returns this error:

error, status code: 413, message: invalid character '<' looking for beginning of value

OK, what is it all about? Is 413 an HTTP status code? where did '<' come from?

So I started to investigate, yep, 413 is an HTTP status code, but the error comes after it message: invalid character '<' looking for beginning of value, has nothing about the 413, and comes from json package which expects a json response.

it's a little bit confusing. logging the error inside handleErrorResp method (go-openai@v1.26.0/client.go:259):

<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>

Expected behavior I expected handleErrorResp returns a clear error like Request Entity Too Large before it tries to unmarshal an HTML, leading to a wrong second error.

Environment (please complete the following information):

Additional context I don't exactly remember but I think I had the same issue with rate limit related errors earlier.

arshamalh commented 6 days ago

I'm willing to open a PR if the issue sounds reasonable.

arshamalh commented 6 days ago

Changing the AudioRequest to:

req := openai.AudioRequest{
    Model:    openai.Whisper1,
    FilePath: "hashhashhash",
}

the file is a valid audio, but it's OGG that OpenAI doesn't support, the error I get is still from json package:

error, status code: 500, message: invalid character 'e' looking for beginning of value

but I expect to get something at least like the one server is returning:

error decoding MP3 file, make sure you send a valid MP3 file.