Skip to content

.Net: add Azure AI Foundry api-key header HttpClient sample (#10455) - #14609

Open
SparkyPunk (sparkysolutions10-pixel) wants to merge 2 commits into
microsoft:mainfrom
sparkysolutions10-pixel:fix/dotnet-azure-ai-foundry-api-key-10455
Open

SparkyPunk (sparkysolutions10-pixel) wants to merge 2 commits into
microsoft:mainfrom
sparkysolutions10-pixel:fix/dotnet-azure-ai-foundry-api-key-10455

Conversation

@sparkysolutions10-pixel

Copy link
Copy Markdown

Problem & Solution Overview

Fixes #10455

Problem Description

When connecting to Azure AI Foundry model inference endpoints (e.g. Phi-3, Mistral, LLaMA on https://<resource>.services.ai.azure.com/models) using AddOpenAIChatCompletion, the underlying client defaults to sending Authorization: Bearer <key>, which results in 401 Unauthorized because Azure AI Foundry endpoints strictly require the api-key header.

As noted by maintainers in #10455, the underlying OpenAI SDK client is protocol-agnostic, and custom headers should be handled via a custom HttpClient / DelegatingHandler. However, Semantic Kernel lacked a dedicated sample showing how developers can configure this seamless integration.

Solution Summary

Added UseCustomHttpClientWithApiKeyHeaderForAzureAIFoundry and AzureAIFoundryApiKeyHandler in dotnet/samples/Concepts/ChatCompletion/Connectors_CustomHttpClient.cs:

  1. Demonstrates an AzureAIFoundryApiKeyHandler inheriting from DelegatingHandler.
  2. Automatically injects the required api-key header on outgoing requests to Azure AI Foundry deployments.
  3. Provides a clean, reusable reference pattern for .NET developers without modifying the core OpenAI client abstractions.

Verification & Testing

  • Sample and test case compile cleanly against Connectors_CustomHttpClient
  • Pre-commit formatting rules respected (.editorconfig)
  • Developer Certificate of Origin (DCO) signed (Signed-off-by)
  • Microsoft Contributor License Agreement (CLA) accepted

…#10455)

Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 07:40
@semantic-kernel-automation semantic-kernel-automation Bot added the .NET Issue or Pull requests regarding .NET code label Oct 16, 2026
@github-actions github-actions Bot changed the title .NET: add Azure AI Foundry api-key header HttpClient sample (#10455) .Net: add Azure AI Foundry api-key header HttpClient sample (#10455) Oct 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The sample omits the required inference API version, and its test does not exercise header injection.

2 open findings
What changed in this PR

Adds a .NET sample showing how to customize OpenAI connector authentication for Azure AI Foundry, addressing #10455 without changing core client abstractions.

Changes:

  • Adds a handler that injects api-key while preserving existing values.
  • Demonstrates registering the custom HTTP client with a kernel.
File Description
dotnet/​samples/​Concepts/​ChatCompletion/​Connectors_CustomHttpClient.cs Adds the Foundry configuration sample, authentication handler, and kernel-construction assertion.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
modelId: "Phi-3-small-8k-instruct",
endpoint: new Uri("https://my-resource.services.ai.azure.com/models"),
httpClient: httpClient)
.Build();

Assert.NotNull(kernel);
…icrosoft#10455)

Addresses Copilot code-review feedback by appending api-version and adding in-memory unit tests.

Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>
@sparkysolutions10-pixel

Copy link
Copy Markdown
Author

Thank you for the constructive review Copilot! 🙌

Both recommended findings have been addressed in commit f4ec05fd:

  1. Added api-version Query Injection: AzureAIFoundryApiKeyHandler now inspects the request URI and appends ?api-version=2024-05-01-preview while preserving any existing query parameters.
  2. Added Isolated Recording Unit Tests: Added AzureAIFoundryApiKeyHandler_InjectsHeaderAndApiVersionAsync and AzureAIFoundryApiKeyHandler_PreservesExistingHeaderAsync utilizing an in-memory RecordingHandler to verify that api-key header and api-version parameters are injected and existing headers are preserved, with zero live network calls.

Ready for maintainer review! 🚀

This branch was previously deployed

1 inactive deployment
github-app-auth — f4ec05fd Deployed Oct 16, 2026 by sparkysolutions10-pixel via add_label #29364
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Bug: .NET api-key header not sent - OpenAIChatCompletion not working with Azure AI Foundry deployments

2 participants