{"record":{"id":"fc9d6be6ad505b7e","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-fc9d6b","errorCode":null,"errorMessage":"Please set OPENAI_API_KEY environment variable.","messagePattern":"Please set OPENAI_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs","lineNumber":20,"sourceCode":"// Use_Bing_Search_With_Semantic_Kernel_Agent.cs\n\nusing AutoGen.Core;\nusing AutoGen.SemanticKernel.Extension;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Plugins.Web;\nusing Microsoft.SemanticKernel.Plugins.Web.Bing;\n\nnamespace AutoGen.SemanticKernel.Sample;\n\npublic class Use_Bing_Search_With_Semantic_Kernel_Agent\n{\n    public static async Task RunAsync()\n    {\n        var bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\") ?? throw new Exception(\"BING_API_KEY environment variable is not set\");\n        var bingSearch = new BingConnector(bingApiKey);\n        var webSearchPlugin = new WebSearchEnginePlugin(bingSearch);\n\n        var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n        var modelId = \"gpt-3.5-turbo\";\n        var kernelBuilder = Kernel.CreateBuilder()\n            .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);\n        kernelBuilder.Plugins.AddFromObject(webSearchPlugin);\n\n        var kernel = kernelBuilder.Build();\n\n        var skAgent = new SemanticKernelAgent(\n            kernel: kernel,\n            name: \"assistant\",\n            systemMessage: \"You are a helpful AI assistant\")\n            .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.\n            .RegisterPrintMessage(); // pretty print the message to the console\n\n        await skAgent.SendAsync(\"Tell me more about gpt-4-o\");\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs#L2-L38","documentation":"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.","triggerScenarios":"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.","commonSituations":"Developer configured only the search key; OpenAI key present in another shell/session; organization uses Azure OpenAI only, so no public-OpenAI key exists.","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."],"exampleFix":"// before\nvar openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n\n// after\nvar openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n    ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set; this sample needs both BING_API_KEY and OPENAI_API_KEY.\");","handlingStrategy":"validation","validationCode":"var missing = new[] { \"BING_API_KEY\", \"OPENAI_API_KEY\" }\n    .Where(k => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(k)))\n    .ToArray();\nif (missing.Length > 0) { Console.Error.WriteLine($\"Missing env vars: {string.Join(\", \", missing)}\"); return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["openai","semantic-kernel","bing","configuration","environment-variables","sample"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}