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 Basics migration sample throws InvalidOperationException when AZURE_OPENAI_ENDPOINT is not set. This sample uses the OpenAI Responses API (OpenAI.Responses namespace) via Azure OpenAI, comparing SK's OpenAIResponseAgent with the Agent Framework equivalent. The OPENAI001 and SKEXP0110 pragmas suppress preview-API warnings.

Source

Thrown at dotnet/samples/AgentFrameworkMigration/AzureOpenAIResponses/Step01_Basics/Program.cs:13

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

using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
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 responseClient = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
        .GetResponsesClient();
    OpenAIResponseAgent agent = new(responseClient, deploymentName)
    {
        Name = "Joker",

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. Ensure the deployed model supports the Responses API
  4. Run az login 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. Azure OpenAI Responses samples require it.");
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 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/Step01_Basics without AZURE_OPENAI_ENDPOINT. The throw at line 13 fires before SKAgentAsync, SKAgent_As_AFAgentAsync, or AFAgentAsync execute.

Common situations: Confusing the Responses API sample with the standard chat completion sample (same env var, different agent type); the deployed model does not support the Responses API; running without az login for AzureCliCredential.

Related errors


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