Skip to content

Commit 846c565

Browse files
authored
.Net: Azure OpenAI Temporary Fix [401 Retry Bug] (#9465)
### Motivation and Context - Fix #8929
1 parent 5ccfeaf commit 846c565

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.ClientModel.Primitives;
5+
using System.Net.Http;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Azure.AI.OpenAI;
9+
using Azure.Core;
10+
using Microsoft.SemanticKernel.ChatCompletion;
11+
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
12+
using SemanticKernel.Connectors.AzureOpenAI.Core;
13+
14+
namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Core;
15+
16+
public sealed class ClientCoreTests : IDisposable
17+
{
18+
private readonly MultipleHttpMessageHandlerStub _multiHttpMessageHandlerStub;
19+
private readonly HttpClient _httpClient;
20+
21+
public ClientCoreTests()
22+
{
23+
this._multiHttpMessageHandlerStub = new MultipleHttpMessageHandlerStub();
24+
this._httpClient = new HttpClient(this._multiHttpMessageHandlerStub);
25+
}
26+
27+
public void Dispose()
28+
{
29+
this._httpClient.Dispose();
30+
this._multiHttpMessageHandlerStub.Dispose();
31+
}
32+
33+
[Fact]
34+
public async Task AuthenticationHeaderShouldBeProvidedOnlyOnce()
35+
{
36+
// Arrange
37+
using var firstResponse = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests);
38+
using var secondResponse = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests);
39+
using var thirdResponse = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests);
40+
41+
this._multiHttpMessageHandlerStub.ResponsesToReturn.AddRange([firstResponse, secondResponse, thirdResponse]);
42+
var options = new AzureOpenAIClientOptions()
43+
{
44+
Transport = new HttpClientPipelineTransport(this._httpClient),
45+
RetryPolicy = new ClientRetryPolicy(2),
46+
NetworkTimeout = TimeSpan.FromSeconds(10),
47+
};
48+
49+
// Bug fix workaround
50+
options.AddPolicy(new SingleAuthorizationHeaderPolicy(), PipelinePosition.PerTry);
51+
52+
var azureClient = new AzureOpenAIClient(
53+
endpoint: new Uri("http://any"),
54+
credential: new TestJWTBearerTokenCredential(),
55+
options: options);
56+
57+
var clientCore = new AzureClientCore("deployment-name", azureClient);
58+
59+
ChatHistory chatHistory = [];
60+
chatHistory.AddUserMessage("User test");
61+
62+
// Act
63+
var exception = await Record.ExceptionAsync(() => clientCore.GetChatMessageContentsAsync("model-id", chatHistory, null, null, CancellationToken.None));
64+
65+
// Assert
66+
Assert.NotNull(exception);
67+
Assert.Equal(3, this._multiHttpMessageHandlerStub.RequestHeaders.Count);
68+
69+
foreach (var requestHeaders in this._multiHttpMessageHandlerStub.RequestHeaders)
70+
{
71+
this._multiHttpMessageHandlerStub.RequestHeaders[2]!.TryGetValues("Authorization", out var authHeaders);
72+
Assert.NotNull(authHeaders);
73+
Assert.Single(authHeaders);
74+
}
75+
}
76+
77+
private sealed class TestJWTBearerTokenCredential : TokenCredential
78+
{
79+
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
80+
{
81+
return new AccessToken("JWT", DateTimeOffset.Now.AddHours(1));
82+
}
83+
84+
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
85+
{
86+
return ValueTask.FromResult(new AccessToken("JWT", DateTimeOffset.Now.AddHours(1)));
87+
}
88+
}
89+
}

‎dotnet/src/Connectors/Connectors.AzureOpenAI/Core/AzureClientCore.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.SemanticKernel.Connectors.OpenAI;
1616
using Microsoft.SemanticKernel.Http;
1717
using OpenAI;
18+
using SemanticKernel.Connectors.AzureOpenAI.Core;
1819

1920
namespace Microsoft.SemanticKernel.Connectors.AzureOpenAI;
2021

@@ -146,6 +147,7 @@ internal static AzureOpenAIClientOptions GetAzureOpenAIClientOptions(HttpClient?
146147

147148
options.UserAgentApplicationId = HttpHeaderConstant.Values.UserAgent;
148149
options.AddPolicy(CreateRequestHeaderPolicy(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(typeof(AzureClientCore))), PipelinePosition.PerCall);
150+
options.AddPolicy(new SingleAuthorizationHeaderPolicy(), PipelinePosition.PerTry);
149151

150152
if (httpClient is not null)
151153
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.ClientModel.Primitives;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace SemanticKernel.Connectors.AzureOpenAI.Core;
9+
10+
/// <summary>
11+
/// This class is used to remove duplicate Authorization headers from the request Azure OpenAI Bug.
12+
/// https://github.com/Azure/azure-sdk-for-net/issues/46109 (Remove when beta.2 is released)
13+
/// </summary>
14+
internal sealed class SingleAuthorizationHeaderPolicy : PipelinePolicy
15+
{
16+
private const string AuthorizationHeaderName = "Authorization";
17+
18+
public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
19+
{
20+
RemoveDuplicateHeaderValues(message.Request.Headers);
21+
22+
ProcessNext(message, pipeline, currentIndex);
23+
}
24+
25+
public override async ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
26+
{
27+
RemoveDuplicateHeaderValues(message.Request.Headers);
28+
29+
await ProcessNextAsync(message, pipeline, currentIndex).ConfigureAwait(false);
30+
}
31+
32+
private static void RemoveDuplicateHeaderValues(PipelineRequestHeaders headers)
33+
{
34+
if (headers.TryGetValues(AuthorizationHeaderName, out var headerValues)
35+
&& headerValues is not null
36+
#if NET
37+
&& headerValues.TryGetNonEnumeratedCount(out var count) && count > 1
38+
#else
39+
&& headerValues.Count() > 1
40+
#endif
41+
)
42+
{
43+
headers.Set(AuthorizationHeaderName, headerValues.First());
44+
}
45+
}
46+
}

‎dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
<ItemGroup>
3030
<InternalsVisibleTo Include="SemanticKernel.Connectors.OpenAI.UnitTests" />
31+
<InternalsVisibleTo Include="SemanticKernel.Connectors.AzureOpenAI.UnitTests" />
3132
<InternalsVisibleTo Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI" />
3233
</ItemGroup>
3334

0 commit comments

Comments
 (0)