{"record":{"id":"e91f6f70aef59b28","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-e91f6f","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/GetStartCodeSnippet.cs","lineNumber":17,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// GetStartCodeSnippet.cs\n\n#region snippet_GetStartCodeSnippet\nusing AutoGen;\nusing AutoGen.Core;\nusing AutoGen.OpenAI;\nusing AutoGen.OpenAI.Extension;\nusing OpenAI;\n#endregion snippet_GetStartCodeSnippet\n\npublic class GetStartCodeSnippet\n{\n    public async Task CodeSnippet1()\n    {\n        #region code_snippet_1\n        var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n        var openAIClient = new OpenAIClient(openAIKey);\n        var model = \"gpt-4o-mini\";\n\n        var assistantAgent = new OpenAIChatAgent(\n            name: \"assistant\",\n            systemMessage: \"You are an assistant that help user to do some tasks.\",\n            chatClient: openAIClient.GetChatClient(model))\n            .RegisterMessageConnector()\n            .RegisterPrintMessage(); // register a hook to print message nicely to console\n\n        // set human input mode to ALWAYS so that user always provide input\n        var userProxyAgent = new UserProxyAgent(\n            name: \"user\",\n            humanInputMode: HumanInputMode.ALWAYS)\n            .RegisterPrintMessage();\n\n        // start the conversation\n        await userProxyAgent.InitiateChatAsync(","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/GetStartCodeSnippet.cs#L1-L35","documentation":"Guard in GetStartCodeSnippet.CodeSnippet1 ('get started' example): OPENAI_API_KEY is null, so it throws before OpenAIClient is constructed with an empty key. The snippet then builds an OpenAIChatAgent on gpt-4o-mini, so a valid key is mandatory.","triggerScenarios":"Running the get-started sample without OPENAI_API_KEY; running the snippet-executing tests in AutoGen.Basic.Sample on a machine without credentials.","commonSituations":"First-run experience: developer clones the repo, runs samples, hasn't exported the key yet; terminal/IDE environment mismatch; key set after the process started.","solutions":["export OPENAI_API_KEY=sk-... (or set it in launchSettings.json) and rerun dotnet run.","Restart the IDE so newly set variables are visible to the debug process.","For tests, set the variable in the test runner environment or filter out live-API tests.","Ensure no trailing whitespace/quotes when exporting the key."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(openAIKey))\n{\n    Console.Error.WriteLine(\"Please set OPENAI_API_KEY environment variable.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set the key before the first run of any get-started sample.","Add env vars to launchSettings.json so IDE runs inherit them.","Restart terminals after exporting keys."],"tags":["environment","api-key","dotnet","sample","openai","getting-started"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}