microsoft/autogen · error · Exception
Please set OPENAI_API_KEY environment variable.
Error message
Please set OPENAI_API_KEY environment variable.
What it means
Guard in GetStartCodeSnippet.CodeSnippet1 ('get started' example): OPENAI_API_KEY is null, so it throws before OpenAIClient is constructed with an empty key. The snippet then builds an OpenAIChatAgent on gpt-4o-mini, so a valid key is mandatory.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/GetStartCodeSnippet.cs:17
// Copyright (c) Microsoft Corporation. All rights reserved.
// GetStartCodeSnippet.cs
#region snippet_GetStartCodeSnippet
using AutoGen;
using AutoGen.Core;
using AutoGen.OpenAI;
using AutoGen.OpenAI.Extension;
using OpenAI;
#endregion snippet_GetStartCodeSnippet
public class GetStartCodeSnippet
{
public async Task CodeSnippet1()
{
#region code_snippet_1
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var openAIClient = new OpenAIClient(openAIKey);
var model = "gpt-4o-mini";
var assistantAgent = new OpenAIChatAgent(
name: "assistant",
systemMessage: "You are an assistant that help user to do some tasks.",
chatClient: openAIClient.GetChatClient(model))
.RegisterMessageConnector()
.RegisterPrintMessage(); // register a hook to print message nicely to console
// set human input mode to ALWAYS so that user always provide input
var userProxyAgent = new UserProxyAgent(
name: "user",
humanInputMode: HumanInputMode.ALWAYS)
.RegisterPrintMessage();
// start the conversation
await userProxyAgent.InitiateChatAsync(View on GitHub (pinned to 027ecf0a37)
Solutions
- export OPENAI_API_KEY=sk-... (or set it in launchSettings.json) and rerun dotnet run.
- Restart the IDE so newly set variables are visible to the debug process.
- For tests, set the variable in the test runner environment or filter out live-API tests.
- Ensure no trailing whitespace/quotes when exporting the key.
Defensive patterns
Strategy: validation
Validate before calling
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
if (string.IsNullOrWhiteSpace(openAIKey))
{
Console.Error.WriteLine("Please set OPENAI_API_KEY environment variable.");
return;
} Prevention
- Set the key before the first run of any get-started sample.
- Add env vars to launchSettings.json so IDE runs inherit them.
- Restart terminals after exporting keys.
When it happens
Trigger: Running the get-started sample without OPENAI_API_KEY; running the snippet-executing tests in AutoGen.Basic.Sample on a machine without credentials.
Common situations: First-run experience: developer clones the repo, runs samples, hasn't exported the key yet; terminal/IDE environment mismatch; key set after the process started.
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 ANTHROPIC_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/e91f6f70aef59b28.
Report an issue: GitHub.