{"record":{"id":"d3620e41a246a06b","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-d3620e","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/GettingStart/FSM_Group_Chat.cs","lineNumber":151,"sourceCode":"            systemMessage: \"\"\"\n            You are a user who is filling an application form. Simply provide the information as requested and answer the questions, don't do anything else.\n            \n            here's some personal information about you:\n            - name: John Doe\n            - email: 1234567@gmail.com\n            - phone: 123-456-7890\n            - address: 1234 Main St, Redmond, WA 98052\n            - want to receive update? true\n            \"\"\")\n            .RegisterMessageConnector()\n            .RegisterPrintMessage();\n        #endregion Create_User_Agent\n        return chatAgent;\n    }\n\n    public static async Task RunAsync()\n    {\n        var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n        var model = \"gpt-4o-mini\";\n        var openaiClient = new OpenAIClient(apiKey);\n        var chatClient = openaiClient.GetChatClient(model);\n        var applicationAgent = await CreateSaveProgressAgent(chatClient);\n        var assistantAgent = await CreateAssistantAgent(chatClient);\n        var userAgent = await CreateUserAgent(chatClient);\n\n        #region Create_Graph\n        var userToApplicationTransition = Transition.Create(userAgent, applicationAgent);\n        var applicationToAssistantTransition = Transition.Create(applicationAgent, assistantAgent);\n        var assistantToUserTransition = Transition.Create(assistantAgent, userAgent);\n\n        var workflow = new Graph(\n            [\n                userToApplicationTransition,\n                applicationToAssistantTransition,\n                assistantToUserTransition,\n            ]);","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/GettingStart/FSM_Group_Chat.cs#L133-L169","documentation":"Startup guard in FSM_Group_Chat.RunAsync: the sample reads OPENAI_API_KEY and throws a plain Exception when it is missing, because OpenAIClient(apiKey) is required to build the chat clients for the application, assistant, and user agents. The throw is in sample code and happens before any agent factory or the group-chat graph is created.","triggerScenarios":"Calling FSM_Group_Chat.RunAsync() with OPENAI_API_KEY unset. Note this file contains an identical guard inside CreateSaveProgressAgent's caller chain; whichever executes first throws.","commonSituations":"Running the getting-started samples without configuring OpenAI credentials; IDE-launched processes missing shell-exported variables; CI without injected secrets.","solutions":["Export OPENAI_API_KEY in the launching shell (or set it via launchSettings.json / IDE run configuration) and re-run.","Confirm the variable name is exactly OPENAI_API_KEY and the value has no stray quotes/whitespace.","Ensure the key can access gpt-4o-mini, the model hard-coded in this sample.","If you meant to use Azure OpenAI, switch to the Azure samples (Connect_To_Azure_OpenAI) which read AZURE_* variables instead."],"exampleFix":"// before\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n\n// after\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n    ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set; configure it in your environment or IDE run profile.\");","handlingStrategy":"validation","validationCode":"var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n{\n    Console.Error.WriteLine(\"Set OPENAI_API_KEY before running FSM_Group_Chat.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await FSM_Group_Chat.RunAsync();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"OPENAI_API_KEY\"))\n{\n    Console.Error.WriteLine($\"Configuration error: {ex.Message}\");\n}","preventionTips":["Configure OPENAI_API_KEY once in the environment/IDE profile shared by all getting-started samples.","Validate credentials at process start before building agents.","Distinguish this OpenAI-based FSM sample from the Azure-based sequential sample when choosing which keys to set."],"tags":["configuration","environment-variables","openai","fsm","group-chat","dotnet","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}