azure-sdk-for-java icon indicating copy to clipboard operation
azure-sdk-for-java copied to clipboard

[QUERY] How to properly handle errors on OpenAI client (azure-ai-openai)?

Open leandrodvd opened this issue 2 years ago • 6 comments

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):

  • Library/Libraries: com.azure:azure-ai-openai:1.0.0-beta.8

leandrodvd avatar Jun 03 '24 23:06 leandrodvd

@mssfang could you please follow up with @leandrodvd? Is this something that should be covered in the README or TROUBLESHOOT guide

joshfree avatar Jun 10 '24 16:06 joshfree

@leandrodvd I created a TROUBLESHOOT.md guideline to help explain how to handle errors. Hope it helps

https://github.com/Azure/azure-sdk-for-java/pull/40671/files#diff-d1d2dcdfd75f2946c83c56d2d59f3a197d4320f40c8e6630a8722a6ba85b1f86R74

mssfang avatar Jun 17 '24 22:06 mssfang

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());

leandrodvd avatar Jun 18 '24 14:06 leandrodvd

@leandrodvd , apologies for the long response time.

The Azure OpenAI Inference SDK has been deprecated in its current form. This package will be repurposed to provide Azure types to be used with the official OpenAI Java SDK in the future, but there is not much detail I can provide at this point in time, as things are still in flux. We have provided a basic migration guide for this in this folder.

That being said, it seems like you are having issues with the documentation not being thorough enough. I believe you can find more details over here, if still necessary: https://learn.microsoft.com/en-us/java/api/com.azure.core.exception?view=azure-java-stable

jpalvarezl avatar Jun 04 '25 09:06 jpalvarezl

Hi @leandrodvd. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

github-actions[bot] avatar Jun 04 '25 09:06 github-actions[bot]

Hi @leandrodvd, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

github-actions[bot] avatar Jun 11 '25 09:06 github-actions[bot]