microsoft/autogen · error · InvalidOperationException
Please set environment variable AZURE_OPENAI_API_KEY
Error message
Please set environment variable AZURE_OPENAI_API_KEY
What it means
Guard in the Connect_To_Azure_OpenAI sample: it reads AZURE_OPENAI_API_KEY and throws InvalidOperationException when it is missing, because AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)) cannot authenticate without it. It is sample-authored fail-fast code executed before the OpenAIChatAgent is constructed; note this sample uses InvalidOperationException, unlike the Basic samples' plain Exception.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.OpenAI.Sample/Connect_To_Azure_OpenAI.cs:18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Connect_To_Azure_OpenAI.cs
#region using_statement
using System.ClientModel;
using AutoGen.Core;
using AutoGen.OpenAI.Extension;
using Azure.AI.OpenAI;
#endregion using_statement
namespace AutoGen.OpenAI.Sample;
public class Connect_To_Azure_OpenAI
{
public static async Task RunAsync()
{
#region create_agent
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable AZURE_OPENAI_API_KEY");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("Please set environment variable AZURE_OPENAI_ENDPOINT");
var model = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? "gpt-4o-mini";
// Use AzureOpenAIClient to connect to openai model deployed on azure.
// The AzureOpenAIClient comes from Azure.AI.OpenAI package
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey));
var agent = new OpenAIChatAgent(
chatClient: openAIClient.GetChatClient(model),
name: "assistant",
systemMessage: "You are a helpful assistant designed to output JSON.",
seed: 0)
.RegisterMessageConnector()
.RegisterPrintMessage();
#endregion create_agent
#region send_message
await agent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?");View on GitHub (pinned to 027ecf0a37)
Solutions
- Set AZURE_OPENAI_API_KEY from your Azure OpenAI resource (Portal -> Keys and Endpoint) in the launching environment.
- Also set AZURE_OPENAI_ENDPOINT (required next line) and optionally AZURE_OPENAI_DEPLOY_NAME (defaults model to gpt-4o-mini).
- Prefer IUserSecrets/CI secret variables over plain exports for long-lived setups.
- Confirm you are not mixing SDK keys — this must be an Azure OpenAI resource key, not an sk- public key.
Example fix
// before
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable AZURE_OPENAI_API_KEY");
// after
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")
?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY is not set. Copy a key from your Azure OpenAI resource and export it before running."); Defensive patterns
Strategy: validation
Validate before calling
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(endpoint))
{
throw new InvalidOperationException("Both AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT are required.");
} Try / catch
try
{
await Connect_To_Azure_OpenAI.RunAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AZURE_OPENAI_API_KEY"))
{
Console.Error.WriteLine($"Missing Azure credential: {ex.Message}");
} Prevention
- Configure the full Azure trio (key, endpoint, optional deployment name) before running Azure samples.
- Use an Azure OpenAI resource key, not a public OpenAI sk- key, with AzureOpenAIClient.
- Prefer secret managers (user-secrets, CI variables) over shell exports for long-lived setups.
When it happens
Trigger: Calling Connect_To_Azure_OpenAI.RunAsync() with AZURE_OPENAI_API_KEY unset. The throw fires before AZURE_OPENAI_ENDPOINT is read, so an endpoint error cannot occur until the key is provided.
Common situations: Running OpenAI-sample suites with Azure credentials absent; key configured only for the public OpenAI endpoint; CI jobs without Azure secrets; fresh environment after cloning.
Related errors
- Please set AZURE_OPENAI_API_KEY environment variable.
- Please set AZURE_OPENAI_ENDPOINT environment variable.
- BING_API_KEY environment variable is not set
- 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/1b2ca3c80bb9ddf6.
Report an issue: GitHub.