Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding Error #28

Open
MarkHoath opened this issue Feb 9, 2023 · 18 comments
Open

Decoding Error #28

MarkHoath opened this issue Feb 9, 2023 · 18 comments
Labels
bug Something isn't working

Comments

@MarkHoath
Copy link

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 MarkHoath changed the title Deconding Error Decoding Error Feb 9, 2023
@MarkHoath
Copy link
Author

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
Copy link
Contributor

albertopeam commented Feb 9, 2023

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
Copy link
Author

MarkHoath commented Feb 10, 2023 via email

@albertopeam
Copy link
Contributor

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
Copy link
Author

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
Copy link

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

@adamrushy
Copy link
Owner

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

@adamrushy adamrushy added the bug Something isn't working label Feb 14, 2023
@albertopeam
Copy link
Contributor

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
Copy link
Owner

@albertopeam I like this approach :]

@albertopeam
Copy link
Contributor

I will implement it then, thanks!

@albertopeam
Copy link
Contributor

PR opened #33
Take a look, probably I will do some improvements regarding unit tests
@adamrushy
@MarkHoath

@albertopeam
Copy link
Contributor

Hi!
Any updates on this @adamrushy? :)
#33

@MarkHoath
Copy link
Author

MarkHoath commented Mar 3, 2023 via email

@pvieito
Copy link
Contributor

pvieito commented Mar 9, 2023

@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
Copy link
Contributor

albertopeam commented Mar 16, 2023

@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
Copy link

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
Copy link

USBA commented Mar 30, 2023

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

@pvieito
Copy link
Contributor

pvieito commented Mar 30, 2023

This PR should solve this: #65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants