Skip to content
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

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

Open
leandrodvd opened this issue Jun 3, 2024 · 3 comments · Fixed by #40671
Open

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

leandrodvd opened this issue Jun 3, 2024 · 3 comments · Fixed by #40671
Assignees
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. OpenAI question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@leandrodvd
Copy link

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 leandrodvd changed the title [QUERY] [QUERY] How to properly handle errors on OpenAI client ? Jun 3, 2024
@leandrodvd leandrodvd changed the title [QUERY] How to properly handle errors on OpenAI client ? [QUERY] How to properly handle errors on OpenAI client (azure-ai-openai)? Jun 3, 2024
@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jun 3, 2024
@github-actions github-actions bot removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jun 10, 2024
@joshfree
Copy link
Member

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

@mssfang
Copy link
Member

mssfang commented Jun 17, 2024

@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
Copy link
Author

leandrodvd commented Jun 18, 2024

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

@mssfang mssfang reopened this Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. OpenAI question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants