microsoft/autogen · error · Exception

Please set OPENAI_API_KEY environment variable.

Error message

Please set OPENAI_API_KEY environment variable.

What it means

Guard inside the shared sample helper LLMConfiguration.GetOpenAIGPT4o_mini(): it reads OPENAI_API_KEY and throws when absent, because OpenAIClient(openAIKey).GetChatClient("gpt-4o-mini") cannot be built without a key. Since many samples funnel through this helper, this single line is the source of OPENAI_API_KEY failures across several example files.

Source

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

// Copyright (c) Microsoft Corporation. All rights reserved.
// LLMConfiguration.cs

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 OPENAI_API_KEY in the environment that launches the samples and re-run.
  2. Prefer configuring all keys the helper file reads (OPENAI_API_KEY, AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOY_NAME) so any sample path works.
  3. Add the keys to launchSettings.json or CI secret variables, not to source.
  4. If you only have Azure credentials, call GetAzureOpenAIGPT3_5_Turbo()-based samples instead of the OpenAI helper.

Example fix

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

// after
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
    ?? throw new InvalidOperationException("OPENAI_API_KEY is not set. LLMConfiguration requires it for the public OpenAI endpoint.");
Defensive patterns

Strategy: validation

Validate before calling

var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
if (string.IsNullOrWhiteSpace(openAIKey))
{
    throw new InvalidOperationException("OPENAI_API_KEY is not set. Export it before calling GetOpenAIGPT4o_mini().");
}

Try / catch

try
{
    var client = LLMConfiguration.GetOpenAIGPT4o_mini();
}
catch (Exception ex) when (ex.Message.Contains("OPENAI_API_KEY"))
{
    Console.Error.WriteLine($"Missing OpenAI credential: {ex.Message}");
}

Prevention

When it happens

Trigger: Any sample calling LLMConfiguration.GetOpenAIGPT4o_mini() (e.g. Example17_ReActAgent) with OPENAI_API_KEY unset. The throw happens before the ChatClient is returned.

Common situations: Running the full sample suite with partial credentials; IDE/CI processes missing exported variables; users who set only the AZURE_* variables and then call an OpenAI-based helper.

Related errors


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