MacPaw / OpenAI

Swift community driven package for OpenAI public API
MIT License
1.88k stars 297 forks source link

Non-JSON formatted data or verbose JSON data on AudioTranscriptions couldn’t decode #130

Open usagimaru opened 7 months ago

usagimaru commented 7 months ago

Describe the bug

I have tested the behavior of new response_format parameter implemented with #114. However, my frontend code always returned a JSON decoding error.

I traced from the breakpoint and found that it catches the error at this line in the method performRequest<ResultType: Codable>() of OpenAI.swift. Errors seems to occur when anything but a specific JSON structure (e.g. plain text) is entered.

I tried all formats in the new enum AudioResponseFormat and found that only .json worked fine, text, Verbose JSON, srt and vtt caught any errors.

I have not tried testing with the AudioTranslations API, but it may contain same problems.


Error example 1

APIErrorResponse(error: OpenAI.APIError(message: "1 validation error for Request\nbody -> response_format\n value is not a valid enumeration member; permitted: \'json\', \'text\', \'vtt\', \'srt\', \'verbose_json\' (type=type_error.enum; enum_values=[<ResponseFormat.JSON: \'json\'>, <ResponseFormat.TEXT: \'text\'>, <ResponseFormat.VTT: \'vtt\'>, <ResponseFormat.SRT: \'srt\'>, <ResponseFormat.VERBOSE_JSON: \'verbose_json\'>])", type: "invalid_request_error", param: nil, code: nil))

Error example 2

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unexpected character 'W' around line 1, column 1." UserInfo={NSDebugDescription=Unexpected character 'W' around line 1, column 1., NSJSONSerializationErrorIndex=0})))

Expected behavior

Correctly decode returned data as JSON or plain text (including vtt, srt).

Desktop (please complete the following information):

paulz commented 2 months ago
    func audioTranscriptions(
        query: AudioTranscriptionQuery
    ) async throws -> AudioTranscriptionResult {

incorrectly throws JSON decoding error for AudioResponseFormat.srt

we could not use other formats as we need time codes