microsoft/autogen · error · ArgumentException

AZURE_OPENAI_API_KEY is not set

Error message

AZURE_OPENAI_API_KEY is not set

What it means

ArgumentException thrown in FunctionCallCodeSnippet.TwoAgentWeatherChatTestAsync when AZURE_OPENAI_API_KEY is null. This sample uses Azure OpenAI, which requires both an endpoint and a key; the key is checked first (an identical check for AZURE_OPENAI_ENDPOINT follows on the next line).

Source

Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/FunctionCallCodeSnippet.cs:120

    {
        IAgent agent = default;
        #region register_function_call_middleware
        var function = new TypeSafeFunctionCall();
        var functionCallMiddleware = new FunctionCallMiddleware(
            functions: new[] { function.WeatherReportFunctionContract },
            functionMap: new Dictionary<string, Func<string, Task<string>>>
            {
                { function.WeatherReportFunctionContract.Name, function.WeatherReportWrapper },
            });

        agent = agent!.RegisterMiddleware(functionCallMiddleware);
        var reply = await agent.SendAsync("What's the weather in Seattle today? today is 2024-01-01");
        #endregion register_function_call_middleware
    }

    public async Task TwoAgentWeatherChatTestAsync()
    {
        var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new ArgumentException("AZURE_OPENAI_API_KEY is not set");
        var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new ArgumentException("AZURE_OPENAI_ENDPOINT is not set");
        var deploymentName = "gpt-35-turbo-16k";
        var config = new AzureOpenAIConfig(endpoint, deploymentName, key);
        #region two_agent_weather_chat
        var function = new TypeSafeFunctionCall();
        var assistant = new AssistantAgent(
            "assistant",
            llmConfig: new ConversableAgentConfig
            {
                ConfigList = new[] { config },
                FunctionContracts = new[]
                {
                    function.WeatherReportFunctionContract,
                },
            });

        var user = new UserProxyAgent(
            name: "user",

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set AZURE_OPENAI_API_KEY (from Azure Portal > your Azure OpenAI resource > Keys and Endpoint).
  2. Also set AZURE_OPENAI_ENDPOINT (https://<resource>.openai.azure.com/) — the very next line throws without it.
  3. Verify the deployment name gpt-35-turbo-16k exists on that resource, since AzureOpenAIConfig needs a valid deployment.
  4. Confirm the variable names exactly match, including the AZURE_ prefix.
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Running the two-agent weather chat sample without AZURE_OPENAI_API_KEY set; note the Azure key is NOT the same as OPENAI_API_KEY — having only the OpenAI key set still triggers it.

Common situations: Developer set OPENAI_API_KEY but this snippet needs Azure credentials; Azure key stored in appsettings/user-secrets instead of environment; CI secret named differently (e.g. AZURE_OPENAI_KEY).

Related errors


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