-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[BUG] Azure OpenAI client fails with 401 when throttling #46109
Comments
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jpalvarezl @ralph-msft @trrwilson. |
FYI, I'm using this handler as a workaround: public 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);
}
} |
A public class AuthorizationHeaderWorkaroundPolicy : PipelinePolicy
{
private const string AuthorizationHeaderName = "Authorization";
public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
RemoveDuplicateHeaderValues(message.Request.Headers);
ProcessNext(message, pipeline, currentIndex);
}
public override async ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
RemoveDuplicateHeaderValues(message.Request.Headers);
await ProcessNextAsync(message, pipeline, currentIndex).ConfigureAwait(false);
}
private static void RemoveDuplicateHeaderValues(PipelineRequestHeaders headers)
{
if (headers.TryGetValues(AuthorizationHeaderName, out var headerValues)
&& headerValues is not null
&& headerValues.TryGetNonEnumeratedCount(out var count)
&& count > 1)
{
headers.Set(AuthorizationHeaderName, headerValues.First());
}
}
} This can be added to the policies on the options.AddPolicy(new AuthorizationHeaderWorkaroundPolicy(), PipelinePosition.PerTry); |
Suspected root cause on L56: azure-sdk-for-net/sdk/openai/Azure.AI.OpenAI/src/Custom/AzureTokenAuthenticationPolicy.cs Lines 46 to 56 in 3a55d32
I believe the code should use Above code was recently modified in this PR: #44983 |
Is there an expected ETA for a release that contains this fix? Being an upstream dependency of many other projects like Semantic Kernal and Kernel Memory this is causing issues for many downstream consumers. |
@trrwilson, would you be able to share plans regarding the next release of the Azure.AI.OpenAI library? |
## 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
…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.
Library name and version
Azure.AI.OpenAI 2.0.0-beta.5
Describe the bug
When using
AzureOpenAIClient
and sending too many requests, the Azure service throttling leads to a "401 Unauthorized" error instead of "429 Too Many Requests".Looking at the internal requests, looks like the code is retrying on 429 as expected, sending a malformed request containing the
Authorization
header twice (with the same token).Expected behavior
The client should keep retrying on 429 and/or fail with a HTTP exception status code 429
Actual behavior
The client fails with a HTTP exception status code 401
Reproduction Steps
Output:
Environment
The text was updated successfully, but these errors were encountered: