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

.Net: Bug: Azure OpenAI client fails with 401 when throttling #8929

Closed
dluc opened this issue Sep 21, 2024 · 4 comments · Fixed by #9465
Closed

.Net: Bug: Azure OpenAI client fails with 401 when throttling #8929

dluc opened this issue Sep 21, 2024 · 4 comments · Fixed by #9465
Assignees
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code

Comments

@dluc
Copy link
Collaborator

dluc commented Sep 21, 2024

When using AzureOpenAITextEmbeddingGenerationService SK client and sending too many requests, the Azure service throttling leads to a "401 Unauthorized" error instead of "429 Too Many Requests".

  • SK version: 1.20.0
  • .NET version: 8.0.401

I inspected the HTTP requests and noticed that the internal retry logic is sending a malformed request during retries. It appears to be adding the Authorization header twice (with the same token), e.g.

  • Request 1 .. 7: ok
  • Request 8: fails with HTTP Status Code 429
  • Request 8 is retried with two auth tokens: fails with HTTP Status Code 401

Code to repro:

public static class Program
{
    public static async Task Main()
    {
        var client = new AzureOpenAITextEmbeddingGenerationService(
            endpoint: "https://....openai.azure.com/",
            deploymentName: "text-embedding-ada-002",
            credential: new DefaultAzureCredential()
        );

        for (int i = 0; i < 200; i++)
        {
            Console.WriteLine($"## {i}");
            await client.GenerateEmbeddingsAsync([RndStr(), RndStr(), RndStr(), RndStr(), RndStr(), RndStr(), RndStr()]);
        }
    }

    private static string RndStr()
    {
        var random = new Random();
        return new(Enumerable.Repeat("ABCDEF GHIJKLMNOP QRSTUVW XYZabcdefgh ijklmnopqrst uvwxyz0123 456789", 8000)
            .Select(s => s[random.Next(s.Length)]).ToArray());
    }
}

Output:

## 0
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
Unhandled exception. Microsoft.SemanticKernel.HttpOperationException: Service request failed.
Status: 401 (Unauthorized)

 ---> System.ClientModel.ClientResultException: Service request failed.
Status: 401 (Unauthorized)

   at Azure.AI.OpenAI.ClientPipelineExtensions.ProcessMessageAsync(ClientPipeline pipeline, PipelineMessage message, RequestOptions options)
   at Azure.AI.OpenAI.Embeddings.AzureEmbeddingClient.GenerateEmbeddingsAsync(BinaryContent content, RequestOptions options)
   at OpenAI.Embeddings.EmbeddingClient.GenerateEmbeddingsAsync(IEnumerable`1 inputs, EmbeddingGenerationOptions options, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func`1 request)
   --- End of inner exception stack trace ---
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func`1 request)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetEmbeddingsAsync(String targetModel, IList`1 data, Kernel kernel, Nullable`1 dimensions, CancellationToken cancellationToken)
   at Program.Main() in Program.cs:line 21
   at Program.<Main>()
@dluc dluc added bug Something isn't working .NET Issue or Pull requests regarding .NET code labels Sep 21, 2024
@github-actions github-actions bot changed the title Bug: Azure OpenAI client fails with 401 when throttling .Net: Bug: Azure OpenAI client fails with 401 when throttling Sep 21, 2024
@dluc
Copy link
Collaborator Author

dluc commented Sep 22, 2024

looks like a bug in the underlying Azure.AI.OpenAI 2.0.0-beta.5, I reported it here Azure/azure-sdk-for-net#46109 including a workaround

@markwallace-microsoft
Copy link
Member

@RogerBarreto I'm assigning this to you, can you monitor the issue Devis created an pick up the fix when it becomes available.

@RogerBarreto RogerBarreto added the follow up Issues that require a follow up from the community. label Sep 27, 2024
@RogerBarreto
Copy link
Member

RogerBarreto commented Oct 7, 2024

The original issue was closed, setting this as complete as the original issue is addressed and will be available automatically to Semantic Kernel package in a future release.

@github-project-automation github-project-automation bot moved this from Bug to Sprint: Done in Semantic Kernel Oct 7, 2024
@dluc
Copy link
Collaborator Author

dluc commented Oct 22, 2024

@RogerBarreto the outstanding open issue here is the reliance on a pre-release package with a noticeable (quite annoying) bug that impacts all SK consumers.

Rather than waiting for the Azure SDK fix to propagate to SK and SK fix to propagate to consumers, I'd suggest that SK includes a workaround as soon as possible, e.g. by injecting a delegating handler:

internal sealed class AuthFixHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.Headers.TryGetValues("Authorization", out var headers) && headers.Count() > 1)
        {
            request.Headers.Authorization = new AuthenticationHeaderValue(
                request.Headers.Authorization!.Scheme,
                request.Headers.Authorization.Parameter);
        }

        return base.SendAsync(request, cancellationToken);
    }
}

@dluc dluc reopened this Oct 22, 2024
@markwallace-microsoft markwallace-microsoft moved this from Sprint: Done to Sprint: In Progress in Semantic Kernel Oct 23, 2024
@RogerBarreto RogerBarreto removed the follow up Issues that require a follow up from the community. label Oct 25, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 30, 2024
@github-project-automation github-project-automation bot moved this from Sprint: In Review to Sprint: Done in Semantic Kernel Oct 30, 2024
dluc added a commit to microsoft/kernel-memory that referenced this issue Oct 31, 2024
## Motivation and Context (Why the change? What's the scenario?)

Temporary workaround for Azure OpenAI SDK bug - service throttling
handled incorrectly and causing incorrect HTTP status error

See:
* Azure/azure-sdk-for-net#46109
* microsoft/semantic-kernel#8929
* #855
dluc added a commit to microsoft/kernel-memory that referenced this issue Nov 1, 2024
…sts (#876)

## Motivation and Context (Why the change? What's the scenario?)

When retrying requests after throttling, the Azure OpenAI SDK is sending
malformed requests, with multiple `Authorization` headers, which are
rejected by the Azure OpenAI service with a `401 (Unauthorized)` error
code, leading to an exception in the SDK.

See 
- Azure/azure-sdk-for-net#46109
- microsoft/semantic-kernel#8929
- #855

## High level description (Approach, Design)

Inject a policy to fix malformed HTTP headers.

Functional test included, to verify the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants