{"record":{"id":"170aa6f0ac646b0d","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-170aa6","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.Basic.Sample/CodeSnippet/SemanticKernelCodeSnippet.cs","lineNumber":22,"sourceCode":"using AutoGen.Core;\nusing AutoGen.SemanticKernel;\nusing AutoGen.SemanticKernel.Extension;\nusing FluentAssertions;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.ChatCompletion;\n\nnamespace AutoGen.Basic.Sample.CodeSnippet;\n\npublic class SemanticKernelCodeSnippet\n{\n    public async Task<string> GetWeather(string location)\n    {\n        return \"The weather in \" + location + \" is sunny.\";\n    }\n    public async Task CreateSemanticKernelAgentAsync()\n    {\n        #region create_semantic_kernel_agent\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 builder = Kernel.CreateBuilder()\n           .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);\n        var kernel = builder.Build();\n\n        // create a semantic kernel agent\n        var semanticKernelAgent = new SemanticKernelAgent(\n            kernel: kernel,\n            name: \"assistant\",\n            systemMessage: \"You are an assistant that help user to do some tasks.\");\n\n        // SemanticKernelAgent supports the following message types:\n        // - IMessage<ChatMessageContent> where ChatMessageContent is from Azure.AI.OpenAI\n\n        var helloMessage = new ChatMessageContent(AuthorRole.User, \"Hello\");\n\n        // Use MessageEnvelope.Create to create an IMessage<ChatRequestMessage>\n        var chatMessageContent = MessageEnvelope.Create(helloMessage);","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/SemanticKernelCodeSnippet.cs#L4-L40","documentation":"Guard in SemanticKernelCodeSnippet.CreateSemanticKernelAgentAsync: OPENAI_API_KEY is null, so it throws before Kernel.CreateBuilder().AddOpenAIChatCompletion(...) is given an empty key. The Semantic Kernel path still consumes an OpenAI key, which surprises developers who expected a separate SK credential.","triggerScenarios":"Running the Semantic Kernel snippet without OPENAI_API_KEY, even though the agent is a SemanticKernelAgent — the underlying connector is OpenAI chat completion.","commonSituations":"Developer assumed Semantic Kernel uses its own auth or Azure key and didn't set the OpenAI one; Azure deployments should use AddAzureOpenAIChatCompletion instead, which needs different env vars.","solutions":["Export OPENAI_API_KEY before running the snippet.","If you intend to use Azure OpenAI with SK, switch to builder.AddAzureOpenAIChatCompletion(deployment, endpoint, azureKey) and set AZURE_OPENAI_API_KEY/AZURE_OPENAI_ENDPOINT instead.","Set the variable in CI secrets/launchSettings for test and IDE runs.","Restart the process after changing the environment."],"exampleFix":"// before\nbuilder.AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);\n\n// after (Azure variant, when only Azure credentials exist)\nbuilder.AddAzureOpenAIChatCompletion(\n    deploymentName: deploymentName,\n    endpoint: endpoint,   // from AZURE_OPENAI_ENDPOINT\n    apiKey: azureKey);    // from AZURE_OPENAI_API_KEY","handlingStrategy":"validation","validationCode":"var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(openAIKey))\n{\n    Console.Error.WriteLine(\"Semantic Kernel snippet uses OpenAI: set OPENAI_API_KEY (or switch to AddAzureOpenAIChatCompletion).\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember AddOpenAIChatCompletion needs OPENAI_API_KEY even under Semantic Kernel.","For Azure, use AddAzureOpenAIChatCompletion with AZURE_OPENAI_* variables.","Centralize credential checks before building the Kernel."],"tags":["environment","api-key","dotnet","sample","semantic-kernel","openai"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}