betalgo / openai

OpenAI .NET sdk - Azure OpenAI, ChatGPT, Whisper, and DALL-E
https://betalgo.github.io/openai/
MIT License
2.84k stars 513 forks source link

AudioCreateTranscriptionRequest - if specify a ResponseFormat that isn't Json, then Response.Successful is not set correctly #573

Closed pappde closed 4 weeks ago

pappde commented 1 month ago

Describe the bug When using AudioCreateTranscriptionRequest() you can specify the ResponseFormat. If you specify a value other than Json or VerboseJson, and the call fails, then it doesn't correctly recognize the Error field. "Successful" will be true, but "Text" will show Json for an Error.

Your code piece Using RunSimpleAudioCreateTranscriptionTest(), change to ResopnseFormat.Text. Then remove the extension from the Filename so that you will get an error back.

            ConsoleExtensions.WriteLine($"Uploading file {fileName}", ConsoleColor.DarkCyan);
            var audioResult = await sdk.Audio.CreateTranscription(new AudioCreateTranscriptionRequest
            {
                FileName = "foo",
                File = sampleFile,
                Model = Models.WhisperV1,
                TimestampGranularities =
                [
                    StaticValues.AudioStatics.TimestampGranularity.Word,
                    StaticValues.AudioStatics.TimestampGranularity.Segment
                ],
                ResponseFormat = StaticValues.AudioStatics.ResponseFormat.Text
                //ResponseFormat = StaticValues.AudioStatics.ResponseFormat.VerboseJson
            });

            if (audioResult.Successful)
            {
                Console.WriteLine($"Segments: {audioResult.Segments.Count}");
                Console.WriteLine($"Words: {audioResult.Words.Count}");
                Console.WriteLine(string.Join("\n", audioResult.Text));
            }
            else
            {
                if (audioResult.Error == null)
                {
                    throw new Exception("Unknown Error");
                }

                Console.WriteLine($"{audioResult.Error.Code}: {audioResult.Error.Message}");
            }

Result audioResult.Successful incorrectly is true, and audioResult.Error is not set. In fact, in this case, the test throws an NullException since the call failed, but it goes into the "audioResult.Successful" branch. If you look at the audioResult.Text property:

{
  "error": {
    "message": "Timestamp granularities are only supported with response_format=verbose_json",
    "type": "invalid_request_error",
    "param": "timestamp_granularities",
    "code": "timestamp_granularities_format"
  }
}

Expected behavior audioResult.Successful == false and audioResult.Error is set.

Additional context I wonder if the library should not allow the user to set ResponseFormat to anything other than json.

kayhantolga commented 1 month ago

I can confirm that this is a bug in the SDK. To solve this issue, we need to manage error messages for text response types, which I will address in the next update.