{"record":{"id":"994ee42c6b706c5f","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-994ee4","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/OpenAICodeSnippet.cs","lineNumber":35,"sourceCode":"    [Function]\n    public async Task<string> GetWeather(string location)\n    {\n        return \"The weather in \" + location + \" is sunny.\";\n    }\n}\n#endregion weather_function\npublic partial class OpenAICodeSnippet\n{\n    [Function]\n    public async Task<string> GetWeather(string location)\n    {\n        return \"The weather in \" + location + \" is sunny.\";\n    }\n\n    public async Task CreateOpenAIChatAgentAsync()\n    {\n        #region create_openai_chat_agent\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 openAIClient = new OpenAIClient(openAIKey);\n\n        // create an open ai chat agent\n        var openAIChatAgent = new OpenAIChatAgent(\n            chatClient: openAIClient.GetChatClient(modelId),\n            name: \"assistant\",\n            systemMessage: \"You are an assistant that help user to do some tasks.\");\n\n        // OpenAIChatAgent supports the following message types:\n        // - IMessage<ChatRequestMessage> where ChatRequestMessage is from Azure.AI.OpenAI\n\n        var helloMessage = new UserChatMessage(\"Hello\");\n\n        // Use MessageEnvelope.Create to create an IMessage<ChatRequestMessage>\n        var chatMessageContent = MessageEnvelope.Create(helloMessage);\n        var reply = await openAIChatAgent.SendAsync(chatMessageContent);\n","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/OpenAICodeSnippet.cs#L17-L53","documentation":"Guard in OpenAICodeSnippet.CreateOpenAIChatAgentAsync: OPENAI_API_KEY is null, so it throws before OpenAIClient(openAIKey) is constructed (an empty/invalid key would otherwise surface later as an OpenAI SDK auth error). This snippet builds an OpenAIChatAgent on gpt-4o-mini.","triggerScenarios":"Running the OpenAI chat-agent snippet (or its executing test) without OPENAI_API_KEY in the process environment.","commonSituations":"Fresh environment without the key exported; snippet executed by doc tests in CI lacking the secret; key set in a different shell session.","solutions":["Export OPENAI_API_KEY and rerun; verify with echo $OPENAI_API_KEY.","Add the variable to the CI pipeline secrets or launchSettings.json for IDE runs.","Load a .env file at startup if the project supports it.","Restart the process after setting the variable."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")))\n{\n    Console.Error.WriteLine(\"Please set OPENAI_API_KEY environment variable.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Export OPENAI_API_KEY before running OpenAI snippets/tests.","Propagate the variable to test hosts in CI.","Fail fast at startup with a list of all missing credentials."],"tags":["environment","api-key","dotnet","sample","openai"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}