SemanticKernel Azure AI Foundry Inference: Azure.RequestFailedException: Unauthorized

Michael Vivet 40 Reputation points
2025-09-09T16:29:47.6033333+00:00

Using Microsoft.SemanticKernel library, I have up until today been able to connect and use deployed models (gpt-4.1, text-embedding-ada-002) in Azure AI Foundry with Microsoft.SemanticKernel.Connectors.AzureAIInference.

Now I get a RequestFailedException with the following 401 error.

Azure.RequestFailedException: Unauthorized. Access token is missing, invalid, audience is incorrect (https://ai.azure.com), or have expired.

Status: 401 (Unauthorized)
Content:
{ "statusCode": 401, "message": "Unauthorized. Access token is missing, invalid, audience is incorrect (https://ai.azure.com), or have expired." }
Headers:
apim-request-id: REDACTED
Strict-Transport-Security: REDACTED
X-Content-Type-Options: REDACTED
Date: Tue, 09 Sep 2025 16:20:08 GMT
Content-Length: 146
Content-Type: application/json

I have not changed my code, and not changed the environmental variables for endpoint or apikey.

I tried making a small console application, and that fails as well with the same exception:

var modelId = "gpt-4.1";
var endpoint = "<my-azure-foundry-project-uri>";
var apiKey = "<my-apikey>";

var services = new ServiceCollection();
services.AddAzureAIInferenceChatCompletion(modelId, apiKey, new Uri(endpoint));

var chatCompletionService = services.BuildServiceProvider().GetRequiredService<IChatCompletionService>();

var history = new ChatHistory();
history.AddUserMessage("What's the weather");

var result = await chatCompletionService.GetChatMessageContentAsync(history);
Console.WriteLine(result);

Any insights would be appreciated.

Foundry Tools
Foundry Tools

Formerly known as Azure AI Services or Azure Cognitive Services is a unified collection of prebuilt AI capabilities within the Microsoft Foundry platform


Answer accepted by question author
Gowtham CP 8,120 Reputation points Volunteer Moderator
2025-09-10T07:16:19.87+00:00

Hi Michael Vivet ,

Thank you for reaching out on Microsoft Q&A.

You can fix the Unauthorized error by using the models inference endpoint instead of the project URI and authenticating correctly. Use:

// Using project API key
var endpoint = new Uri("https://<resource>.services.ai.azure.com/models");
services.AddAzureAIInferenceChatCompletion("gpt-4.1", apiKey: "<PROJECT_API_KEY>", endpoint: endpoint);

// Or using Entra ID token
var endpoint = new Uri("https://<resource>.services.ai.azure.com/models");
services.AddAzureAIInferenceChatCompletion("gpt-4.1", credential: new DefaultAzureCredential(), endpoint: endpoint);

References:

Models endpoint & auth

SK AddAzureAIInferenceChatCompletion

Q&A confirmation

SK GitHub discussion

I hope this helps! If the information is useful, please accept the answer and upvote so it can assist other community members as well.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Vivet 40 Reputation points
    2025-09-11T07:59:41.0066667+00:00

    That's not really solving the problem. I need the project url so i can't easily switch between models

    Using the project URL worked last week, did anything change?

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.