{"record":{"id":"26fb768c5e68d8f0","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-26fb76","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_Kernel_Functions_With_Other_Agent.cs","lineNumber":19,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// Use_Kernel_Functions_With_Other_Agent.cs\n\n#region Using\nusing AutoGen.Core;\nusing AutoGen.OpenAI;\nusing AutoGen.OpenAI.Extension;\nusing Microsoft.SemanticKernel;\nusing OpenAI;\n#endregion Using\n\nnamespace AutoGen.SemanticKernel.Sample;\n\npublic class Use_Kernel_Functions_With_Other_Agent\n{\n    public static async Task RunAsync()\n    {\n        #region Create_plugin\n        var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n        var modelId = \"gpt-4o-mini\";\n        var kernelBuilder = Kernel.CreateBuilder();\n        var kernel = kernelBuilder.Build();\n        var getWeatherFunction = KernelFunctionFactory.CreateFromMethod(\n                       method: (string location) => $\"The weather in {location} is 75 degrees Fahrenheit.\",\n                       functionName: \"GetWeather\",\n                       description: \"Get the weather for a location.\");\n        var plugin = kernel.CreatePluginFromFunctions(\"my_plugin\", [getWeatherFunction]);\n        #endregion Create_plugin\n\n        #region Use_plugin\n        // Create a middleware to handle the plugin functions\n        var kernelPluginMiddleware = new KernelPluginMiddleware(kernel, plugin);\n\n        var openAIClient = new OpenAIClient(openAIKey);\n        var openAIAgent = new OpenAIChatAgent(\n            chatClient: openAIClient.GetChatClient(modelId),\n            name: \"assistant\")","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Kernel_Functions_With_Other_Agent.cs#L1-L37","documentation":"Thrown by the 'Use_Kernel_Functions_With_Other_Agent' sample when OPENAI_API_KEY is absent. Although the sample's kernel only hosts a local GetWeather plugin, it later drives an OpenAI agent (via AutoGen.OpenAI), so the OpenAI key is still required. The guard fires at the top of RunAsync before anything is constructed.","triggerScenarios":"Running Use_Kernel_Functions_With_Other_Agent.RunAsync() without OPENAI_API_KEY; the key is consumed when the OpenAIChatAgent/kernel-plugin middleware is created further down.","commonSituations":"Assuming the sample is offline because it shows a local weather function (it is not); env var set in a different terminal; CI run without secrets.","solutions":["Set OPENAI_API_KEY for the launching process (export / $env: / launchSettings.json) and rerun.","Verify the variable name matches exactly (OPENAI_API_KEY, underscores, all caps).","If you truly want an offline run, replace the OpenAI agent with a local mock agent, but the sample as shipped needs the key."],"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 add it to launchSettings.json.\");","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")))\n{\n    Console.Error.WriteLine(\"OPENAI_API_KEY missing; this sample still needs the OpenAI key for the LLM agent.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not assume samples with local plugins are offline — read the env-var guards at the top of RunAsync.","Standardize on user-secrets or launchSettings.json for all sample keys."],"tags":["openai","semantic-kernel","kernel-functions","configuration","environment-variables","sample"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}