microsoft/autogen · error · Exception
Please set OPENAI_API_KEY environment variable.
Error message
Please set OPENAI_API_KEY environment variable.
What it means
Thrown by the 'Create_Semantic_Kernel_Chat_Agent' sample when OPENAI_API_KEY is missing. This sample builds a Microsoft.SemanticKernel Kernel and wraps Semantic Kernel's built-in ChatCompletionAgent, and the key is required by AddOpenAIChatCompletion before any agent runs. The guard makes the missing-configuration failure explicit at startup.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Chat_Agent.cs:16
// Copyright (c) Microsoft Corporation. All rights reserved.
// Create_Semantic_Kernel_Chat_Agent.cs
#region Using
using AutoGen.Core;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
#endregion Using
namespace AutoGen.SemanticKernel.Sample;
public class Create_Semantic_Kernel_Chat_Agent
{
public static async Task RunAsync()
{
#region Create_Kernel
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var modelId = "gpt-3.5-turbo";
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey)
.Build();
#endregion Create_Kernel
#region Create_ChatCompletionAgent
// The built-in ChatCompletionAgent from semantic kernel.
var chatAgent = new ChatCompletionAgent()
{
Kernel = kernel,
Name = "assistant",
Description = "You are a helpful AI assistant",
};
#endregion Create_ChatCompletionAgent
#region Create_SemanticKernelChatCompletionAgent
var messageConnector = new SemanticKernelChatMessageContentConnector();View on GitHub (pinned to 027ecf0a37)
Solutions
- Set OPENAI_API_KEY in the launching shell: export OPENAI_API_KEY=sk-... or $env:OPENAI_API_KEY='sk-...'.
- Persist the variable (setx on Windows, shell rc file on Unix) and restart the IDE/terminal.
- Put it in Properties/launchSettings.json environmentVariables for IDE debugging.
- Verify with: echo $OPENAI_API_KEY (should print sk-...) before rerunning.
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; export it or configure it in launchSettings.json."); Defensive patterns
Strategy: validation
Validate before calling
string[] required = { "OPENAI_API_KEY" };
var missing = required.Where(v => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(v))).ToArray();
if (missing.Length > 0) { Console.Error.WriteLine($"Missing env vars: {string.Join(", ", missing)}"); return; } Prevention
- Validate configuration once at process start and fail with a list of all missing keys.
- Keep env vars in launchSettings.json version-controlled with placeholder values plus a README mapping to real secrets.
When it happens
Trigger: Running the AutoGen.SemanticKernel sample's Create_Semantic_Kernel_Chat_Agent.RunAsync() in a process where OPENAI_API_KEY is null.
Common situations: Fresh repo clone; env var set at user scope but the sample launched from VS Code/WSL/Docker where it is not inherited; CI without secrets; typos in the variable name (e.g. OPENAI_APIKEY).
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/3908ad22fbe6b1bb.
Report an issue: GitHub.