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 Responses DependencyInjection migration sample throws InvalidOperationException when AZURE_OPENAI_ENDPOINT is unset. This sample shows DI-based agent registration using the OpenAI Responses API on Azure OpenAI. The endpoint is needed at composition time to build the Responses API client that backs the registered AIAgent.

Source

Thrown at dotnet/samples/AgentFrameworkMigration/AzureOpenAIResponses/Step04_DependencyInjection/Program.cs:14

// 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.Agents.OpenAI;
using OpenAI.Responses;

#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

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 SKAgentAsync();
await SKAgent_As_AFAgentAsync();
await AFAgentAsync();

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

    var serviceCollection = new ServiceCollection();
    serviceCollection.AddTransient<Microsoft.SemanticKernel.Agents.Agent>((sp)
        => new OpenAIResponseAgent(new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
        .GetResponsesClient(), deploymentName)
        {

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 using VS, add the variable to 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.md for configuration.");
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 Responses API 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 AzureOpenAIResponses/Step04_DependencyInjection without AZURE_OPENAI_ENDPOINT. The throw at line 14 fires before any DI container or agent factory is invoked.

Common situations: DI samples read env vars directly, not IConfiguration or user secrets; the endpoint may be in a sibling project's launchSettings.json; running from CLI without the VS launch profile.

Related errors


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