.Net: Fix #13183: .NET — Kernel.AddOpenAIChatClient throws an error when us… #13198
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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