{"record":{"id":"431002a656142778","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-431002","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/LLMConfiguration.cs","lineNumber":13,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// LLMConfiguration.cs\n\nusing OpenAI;\nusing OpenAI.Chat;\n\nnamespace AutoGen.Basic.Sample;\n\ninternal static class LLMConfiguration\n{\n    public static ChatClient GetOpenAIGPT4o_mini()\n    {\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\n        return new OpenAIClient(openAIKey).GetChatClient(modelId);\n    }\n\n    public static AzureOpenAIConfig GetAzureOpenAIGPT3_5_Turbo(string? deployName = null)\n    {\n        var azureOpenAIKey = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_API_KEY\") ?? throw new Exception(\"Please set AZURE_OPENAI_API_KEY environment variable.\");\n        var endpoint = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\") ?? throw new Exception(\"Please set AZURE_OPENAI_ENDPOINT environment variable.\");\n        deployName = deployName ?? Environment.GetEnvironmentVariable(\"AZURE_OPENAI_DEPLOY_NAME\") ?? throw new Exception(\"Please set AZURE_OPENAI_DEPLOY_NAME environment variable.\");\n        return new AzureOpenAIConfig(endpoint, deployName, azureOpenAIKey);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/LLMConfiguration.cs#L1-L27","documentation":"Guard inside the shared sample helper LLMConfiguration.GetOpenAIGPT4o_mini(): it reads OPENAI_API_KEY and throws when absent, because OpenAIClient(openAIKey).GetChatClient(\"gpt-4o-mini\") cannot be built without a key. Since many samples funnel through this helper, this single line is the source of OPENAI_API_KEY failures across several example files.","triggerScenarios":"Any sample calling LLMConfiguration.GetOpenAIGPT4o_mini() (e.g. Example17_ReActAgent) with OPENAI_API_KEY unset. The throw happens before the ChatClient is returned.","commonSituations":"Running the full sample suite with partial credentials; IDE/CI processes missing exported variables; users who set only the AZURE_* variables and then call an OpenAI-based helper.","solutions":["Set OPENAI_API_KEY in the environment that launches the samples and re-run.","Prefer configuring all keys the helper file reads (OPENAI_API_KEY, AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOY_NAME) so any sample path works.","Add the keys to launchSettings.json or CI secret variables, not to source.","If you only have Azure credentials, call GetAzureOpenAIGPT3_5_Turbo()-based samples instead of the OpenAI helper."],"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. LLMConfiguration requires it for the public OpenAI endpoint.\");","handlingStrategy":"validation","validationCode":"var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(openAIKey))\n{\n    throw new InvalidOperationException(\"OPENAI_API_KEY is not set. Export it before calling GetOpenAIGPT4o_mini().\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var client = LLMConfiguration.GetOpenAIGPT4o_mini();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"OPENAI_API_KEY\"))\n{\n    Console.Error.WriteLine($\"Missing OpenAI credential: {ex.Message}\");\n}","preventionTips":["Treat LLMConfiguration.cs as the single source of required variables; set every name it reads up front.","Centralize env validation at startup and fail once with a consolidated message listing all missing keys.","Keep OpenAI vs Azure credential sets distinct and named exactly as the helper expects."],"tags":["configuration","environment-variables","openai","shared-helper","dotnet","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}