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 FSM_Group_Chat.RunAsync: the sample reads OPENAI_API_KEY and throws a plain Exception when it is missing, because OpenAIClient(apiKey) is required to build the chat clients for the application, assistant, and user agents. The throw is in sample code and happens before any agent factory or the group-chat graph is created.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/GettingStart/FSM_Group_Chat.cs:151
systemMessage: """
You are a user who is filling an application form. Simply provide the information as requested and answer the questions, don't do anything else.
here's some personal information about you:
- name: John Doe
- email: 1234567@gmail.com
- phone: 123-456-7890
- address: 1234 Main St, Redmond, WA 98052
- want to receive update? true
""")
.RegisterMessageConnector()
.RegisterPrintMessage();
#endregion Create_User_Agent
return chatAgent;
}
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";
var openaiClient = new OpenAIClient(apiKey);
var chatClient = openaiClient.GetChatClient(model);
var applicationAgent = await CreateSaveProgressAgent(chatClient);
var assistantAgent = await CreateAssistantAgent(chatClient);
var userAgent = await CreateUserAgent(chatClient);
#region Create_Graph
var userToApplicationTransition = Transition.Create(userAgent, applicationAgent);
var applicationToAssistantTransition = Transition.Create(applicationAgent, assistantAgent);
var assistantToUserTransition = Transition.Create(assistantAgent, userAgent);
var workflow = new Graph(
[
userToApplicationTransition,
applicationToAssistantTransition,
assistantToUserTransition,
]);View on GitHub (pinned to 027ecf0a37)
Solutions
- Export OPENAI_API_KEY in the launching shell (or set it via launchSettings.json / IDE run configuration) and re-run.
- Confirm the variable name is exactly OPENAI_API_KEY and the value has no stray quotes/whitespace.
- Ensure the key can access gpt-4o-mini, the model hard-coded in this sample.
- If you meant to use Azure OpenAI, switch to the Azure samples (Connect_To_Azure_OpenAI) which read AZURE_* variables instead.
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; configure it in your environment or IDE run profile."); 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 FSM_Group_Chat.");
return;
} Try / catch
try
{
await FSM_Group_Chat.RunAsync();
}
catch (Exception ex) when (ex.Message.Contains("OPENAI_API_KEY"))
{
Console.Error.WriteLine($"Configuration error: {ex.Message}");
} Prevention
- Configure OPENAI_API_KEY once in the environment/IDE profile shared by all getting-started samples.
- Validate credentials at process start before building agents.
- Distinguish this OpenAI-based FSM sample from the Azure-based sequential sample when choosing which keys to set.
When it happens
Trigger: Calling FSM_Group_Chat.RunAsync() with OPENAI_API_KEY unset. Note this file contains an identical guard inside CreateSaveProgressAgent's caller chain; whichever executes first throws.
Common situations: Running the getting-started samples without configuring OpenAI credentials; IDE-launched processes missing shell-exported variables; CI without injected secrets.
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/d3620e41a246a06b.
Report an issue: GitHub.