microsoft/autogen · error · Exception
Please set OPENAI_API_KEY environment variable.
Error message
Please set OPENAI_API_KEY environment variable.
What it means
Startup guard in the Dynamic Group Chat getting-started sample: RunAsync reads OPENAI_API_KEY and throws a plain Exception when absent, because OpenAIClient(apiKey) cannot be constructed for the coder/other chat agents without it. The throw is sample-authored and fires before any agent or group-chat scaffolding is built.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/GettingStart/Dynamic_Group_Chat.cs:18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Dynamic_Group_Chat.cs
using AutoGen.Core;
using AutoGen.OpenAI;
using AutoGen.OpenAI.Extension;
using AutoGen.SemanticKernel;
using AutoGen.SemanticKernel.Extension;
using Microsoft.SemanticKernel;
using OpenAI;
namespace AutoGen.Basic.Sample;
public class Dynamic_Group_Chat
{
public static async Task RunAsync()
{
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var model = "gpt-4o-mini";
#region Create_Coder
var openaiClient = new OpenAIClient(apiKey);
var coder = new OpenAIChatAgent(
chatClient: openaiClient.GetChatClient(model),
name: "coder",
systemMessage: "You are a C# coder, when writing csharp code, please put the code between ```csharp and ```")
.RegisterMessageConnector() // convert OpenAI message to AutoGen message
.RegisterPrintMessage(); // print the message content
#endregion Create_Coder
#region Create_Commenter
var kernel = Kernel
.CreateBuilder()
.AddOpenAIChatCompletion(modelId: model, apiKey: apiKey)
.Build();
var commenter = new SemanticKernelAgent(View on GitHub (pinned to 027ecf0a37)
Solutions
- Set OPENAI_API_KEY (export on bash, $env:/setx on Windows) in the environment that launches the sample.
- Add the key to your IDE's debug environment profile if you run the sample from Visual Studio/Rider.
- Verify the process actually sees the variable (quick console print) — dotnet does not load .env automatically.
- Ensure the key has gpt-4o-mini access, since the sample hard-codes that model.
Example fix
// before
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
// after
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("OPENAI_API_KEY is not set. Export it before running Dynamic_Group_Chat."); Defensive patterns
Strategy: validation
Validate before calling
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("Set OPENAI_API_KEY before running Dynamic_Group_Chat.");
return;
} Try / catch
try
{
await Dynamic_Group_Chat.RunAsync();
}
catch (Exception ex) when (ex.Message.Contains("OPENAI_API_KEY"))
{
Console.Error.WriteLine($"Configuration error: {ex.Message}");
} Prevention
- Follow the getting-started setup step that exports the API key before running samples.
- Set keys in the IDE profile when launching from Visual Studio/Rider so they survive across sessions.
- dotnet run does not read .env files — export variables explicitly or load them in code.
When it happens
Trigger: Invoking Dynamic_Group_Chat.RunAsync() with OPENAI_API_KEY unset or empty in the launching process. It fails at the first line, before the coder OpenAIChatAgent is created.
Common situations: New users following the getting-started docs but skipping the API-key setup step; running samples in CI without OpenAI secrets; the variable set in a parent shell but the sample launched from a fresh terminal or IDE.
Related errors
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/f84b70b714b3bb14.
Report an issue: GitHub.