Open leandrodvd opened 1 month ago
@mssfang could you please follow up with @leandrodvd? Is this something that should be covered in the README or TROUBLESHOOT guide
@leandrodvd I created a TROUBLESHOOT.md guideline to help explain how to handle errors. Hope it helps
Hi, yeah, the documentation looks like a good start, but it's kinda shallow, only shows what exception to catch, could have more details on the exception content. For instance I learned that the HttpResponseException.getValue()
contains an Error object with an error code that tells me what is the actual error. That error code is fundamental to understanding what is the actual error. This kind of detail could be documented.
A better solution (and maybe a separate issue) would be to have the error mapped to some model class that I can use. That's in fact the solution I implemented, I created an "Error"
record and on HttpResponseException
catch I deserialize the .getValue()
content to that class so that I can access the error details (for instance I'm interested in error code so that I can treat content_filter
and context_length_exceeded
errors so that I can format a nice answer to my user)
I implemented this Error class on my side but I believe this should be part of the SDK
public record HttpResponseExceptionError(
Error error
) {
}
public record Error(
String message,
String type,
String param,
String code
) {
}
try {
chatCompletions = openAiClient.getChatCompletions(...);
} catch (HttpResponseException e) {
HttpResponseExceptionError httpResponseExceptionError = BinaryData.fromObject(e.getValue()).toObject(HttpResponseExceptionError.class);
log.info("HttpResponseException occurred - error code {}", httpResponseExceptionError.error().code());
Query/Question How to properly handle errors on OpenAI client ? I'm using
OpenAIClient.getChatCompletions()
. I noticed that different types of errors will generate HttpResponseException. I`m trying to understand what is the best approach to properly parse/handle those errors.For instance, the same HttpResponseException is thrown when the request exceeds the model length and when the requests is filtered by the open ai content filter. I have very different approaches for those errors. For model length limit I want to retry the request with a different model, for content filter I want to reply a polite message to my user.
How can I differentiate what is the actual error causing the exception ? Is there anything on the SDK that can help me parse the error ?
Maybe some sample code with error handling would help
Why is this not a Bug or a feature Request? Not a bug, just need to guidance on how to handle errors properly.
Setup (please complete the following information if applicable):