{"record":{"id":"2ae979cb785343a7","repo":"microsoft/autogen","slug":"please-set-environment-variable-openai-api-key","errorCode":null,"errorMessage":"Please set environment variable OPENAI_API_KEY","messagePattern":"Please set environment variable OPENAI_API_KEY","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs","lineNumber":14,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// Connect_To_OpenAI_o1_preview.cs\n\nusing AutoGen.Core;\nusing OpenAI;\n\nnamespace AutoGen.OpenAI.Sample;\n\npublic class Connect_To_OpenAI_o1_preview\n{\n    public static async Task RunAsync()\n    {\n        #region create_agent\n        var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new InvalidOperationException(\"Please set environment variable OPENAI_API_KEY\");\n        var openAIClient = new OpenAIClient(apiKey);\n\n        // until 2024/09/12\n        // openai o1-preview doesn't support systemMessage, temperature, maxTokens, streaming output\n        // so in order to use OpenAIChatAgent with o1-preview, you need to set those parameters to null\n        var agent = new OpenAIChatAgent(\n            chatClient: openAIClient.GetChatClient(\"o1-preview\"),\n            name: \"assistant\",\n            systemMessage: null,\n            temperature: null,\n            maxTokens: null,\n            seed: 0)\n            // by using RegisterMiddleware instead of RegisterStreamingMiddleware\n            // it turns an IStreamingAgent into an IAgent and disables streaming\n            .RegisterMiddleware(new OpenAIChatRequestMessageConnector())\n            .RegisterPrintMessage();\n        #endregion create_agent\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs#L1-L32","documentation":"Guard in the o1-preview connection sample: it reads OPENAI_API_KEY and throws InvalidOperationException when absent, because OpenAIClient(apiKey) must authenticate the o1-preview chat client. Sample-authored fail-fast: thrown before the OpenAIChatAgent (configured with null systemMessage/temperature/maxTokens for o1-preview limitations) is created.","triggerScenarios":"Calling Connect_To_OpenAI_o1_preview.RunAsync() with OPENAI_API_KEY unset or empty in the process. Nothing network-related runs before the throw.","commonSituations":"Running the OpenAI sample project without credentials; key expired or not granted o1-preview access (that fails later at the API, not here); IDE processes missing shell exports.","solutions":["Export OPENAI_API_KEY in the shell or IDE run profile that launches the sample.","Confirm your organization has been granted access to the o1-preview model, otherwise the next failure is an API-level model-access error.","Respect the sample's o1-preview constraints (no systemMessage, temperature, maxTokens, streaming) when adapting the code.","If o1-preview is unavailable to your account, swap the model name to one you can access."],"exampleFix":"// before\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new InvalidOperationException(\"Please set environment variable OPENAI_API_KEY\");\n\n// after\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n    ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set; export it before running this o1-preview sample.\");","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 the o1-preview sample.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await Connect_To_OpenAI_o1_preview.RunAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"OPENAI_API_KEY\"))\n{\n    Console.Error.WriteLine($\"Missing credential: {ex.Message}\");\n}","preventionTips":["Verify model access (o1-preview requires org-level grants) before running, or the API call fails after startup.","Respect o1-preview constraints: null systemMessage, temperature, maxTokens as the sample shows.","Centralize key checks at startup instead of relying on the sample's inline throw."],"tags":["configuration","environment-variables","openai","o1","dotnet","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}