{"record":{"id":"5d88b65f10a8d751","repo":"microsoft/autogen","slug":"please-set-azure-openai-api-key-environment-variab","errorCode":null,"errorMessage":"Please set AZURE_OPENAI_API_KEY environment variable.","messagePattern":"Please set AZURE_OPENAI_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.Basic.Sample/LLMConfiguration.cs","lineNumber":21,"sourceCode":"\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":3,"sourceCodeEnd":27,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/LLMConfiguration.cs#L3-L27","documentation":"Guard in LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo(): it requires AZURE_OPENAI_API_KEY because AzureOpenAIConfig(endpoint, deployName, azureOpenAIKey) needs the key to authenticate against your Azure OpenAI resource. This is the first of three Azure variables the helper validates (key, then endpoint, then deployment name), so seeing this message means the key was missing before the others were checked.","triggerScenarios":"Any sample calling LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo() (e.g. the Bing search agent in the sequential group-chat sample) with AZURE_OPENAI_API_KEY unset. OPENAI_API_KEY-based paths are unaffected.","commonSituations":"Users who configured public-OpenAI samples and then run an Azure sample with different variable names; key stored in Azure Key Vault but not exported; CI templates missing the Azure secrets; wrong variable spelling (AZURE_OPENAI_KEY vs AZURE_OPENAI_API_KEY).","solutions":["Set AZURE_OPENAI_API_KEY to a key from your Azure OpenAI resource (Azure Portal -> your resource -> Keys and Endpoint) and re-run.","Set the companion variables AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOY_NAME in the same environment — the helper checks them next.","Add all three to launchSettings.json / CI secret variables rather than hard-coding.","Verify the key is from an Azure OpenAI resource, not a generic OpenAI sk- key."],"exampleFix":"// before\nvar azureOpenAIKey = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_API_KEY\") ?? throw new Exception(\"Please set AZURE_OPENAI_API_KEY environment variable.\");\n\n// after\nvar azureOpenAIKey = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_API_KEY\")\n    ?? throw new InvalidOperationException(\"AZURE_OPENAI_API_KEY is not set. Copy a key from your Azure OpenAI resource (Keys and Endpoint) and export it.\");","handlingStrategy":"validation","validationCode":"string[] required = [\"AZURE_OPENAI_API_KEY\", \"AZURE_OPENAI_ENDPOINT\", \"AZURE_OPENAI_DEPLOY_NAME\"];\nvar missing = required.Where(v => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(v))).ToArray();\nif (missing.Length > 0)\n{\n    throw new InvalidOperationException($\"Missing Azure OpenAI configuration: {string.Join(\", \", missing)}\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var config = LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"AZURE_OPENAI\"))\n{\n    Console.Error.WriteLine($\"Incomplete Azure OpenAI config: {ex.Message}\");\n}","preventionTips":["Copy key, endpoint, and deployment name from the same Azure Portal page ('Keys and Endpoint') in one sitting.","Validate all three AZURE_OPENAI_* variables together at startup instead of discovering them one throw at a time.","Store Azure credentials in CI secret variables or user-secrets, never in source."],"tags":["configuration","environment-variables","azure","azure-openai","dotnet","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}