microsoft/autogen · error · Exception
Please set OPENAI_API_KEY environment variable.
Error message
Please set OPENAI_API_KEY environment variable.
What it means
Same file as error 83: after the BING_API_KEY guard passes, the sample throws this if OPENAI_API_KEY is also missing, because it builds the chat-completion kernel with AddOpenAIChatCompletion('gpt-3.5-turbo', openAIKey). The sample needs two independent keys: a Bing key for search and an OpenAI key for the LLM.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs:20
// Use_Bing_Search_With_Semantic_Kernel_Agent.cs
using AutoGen.Core;
using AutoGen.SemanticKernel.Extension;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Web;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
namespace AutoGen.SemanticKernel.Sample;
public class Use_Bing_Search_With_Semantic_Kernel_Agent
{
public static async Task RunAsync()
{
var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY") ?? throw new Exception("BING_API_KEY environment variable is not set");
var bingSearch = new BingConnector(bingApiKey);
var webSearchPlugin = new WebSearchEnginePlugin(bingSearch);
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var modelId = "gpt-3.5-turbo";
var kernelBuilder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);
kernelBuilder.Plugins.AddFromObject(webSearchPlugin);
var kernel = kernelBuilder.Build();
var skAgent = new SemanticKernelAgent(
kernel: kernel,
name: "assistant",
systemMessage: "You are a helpful AI assistant")
.RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.
.RegisterPrintMessage(); // pretty print the message to the console
await skAgent.SendAsync("Tell me more about gpt-4-o");
}
}
View on GitHub (pinned to 027ecf0a37)
Solutions
- Export OPENAI_API_KEY alongside BING_API_KEY in the same session and rerun.
- If your org uses Azure OpenAI, edit the sample to use AddAzureOpenAIChatCompletion with endpoint/deployment/key configuration instead.
- Add both keys to launchSettings.json when debugging from an IDE so they are always present.
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; this sample needs both BING_API_KEY and OPENAI_API_KEY."); Defensive patterns
Strategy: validation
Validate before calling
var missing = new[] { "BING_API_KEY", "OPENAI_API_KEY" }
.Where(k => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(k)))
.ToArray();
if (missing.Length > 0) { Console.Error.WriteLine($"Missing env vars: {string.Join(", ", missing)}"); return; } Prevention
- Check all required keys up front so the sample fails once with a complete list instead of key-by-key.
- If migrating to Azure OpenAI, swap both the connector and the key set in the same change.
When it happens
Trigger: Running the Bing-search sample with BING_API_KEY set but OPENAI_API_KEY unset; the exception fires at the openAIKey line just after the Bing connector is created.
Common situations: Developer configured only the search key; OpenAI key present in another shell/session; organization uses Azure OpenAI only, so no public-OpenAI key exists.
Related errors
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY 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/fc9d6be6ad505b7e.
Report an issue: GitHub.