microsoft/autogen · error · InvalidOperationException
Please set environment variable OPENAI_API_KEY
Error message
Please set environment variable OPENAI_API_KEY
What it means
Guard in the o1-preview connection sample: it reads OPENAI_API_KEY and throws InvalidOperationException when absent, because OpenAIClient(apiKey) must authenticate the o1-preview chat client. Sample-authored fail-fast: thrown before the OpenAIChatAgent (configured with null systemMessage/temperature/maxTokens for o1-preview limitations) is created.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs:14
// Copyright (c) Microsoft Corporation. All rights reserved.
// Connect_To_OpenAI_o1_preview.cs
using AutoGen.Core;
using OpenAI;
namespace AutoGen.OpenAI.Sample;
public class Connect_To_OpenAI_o1_preview
{
public static async Task RunAsync()
{
#region create_agent
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable OPENAI_API_KEY");
var openAIClient = new OpenAIClient(apiKey);
// until 2024/09/12
// openai o1-preview doesn't support systemMessage, temperature, maxTokens, streaming output
// so in order to use OpenAIChatAgent with o1-preview, you need to set those parameters to null
var agent = new OpenAIChatAgent(
chatClient: openAIClient.GetChatClient("o1-preview"),
name: "assistant",
systemMessage: null,
temperature: null,
maxTokens: null,
seed: 0)
// by using RegisterMiddleware instead of RegisterStreamingMiddleware
// it turns an IStreamingAgent into an IAgent and disables streaming
.RegisterMiddleware(new OpenAIChatRequestMessageConnector())
.RegisterPrintMessage();
#endregion create_agent
View on GitHub (pinned to 027ecf0a37)
Solutions
- Export OPENAI_API_KEY in the shell or IDE run profile that launches the sample.
- Confirm your organization has been granted access to the o1-preview model, otherwise the next failure is an API-level model-access error.
- Respect the sample's o1-preview constraints (no systemMessage, temperature, maxTokens, streaming) when adapting the code.
- If o1-preview is unavailable to your account, swap the model name to one you can access.
Example fix
// before
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable OPENAI_API_KEY");
// after
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("OPENAI_API_KEY is not set; export it before running this o1-preview sample."); 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 the o1-preview sample.");
return;
} Try / catch
try
{
await Connect_To_OpenAI_o1_preview.RunAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("OPENAI_API_KEY"))
{
Console.Error.WriteLine($"Missing credential: {ex.Message}");
} Prevention
- Verify model access (o1-preview requires org-level grants) before running, or the API call fails after startup.
- Respect o1-preview constraints: null systemMessage, temperature, maxTokens as the sample shows.
- Centralize key checks at startup instead of relying on the sample's inline throw.
When it happens
Trigger: Calling Connect_To_OpenAI_o1_preview.RunAsync() with OPENAI_API_KEY unset or empty in the process. Nothing network-related runs before the throw.
Common situations: Running the OpenAI sample project without credentials; key expired or not granted o1-preview access (that fails later at the API, not here); IDE processes missing shell exports.
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/2ae979cb785343a7.
Report an issue: GitHub.