-
-
Notifications
You must be signed in to change notification settings - Fork 250
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
Comments
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": { |
Hi! 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? I hope it helps. If you can provide more info it would help to debug the exact problem |
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
<#28 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGJCPF6KHMCFU7VZZMF4AGTWWVR7DANCNFSM6AAAAAAUWAHHVY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Hi! It would be nice if you can share the |
Yes the Status Code is 200 and the JSONDecode is failing, which I then did a JSONSerialization on and get the following. ["error": { |
I'm getting the same error and haven't found a solution yet unfortunately. |
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 |
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? |
@albertopeam I like this approach :] |
I will implement it then, thanks! |
PR opened #33 |
Hi! |
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 <#33>
—
Reply to this email directly, view it on GitHub
<#28 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGJCPF6HA43HAJLPLCQOLZDW2JFJLANCNFSM6AAAAAAUWAHHVY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@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) |
@adamrushy @MarkHoath Take in mind that I have added unit tests to the completions endpoint. Now on you can easily add tests to |
I have this error in my project 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 ? |
I got the same error today. Did you found any solution @davidyannick86 ? |
This PR should solve this: #65 |
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.
The text was updated successfully, but these errors were encountered: