-
Notifications
You must be signed in to change notification settings - Fork 4.3k
.Net: Fix #13183: .NET — Kernel.AddOpenAIChatClient throws an error when us… #13198
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
base: main
Are you sure you want to change the base?
Conversation
…r when using the default HttpClient.
...ectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs
Outdated
Show resolved
Hide resolved
...ectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs
Outdated
Show resolved
Hide resolved
…ceCollectionExtensions.DependencyInjection.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
…ceCollectionExtensions.DependencyInjection.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
…lient method to ensure the correct use of innerHttpClient to avoid confusion
@iangithub Please run |
{ | ||
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also check here if BaseAddress != endpoint
?
I would expect that if a base address is set, but it isn't what the caller requested via endpoint, then we should not use the default client, but also create a new one with the desired alternative endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the issue is that AddOpenAIChatClient
has both endpoint
and httpClient
. When these two settings are not consistent, it causes confusion about which one should take priority. For example, if both endpoint
and httpClient
are provided but httpClient.BaseAddress
≠ endpoint
, this leads to a conflict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but in this case only endpoint is being provided . The client from DI is the one that may potentially have a different base address set.
…dress doesn't match the endpoint, create one with the endpoint
Motivation and Context
Why is this change required?
When using AddOpenAIChatClient with a custom endpoint parameter but without providing a custom httpClient, the code would create an HttpClient without setting its BaseAddress property. This mismatch between the HttpClient configuration and the OpenAIClientOptions.Endpoint setting causes SSL/TLS handshake failures.
What problem does it solve?
This PR fixes the SSL connection error (System.ClientModel.ClientResultException: The SSL connection could not be established) that occurs when users call:
What scenario does it contribute to?
This enables users to easily connect to OpenAI-compatible endpoints (such as Azure OpenAI, local LLM servers, or other OpenAI-compatible APIs) without having to manually create and configure an HttpClient instance.
Fixes SSL connection failures when using custom endpoints with the default HttpClient.
Related Issue:
.Net: Bug: Kernel.AddOpenAIChatClient with default httpClient produces error #13183
Description
Changes made:
Fixed invalid GetOpenAIClientOptions call in the first overload (lines 40-77):
Removed the unused GetOpenAIClientOptions call at lines 56-59
Added missing endpoint and orgId parameters to the GetOpenAIClientOptions call used in OpenAIClient construction
Fixed SSL issue in the third overload with custom endpoint (lines 125-170):
Added logic to ensure the HttpClient has the correct BaseAddress when using a custom endpoint
When no custom httpClient is provided and the default client has no BaseAddress, creates a new HttpClient with BaseAddress set to the provided endpoint
This ensures consistency between the HttpClient.BaseAddress and OpenAIClientOptions.Endpoint, preventing SSL certificate validation failures
Verified AddOpenAIEmbeddingGenerator methods:
Confirmed both overloads are correctly implemented with no similar issues
The root cause was that HttpClientPipelineTransport uses the HttpClient for making requests, but when the HttpClient.BaseAddress is null and the endpoint is only set in OpenAIClientOptions.Endpoint, the SSL/TLS handshake fails due to hostname mismatch during certificate validation.
The fix ensures that when a custom endpoint is provided, the HttpClient is properly configured with the matching BaseAddress, allowing SSL/TLS to validate the certificate correctly.
Contribution Checklist