{"record":{"id":"3908ad22fbe6b1bb","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-3908ad","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/Create_Semantic_Kernel_Chat_Agent.cs","lineNumber":16,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// Create_Semantic_Kernel_Chat_Agent.cs\n\n#region Using\nusing AutoGen.Core;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Agents;\n#endregion Using\nnamespace AutoGen.SemanticKernel.Sample;\n\npublic class Create_Semantic_Kernel_Chat_Agent\n{\n    public static async Task RunAsync()\n    {\n        #region Create_Kernel\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 kernel = Kernel.CreateBuilder()\n            .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey)\n            .Build();\n        #endregion Create_Kernel\n\n        #region Create_ChatCompletionAgent\n        // The built-in ChatCompletionAgent from semantic kernel.\n        var chatAgent = new ChatCompletionAgent()\n        {\n            Kernel = kernel,\n            Name = \"assistant\",\n            Description = \"You are a helpful AI assistant\",\n        };\n        #endregion Create_ChatCompletionAgent\n\n        #region Create_SemanticKernelChatCompletionAgent\n        var messageConnector = new SemanticKernelChatMessageContentConnector();","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Chat_Agent.cs#L1-L34","documentation":"Thrown by the 'Create_Semantic_Kernel_Chat_Agent' sample when OPENAI_API_KEY is missing. This sample builds a Microsoft.SemanticKernel Kernel and wraps Semantic Kernel's built-in ChatCompletionAgent, and the key is required by AddOpenAIChatCompletion before any agent runs. The guard makes the missing-configuration failure explicit at startup.","triggerScenarios":"Running the AutoGen.SemanticKernel sample's Create_Semantic_Kernel_Chat_Agent.RunAsync() in a process where OPENAI_API_KEY is null.","commonSituations":"Fresh repo clone; env var set at user scope but the sample launched from VS Code/WSL/Docker where it is not inherited; CI without secrets; typos in the variable name (e.g. OPENAI_APIKEY).","solutions":["Set OPENAI_API_KEY in the launching shell: export OPENAI_API_KEY=sk-... or $env:OPENAI_API_KEY='sk-...'.","Persist the variable (setx on Windows, shell rc file on Unix) and restart the IDE/terminal.","Put it in Properties/launchSettings.json environmentVariables for IDE debugging.","Verify with: echo $OPENAI_API_KEY (should print sk-...) before rerunning."],"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; export it or configure it in launchSettings.json.\");","handlingStrategy":"validation","validationCode":"string[] required = { \"OPENAI_API_KEY\" };\nvar missing = required.Where(v => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(v))).ToArray();\nif (missing.Length > 0) { Console.Error.WriteLine($\"Missing env vars: {string.Join(\", \", missing)}\"); return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate configuration once at process start and fail with a list of all missing keys.","Keep env vars in launchSettings.json version-controlled with placeholder values plus a README mapping to real secrets."],"tags":["semantic-kernel","openai","chat-completion-agent","configuration","environment-variables","sample"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}