microsoft/semantic-kernel · error · InvalidOperationException

AZURE_OPENAI_ENDPOINT is not set.

Error message

AZURE_OPENAI_ENDPOINT is not set.

What it means

The Azure OpenAI DependencyInjection migration sample throws InvalidOperationException when AZURE_OPENAI_ENDPOINT is unset. This sample shows DI-based agent registration, comparing SK's ChatCompletionAgent (requiring a Kernel from the service container) with AF's keyed-singleton AIAgent pattern. The endpoint is needed at composition time to build the chat client registered in the container.

Source

Thrown at dotnet/samples/AgentFrameworkMigration/AzureOpenAI/Step03_DependencyInjection/Program.cs:12

// Copyright (c) Microsoft. All rights reserved.

using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using OpenAI.Chat;

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o";
var userInput = "Tell me a joke about a pirate.";

Console.WriteLine($"User Input: {userInput}");

await SKAgent();
await SKAgent_As_AFAgentAsync();
await AFAgent();

async Task SKAgent()
{
    Console.WriteLine("\n=== SK Agent ===\n");

    var serviceCollection = new ServiceCollection();
    serviceCollection.AddKernel().AddAzureOpenAIChatClient(deploymentName, endpoint, new AzureCliCredential());
    serviceCollection.AddTransient((sp) => new ChatCompletionAgent()
    {
        Kernel = sp.GetRequiredService<Kernel>(),

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set AZURE_OPENAI_ENDPOINT to your Azure OpenAI resource URL
  2. Optionally set AZURE_OPENAI_DEPLOYMENT_NAME (defaults to gpt-4o)
  3. If running from VS, add the variable in Properties/launchSettings.json
  4. Ensure az login is current for AzureCliCredential

Example fix

// before
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");

// after
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
if (string.IsNullOrWhiteSpace(endpoint))
    throw new InvalidOperationException(
        "AZURE_OPENAI_ENDPOINT is not set. See README Environment Variables section.");
Defensive patterns

Strategy: validation

Validate before calling

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
if (string.IsNullOrWhiteSpace(endpoint))
{
    Console.Error.WriteLine("AZURE_OPENAI_ENDPOINT is not set. Required for DI samples.");
    return;
}

Type guard

static bool IsAzureOpenAIEndpointConfigured() =>
    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"));

Try / catch

try
{
    var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
        ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AZURE_OPENAI_ENDPOINT"))
{
    Console.Error.WriteLine(ex.Message);
    return;
}

Prevention

When it happens

Trigger: Running AzureOpenAI/Step03_DependencyInjection without AZURE_OPENAI_ENDPOINT. The throw at line 12 fires before any ServiceCollection is created or agents registered.

Common situations: Expecting DI samples to use IConfiguration or user secrets (they read env vars directly); the endpoint is in launchSettings.json of a different sample project; running from CLI where the VS launch profile env vars do not apply.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/7821e1c701f221dc. Report an issue: GitHub.