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 OpenAICodeSnippet.CreateOpenAIChatAgentAsync: OPENAI_API_KEY is null, so it throws before OpenAIClient(openAIKey) is constructed (an empty/invalid key would otherwise surface later as an OpenAI SDK auth error). This snippet builds an OpenAIChatAgent on gpt-4o-mini.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/OpenAICodeSnippet.cs:35
[Function]
public async Task<string> GetWeather(string location)
{
return "The weather in " + location + " is sunny.";
}
}
#endregion weather_function
public partial class OpenAICodeSnippet
{
[Function]
public async Task<string> GetWeather(string location)
{
return "The weather in " + location + " is sunny.";
}
public async Task CreateOpenAIChatAgentAsync()
{
#region create_openai_chat_agent
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var modelId = "gpt-4o-mini";
var openAIClient = new OpenAIClient(openAIKey);
// create an open ai chat agent
var openAIChatAgent = new OpenAIChatAgent(
chatClient: openAIClient.GetChatClient(modelId),
name: "assistant",
systemMessage: "You are an assistant that help user to do some tasks.");
// OpenAIChatAgent supports the following message types:
// - IMessage<ChatRequestMessage> where ChatRequestMessage is from Azure.AI.OpenAI
var helloMessage = new UserChatMessage("Hello");
// Use MessageEnvelope.Create to create an IMessage<ChatRequestMessage>
var chatMessageContent = MessageEnvelope.Create(helloMessage);
var reply = await openAIChatAgent.SendAsync(chatMessageContent);
View on GitHub (pinned to 027ecf0a37)
Solutions
- Export OPENAI_API_KEY and rerun; verify with echo $OPENAI_API_KEY.
- Add the variable to the CI pipeline secrets or launchSettings.json for IDE runs.
- Load a .env file at startup if the project supports it.
- Restart the process after setting the variable.
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OPENAI_API_KEY")))
{
Console.Error.WriteLine("Please set OPENAI_API_KEY environment variable.");
return;
} Prevention
- Export OPENAI_API_KEY before running OpenAI snippets/tests.
- Propagate the variable to test hosts in CI.
- Fail fast at startup with a list of all missing credentials.
When it happens
Trigger: Running the OpenAI chat-agent snippet (or its executing test) without OPENAI_API_KEY in the process environment.
Common situations: Fresh environment without the key exported; snippet executed by doc tests in CI lacking the secret; key set in a different shell session.
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/994ee42c6b706c5f.
Report an issue: GitHub.