adamrushy / OpenAISwift

This is a wrapper library around the ChatGPT and OpenAI HTTP API
MIT License
1.64k stars 250 forks source link

Decoding Error #28

Open MarkHoath opened 1 year ago

MarkHoath commented 1 year ago

I get this random error every 2-10 requests, and I cant figure out the reason why.

I think in the OpenAI file, the object should be optional

public struct OpenAI: Codable { public let object: String public let model: String? public let choices: [Choice] }

ie public let object: String?

Error is...

Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "object", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"object\", intValue: nil) (\"object\").", underlyingError: nil)))

Sorry Im not advanced enough to do a pull request and test etc.

MarkHoath commented 1 year ago

OK So I figured out how to edit the package and I added a JSONSerialization to the Error Response and it comes back with

["error": { code = ""; message = "The server had an error while processing your request. Sorry about that!"; param = ""; type = "server_error"; }]

albertopeam commented 1 year ago

Hi! Official Doc doesn't say nothing about optionals. It seems to not be very well documented at least the responses with/without errors.

I have created a test to try to reproduce the scenario that you have shared and the only way I have found to reproduce it is to provide a wrong auth token, example:

import XCTest
@testable import OpenAISwift

final class OpenAISwiftTests: XCTestCase {
    let fakeAuthToken = "" // wrong auth token

    func testCompletions() async throws {
        do {
            let sut = OpenAISwift.init(authToken: fakeAuthToken)

            let result = try await sut.sendCompletion(with: "Write a haiku")

            XCTAssertFalse(result.choices.isEmpty)
        } catch let OpenAIError.genericError(error) {
            print(error)
        } catch let OpenAIError.decodingError(error) {
            print(error)
        }
    }
}

This test output is similar to what you comment

Test Case '-[OpenAISwiftTests.OpenAISwiftTests testCompletions]' started.
keyNotFound(CodingKeys(stringValue: "object", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"object\", intValue: nil) (\"object\").", underlyingError: nil))

Said that, have you checked that your auth token is not missing and correct? You can check it here https://platform.openai.com/account/api-keys

I hope it helps. If you can provide more info it would help to debug the exact problem

MarkHoath commented 1 year ago

The bottom half of my comment provides the response that is returned “error” and it appears that it occurs when the servers are overloaded, because if I resend the same request on getting that error, it eventually returns a response. After 2 to 5 tries.

It’s got nothing to do with my authToken

The error is in the JSON Decode because “error” is not in the OpenAI codable struct you use in JSONDecode…

I will send a detailed response when I get home

On Fri, 10 Feb 2023 at 8:58 am, Alberto Penas Amor @.***> wrote:

Hi! Official documentation doesn't say nothing about optionals, Doc https://platform.openai.com/docs/api-reference/completions. It seems to not be very well documented at least the responses with/without errors.

I have created a test to try to reproduce the scenario that you have shared and the only way I have found to reproduce it is to provide a wrong auth token, example:

import @.*** import OpenAISwift final class OpenAISwiftTests: XCTestCase { let fakeAuthToken = "" // wrong auth token func testCompletions() async throws { do { let sut = OpenAISwift.init(authToken: fakeAuthToken)

        let result = try await sut.sendCompletion(with: "Write a haiku")

        XCTAssertFalse(result.choices.isEmpty)
    } catch let OpenAIError.genericError(error) {
        print(error)
    } catch let OpenAIError.decodingError(error) {
        print(error)
    }
}

}

This test output is similar to what you comment

Test Case '-[OpenAISwiftTests.OpenAISwiftTests testCompletions]' started. keyNotFound(CodingKeys(stringValue: "object", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"object\", intValue: nil) (\"object\").", underlyingError: nil))

Said that, have you checked that your auth token is not missing and correct? You can check it here https://platform.openai.com/account/api-keys

I hope it helps. If you can provide more info if thats not the case it would help to debug the exact problem

— Reply to this email directly, view it on GitHub https://github.com/adamrushy/OpenAISwift/issues/28#issuecomment-1424890013, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGJCPF6KHMCFU7VZZMF4AGTWWVR7DANCNFSM6AAAAAAUWAHHVY . You are receiving this because you authored the thread.Message ID: @.***>

albertopeam commented 1 year ago

Hi!

It would be nice if you can share the statusCode of the response, I guess is possible that you get this DecodingError if the OpenAPI returns 200 and the error body that you have shared previously.

MarkHoath commented 1 year ago

Yes the Status Code is 200 and the JSONDecode is failing, which I then did a JSONSerialization on and get the following.

["error": { code = ""; message = "The server had an error while processing your request. Sorry about that!"; param = ""; type = "server_error"; }]

Berkay-Disli commented 1 year ago

I'm getting the same error and haven't found a solution yet unfortunately.

adamrushy commented 1 year ago

I think we have to monitor this and reach out to OpenAI - my gut feeling is that it's to do with some kind of rate limiting on the API or some changes they're making

albertopeam commented 1 year ago

Yes the Status Code is 200 and the JSONDecode is failing, which I then did a JSONSerialization on and get the following.

["error": { code = ""; message = "The server had an error while processing your request. Sorry about that!"; param = ""; type = "server_error"; }]

It looks like instead of returning a 500 for an internal error they return 200, so the body is tried to be parsed to a succesfull response, so it seems normal the DecodingError.

A possible workaround strategy is to try to parse the success response and catch the DecodingError, if it throws we can parse this error body that you have shared and then throw a new error like internalError, this can contain the message that the API returned.

It can be done easily @adamrushy, what do you think on this approach?

adamrushy commented 1 year ago

@albertopeam I like this approach :]

albertopeam commented 1 year ago

I will implement it then, thanks!

albertopeam commented 1 year ago

PR opened https://github.com/adamrushy/OpenAISwift/pull/33 Take a look, probably I will do some improvements regarding unit tests @adamrushy @MarkHoath

albertopeam commented 1 year ago

Hi! Any updates on this @adamrushy? :) https://github.com/adamrushy/OpenAISwift/pull/33

MarkHoath commented 1 year ago

Also I see there is a new model, we should probably add that ? I might play around with it tonight

On Sat, 4 Mar 2023 at 7:05 am, Alberto Penas Amor @.***> wrote:

Hi! Any updates on this @adamrushy https://github.com/adamrushy? :)

33 https://github.com/adamrushy/OpenAISwift/pull/33

— Reply to this email directly, view it on GitHub https://github.com/adamrushy/OpenAISwift/issues/28#issuecomment-1454067064, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGJCPF6HA43HAJLPLCQOLZDW2JFJLANCNFSM6AAAAAAUWAHHVY . You are receiving this because you were mentioned.Message ID: @.***>

pvieito commented 1 year ago

@adamrushy Hi!

Any update on this? We should fix the implementation on OpenAISwift. URLSession does not "auto-fail" when the status code is not 2XX-3XX, we have to implement that behavior, and retrieve the error message properly so it can be presented to the user or properly log it. To do that I would recommend following the same approach as in the official Python OpenAI API:

(Here is a very experimental implementation as an example: https://github.com/adamrushy/OpenAISwift/pull/42/files)

albertopeam commented 1 year ago

@adamrushy @MarkHoath Solved conflicts, ready to review and merge! Could you take a look @adamrushy

Take in mind that I have added unit tests to the completions endpoint. Now on you can easily add tests to sendEdits and sendChats

davidyannick86 commented 1 year ago

I have this error in my project "2023-03-16 17:41:54.001076+0100 ChatGPT-IOS[56127:1373580] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600002da68a0> F8BB1C28-BAE8-11D6-9C31-00039315CD46

decodingError(error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "object", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"object\", intValue: nil) (\"object\").", underlyingError: nil)))"

Is is the same as you describe ?

USBA commented 1 year ago

I got the same error today. Did you found any solution @davidyannick86 ?

pvieito commented 1 year ago

This PR should solve this: https://github.com/adamrushy/OpenAISwift/pull/65