From cec06af4d3c62acc2216598ef71e5d2a99b5ba56 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 30 Sep 2025 13:22:49 +0800 Subject: [PATCH 1/5] =?UTF-8?q?Fix=20#13183:=20.NET=20=E2=80=94=20Kernel.A?= =?UTF-8?q?ddOpenAIChatClient=20throws=20an=20error=20when=20using=20the?= =?UTF-8?q?=20default=20HttpClient.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ollectionExtensions.DependencyInjection.cs | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs index c4743350c2b8..4cb32ee3a7ab 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs @@ -53,14 +53,12 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) { var loggerFactory = serviceProvider.GetService(); - ClientCore.GetOpenAIClientOptions( - endpoint: null, - orgId: orgId, - httpClient: HttpClientProvider.GetHttpClient(httpClient, serviceProvider)); - var builder = new OpenAIClient( credential: new ApiKeyCredential(apiKey ?? SingleSpace), - options: ClientCore.GetOpenAIClientOptions(HttpClientProvider.GetHttpClient(httpClient, serviceProvider))) + options: ClientCore.GetOpenAIClientOptions( + httpClient: HttpClientProvider.GetHttpClient(httpClient, serviceProvider), + endpoint: null, + orgId: orgId)) .GetChatClient(modelId) .AsIChatClient() .AsBuilder() @@ -153,10 +151,33 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) { var loggerFactory = serviceProvider.GetService(); + // Get or create HttpClient with proper BaseAddress for the endpoint + HttpClient clientHttpClient; + if (httpClient is not null) + { + clientHttpClient = httpClient; + } + else + { + var defaultClient = HttpClientProvider.GetHttpClient(serviceProvider); + // If using default client and it doesn't have BaseAddress set, create one with the endpoint + if (defaultClient.BaseAddress is null) + { + clientHttpClient = new HttpClient(new HttpClientHandler()) + { + BaseAddress = endpoint + }; + } + else + { + clientHttpClient = defaultClient; + } + } + var builder = new OpenAIClient( credential: new ApiKeyCredential(apiKey ?? SingleSpace), options: ClientCore.GetOpenAIClientOptions( - httpClient: HttpClientProvider.GetHttpClient(httpClient, serviceProvider), + httpClient: clientHttpClient, endpoint: endpoint, orgId: orgId)) .GetChatClient(modelId) From 8ea9ba6bcbdb5b86f6ef5ed9d2e328227819698b Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 3 Oct 2025 09:22:17 +0800 Subject: [PATCH 2/5] Update dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> --- .../OpenAIServiceCollectionExtensions.DependencyInjection.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs index 4cb32ee3a7ab..75c1dd38c689 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs @@ -163,7 +163,10 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) // If using default client and it doesn't have BaseAddress set, create one with the endpoint if (defaultClient.BaseAddress is null) { - clientHttpClient = new HttpClient(new HttpClientHandler()) + Verify.NotNull(endpoint); + + // A new one needs to be created as we can't cross boundaries and modify an existing client + innerHttpClient = HttpClientProvider.GetHttpClient() { BaseAddress = endpoint }; From 5f8a62c0b1967c87fe1eab1bf5cc79c1117d164d Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 3 Oct 2025 09:22:25 +0800 Subject: [PATCH 3/5] Update dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> --- .../OpenAIServiceCollectionExtensions.DependencyInjection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs index 75c1dd38c689..409b59579d2a 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs @@ -152,7 +152,7 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) var loggerFactory = serviceProvider.GetService(); // Get or create HttpClient with proper BaseAddress for the endpoint - HttpClient clientHttpClient; + HttpClient innerHttpClient; if (httpClient is not null) { clientHttpClient = httpClient; From 481420f10d79a3abee913da1442600b02aee4c34 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 3 Oct 2025 09:51:45 +0800 Subject: [PATCH 4/5] Corrected the naming of the HttpClient variable in the AddOpenAIChatClient method to ensure the correct use of innerHttpClient to avoid confusion --- ...viceCollectionExtensions.DependencyInjection.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs index 409b59579d2a..31f56127dfcb 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs @@ -155,7 +155,7 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) HttpClient innerHttpClient; if (httpClient is not null) { - clientHttpClient = httpClient; + innerHttpClient = httpClient; } else { @@ -164,23 +164,21 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) if (defaultClient.BaseAddress is null) { Verify.NotNull(endpoint); - + // A new one needs to be created as we can't cross boundaries and modify an existing client - innerHttpClient = HttpClientProvider.GetHttpClient() - { - BaseAddress = endpoint - }; + innerHttpClient = HttpClientProvider.GetHttpClient(); + innerHttpClient.BaseAddress = endpoint; } else { - clientHttpClient = defaultClient; + innerHttpClient = defaultClient; } } var builder = new OpenAIClient( credential: new ApiKeyCredential(apiKey ?? SingleSpace), options: ClientCore.GetOpenAIClientOptions( - httpClient: clientHttpClient, + httpClient: innerHttpClient, endpoint: endpoint, orgId: orgId)) .GetChatClient(modelId) From 6af9919e6d4c60ae9fa06efce8a62fb5a5c0fe3a Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 8 Oct 2025 09:21:52 +0800 Subject: [PATCH 5/5] fix dotnet format error --- dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs b/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs index 21504eda40c2..b3045c3e9f5d 100644 --- a/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs +++ b/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Text; using System.Text.Json; using Microsoft.SemanticKernel.ChatCompletion;