microsoft/autogen · error · Exception

Please set AZURE_OPENAI_API_KEY environment variable.

Error message

Please set AZURE_OPENAI_API_KEY environment variable.

What it means

Guard in LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo(): it requires AZURE_OPENAI_API_KEY because AzureOpenAIConfig(endpoint, deployName, azureOpenAIKey) needs the key to authenticate against your Azure OpenAI resource. This is the first of three Azure variables the helper validates (key, then endpoint, then deployment name), so seeing this message means the key was missing before the others were checked.

Source

Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/LLMConfiguration.cs:21

using OpenAI;
using OpenAI.Chat;

namespace AutoGen.Basic.Sample;

internal static class LLMConfiguration
{
    public static ChatClient GetOpenAIGPT4o_mini()
    {
        var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
        var modelId = "gpt-4o-mini";

        return new OpenAIClient(openAIKey).GetChatClient(modelId);
    }

    public static AzureOpenAIConfig GetAzureOpenAIGPT3_5_Turbo(string? deployName = null)
    {
        var azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
        var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
        deployName = deployName ?? Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
        return new AzureOpenAIConfig(endpoint, deployName, azureOpenAIKey);
    }
}

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set AZURE_OPENAI_API_KEY to a key from your Azure OpenAI resource (Azure Portal -> your resource -> Keys and Endpoint) and re-run.
  2. Set the companion variables AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOY_NAME in the same environment — the helper checks them next.
  3. Add all three to launchSettings.json / CI secret variables rather than hard-coding.
  4. Verify the key is from an Azure OpenAI resource, not a generic OpenAI sk- key.

Example fix

// before
var azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");

// after
var azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")
    ?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY is not set. Copy a key from your Azure OpenAI resource (Keys and Endpoint) and export it.");
Defensive patterns

Strategy: validation

Validate before calling

string[] required = ["AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME"];
var missing = required.Where(v => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(v))).ToArray();
if (missing.Length > 0)
{
    throw new InvalidOperationException($"Missing Azure OpenAI configuration: {string.Join(", ", missing)}");
}

Try / catch

try
{
    var config = LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo();
}
catch (Exception ex) when (ex.Message.Contains("AZURE_OPENAI"))
{
    Console.Error.WriteLine($"Incomplete Azure OpenAI config: {ex.Message}");
}

Prevention

When it happens

Trigger: Any sample calling LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo() (e.g. the Bing search agent in the sequential group-chat sample) with AZURE_OPENAI_API_KEY unset. OPENAI_API_KEY-based paths are unaffected.

Common situations: Users who configured public-OpenAI samples and then run an Azure sample with different variable names; key stored in Azure Key Vault but not exported; CI templates missing the Azure secrets; wrong variable spelling (AZURE_OPENAI_KEY vs AZURE_OPENAI_API_KEY).

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/5d88b65f10a8d751. Report an issue: GitHub.