{"record":{"id":"2b35df7b4a035da3","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable-2b35df","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/CreateAnAgent.cs","lineNumber":17,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// CreateAnAgent.cs\n\nusing AutoGen;\nusing AutoGen.Core;\nusing AutoGen.OpenAI;\nusing AutoGen.OpenAI.Extension;\nusing FluentAssertions;\nusing OpenAI;\n\npublic partial class AssistantCodeSnippet\n{\n    public void CodeSnippet1()\n    {\n        #region code_snippet_1\n        // get OpenAI Key and create config\n        var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n        var llmConfig = new OpenAIConfig(openAIKey, \"gpt-3.5-turbo\");\n\n        // create assistant agent\n        var assistantAgent = new AssistantAgent(\n            name: \"assistant\",\n            systemMessage: \"You are an assistant that help user to do some tasks.\",\n            llmConfig: new ConversableAgentConfig\n            {\n                Temperature = 0,\n                ConfigList = new[] { llmConfig },\n            });\n        #endregion code_snippet_1\n\n    }\n\n    public void CodeSnippet2()\n    {\n        #region code_snippet_2","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/CreateAnAgent.cs#L1-L35","documentation":"Guard in the AssistantAgent code snippet: OPENAI_API_KEY is null, so it throws before creating OpenAIConfig. It exists so the documented snippet (used in docs via #region extraction) fails with an actionable message rather than an opaque OpenAI auth 401 later.","triggerScenarios":"Invoking AssistantCodeSnippet.CodeSnippet1 (often via the test suite or a doc build that executes snippets) without OPENAI_API_KEY.","commonSituations":"Running the AutoGen.Basic.Sample tests on a machine without the key; doc-generation pipelines that execute snippets; new contributors running samples before configuring secrets.","solutions":["Set OPENAI_API_KEY in the shell/CI environment before running samples or tests.","Skip live-key snippet tests locally if the repo marks them as requiring credentials.","Use a .env loader (e.g. DotNetEnv.Env.Load()) at sample startup.","For docs builds, ensure the pipeline secret is mapped to the environment variable name."],"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":["Export required keys before running the sample test suite.","Mark snippet tests with a trait (e.g. [Fact(Skip=\"requires OPENAI_API_KEY\")]) when the var is absent.","Use a shared env-validation helper in all snippets."],"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"}