openai-java
openai-java copied to clipboard
No error handling at all
There are a variety of errors that may be returned from the openai API, but none of them are handled by this library. I pulled the source code and could not find any use of keywords such as "error" or "invalid_request_error" so it seems this was missed. There are a variety of reasons errors could happen such as running out of tokens, rate limiting, system overloaded, etc. Instead, the library just crashes with no indication of what the error is. The library needs a way for clients to know why the request failed so that appropriate action can be taken. For instance, if it's a rate limit error, the client code could pause for a minute, increase the sleep time between requests, and then retry.
Example response from openai:
"error": {
"message": "Incorrect API key provided: xxxxxxxx****************************************xxxx. You can find your API key at https://beta.openai.com.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
How openai-java behaves:
Exception in thread "main" retrofit2.adapter.rxjava2.HttpException: HTTP 401
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:57)
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:38)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:48)
at io.reactivex.Observable.subscribe(Observable.java:10151)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35)
at io.reactivex.Observable.subscribe(Observable.java:10151)
at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
at io.reactivex.Single.subscribe(Single.java:2517)
at io.reactivex.Single.blockingGet(Single.java:2001)
at com.theokanning.openai.OpenAiService.createCompletion(OpenAiService.java:91)
That's actually a great point. BTW, w/ the launch of chat.openai.com ("ChatGPT") I've been getting a ton of intermittent 404 and 503's even with valid tokens and software which are service errors.
https://status.openai.com/
I can't find any definitive list of error types/codes used by openai. But a quick fix here might be to look for the json like the error above and then throw your own exception with the error codes and message string in the exception's message.
As of 0.10.0, the OpenAiService in the new service library reads server errors and throws a new exception with the details. Hopefully this should make random error much easier to diagnose 👍