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

Return an error message when OpenAI returns an HTML error instead of JSON. #447

Closed
kayhantolga opened this issue Dec 7, 2023 · 1 comment
Assignees
Labels
bug Something isn't working Ready for next version This issue solved and waiting for next release
Milestone

Comments

@kayhantolga
Copy link
Member

OpenAI returns an HTML message whenever it encounters an internal error, which causes the SDK to throw an exception without any meaningful message. It is important to catch these cases and return meaningful error messages.
here are sample issues: #408 #277

@CongquanHu
Copy link
Contributor

CongquanHu commented Dec 8, 2023

I wonder if we can read the data type of the response first, if it is json format, then we can rest assured to deserialize to json object; If it is an html message, we first read it as a string, then build an object with code and Message, and finally deserialize the object to the generic TResponse. If this can avoid the bad exception crash problem.

      public static async Task<TResponse> PostAndReadAsAsync<TResponse>(this HttpClient client, string uri, object? requestModel, CancellationToken cancellationToken = default)
    {
        //var response = await client.PostAsJsonAsync(uri, requestModel, new JsonSerializerOptions
        //{
        //    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
        //}, cancellationToken);
        //return await response.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken) ?? throw new InvalidOperationException();

        var response = await client.PostAsJsonAsync(uri, requestModel, new JsonSerializerOptions
        {
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
        }, cancellationToken);

        var contentType = response.Content.Headers.ContentType?.MediaType;
        // Check that the response is successful and of JSON type
        if (response.IsSuccessStatusCode && contentType != null && contentType.Contains("application/json"))
        {
            return await response.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken) ?? throw new InvalidOperationException();
        }
        else if (response.IsSuccessStatusCode && contentType != null && contentType.Contains("text/html"))
        {
            return JsonSerializer.Deserialize<TResponse>(JsonSerializer.Serialize(new
            {
                code = "500",
                message = await response.Content.ReadAsStringAsync(cancellationToken: cancellationToken)
            })) ?? throw new InvalidOperationException();
        }
        else
        {
            throw new InvalidOperationException("Unexpected  response from the server.");
        }
    }

Let me know if it helps. Thanks

@kayhantolga kayhantolga added this to the 7.4.3 milestone Dec 9, 2023
@kayhantolga kayhantolga added the Ready for next version This issue solved and waiting for next release label Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Ready for next version This issue solved and waiting for next release
Projects
None yet
Development

No branches or pull requests

2 participants