Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.25k stars 1.93k forks source link

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

Open leandrodvd opened 1 month ago

leandrodvd commented 1 month ago

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

joshfree commented 3 weeks ago

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

mssfang commented 2 weeks ago

@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

leandrodvd commented 2 weeks ago

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