{"record":{"id":"6fdef284c9fbb71c","repo":"microsoft/semantic-kernel","slug":"openai-api-key-is-not-set","errorCode":null,"errorMessage":"OPENAI_API_KEY is not set.","messagePattern":"OPENAI_API_KEY is not set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentFrameworkMigration/OpenAI/Step01_Basics/Program.cs","lineNumber":10,"sourceCode":"﻿// Copyright (c) Microsoft. All rights reserved.\n\nusing Microsoft.Agents.AI;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Agents;\nusing Microsoft.SemanticKernel.Connectors.OpenAI;\nusing OpenAI;\nusing OpenAI.Chat;\n\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set.\");\nvar model = System.Environment.GetEnvironmentVariable(\"OPENAI_MODEL\") ?? \"gpt-4o\";\nvar userInput = \"Tell me a joke about a pirate.\";\n\nConsole.WriteLine($\"User Input: {userInput}\");\n\nawait SKAgentAsync();\nawait SKAgent_As_AFAgentAsync();\nawait AFAgentAsync();\n\n// Example of Semantic Kernel Agent code\nasync Task SKAgentAsync()\n{\n    Console.WriteLine(\"\\n=== SK Agent ===\\n\");\n\n    var builder = Kernel.CreateBuilder().AddOpenAIChatClient(model, apiKey);\n\n    var agent = new ChatCompletionAgent()\n    {","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/AgentFrameworkMigration/OpenAI/Step01_Basics/Program.cs#L1-L28","documentation":"The OpenAI Basics migration sample throws InvalidOperationException when OPENAI_API_KEY is null. This sample targets the direct OpenAI API (not Azure), comparing SK's ChatCompletionAgent with the Agent Framework's AIAgent. The API key constructs an OpenAIClient used to build the chat client. No Azure credentials or endpoints are needed.","triggerScenarios":"Running OpenAI/Step01_Basics without OPENAI_API_KEY in the environment. The throw at line 10 fires before SKAgentAsync, SKAgent_As_AFAgentAsync, or AFAgentAsync execute.","commonSituations":"Running OpenAI samples with Azure env vars set instead (wrong variable); the API key expired or was rotated; using an organization-scoped key without the org header; OPENAI_API_KEY set in .env but not loaded by the console host.","solutions":["Set OPENAI_API_KEY to your OpenAI API key (starts with sk-)","Optionally set OPENAI_MODEL (defaults to gpt-4o)","On Windows: $env:OPENAI_API_KEY = 'sk-...'; on bash/macOS: export OPENAI_API_KEY='sk-...'","Verify the key is valid at platform.openai.com/api-keys"],"exampleFix":"// before\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set.\");\n\n// after\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n    throw new InvalidOperationException(\n        \"OPENAI_API_KEY is not set. Get a key at https://platform.openai.com/api-keys\");","handlingStrategy":"validation","validationCode":"var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n{\n    Console.Error.WriteLine(\"OPENAI_API_KEY is not set.\");\n    Console.Error.WriteLine(\"Get a key at https://platform.openai.com/api-keys\");\n    return;\n}","typeGuard":"static bool IsOpenAIKeyConfigured() =>\n    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\"));","tryCatchPattern":"try\n{\n    var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n        ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set.\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"OPENAI_API_KEY\"))\n{\n    Console.Error.WriteLine(ex.Message);\n    Console.Error.WriteLine(\"Get a key at https://platform.openai.com/api-keys\");\n    return;\n}","preventionTips":["Do not set Azure env vars (AZURE_OPENAI_ENDPOINT) for direct OpenAI samples — use OPENAI_API_KEY instead","Store the key in a .env file or .NET user secrets for local development","Verify the key is valid and has sufficient quota at platform.openai.com/api-keys","Set OPENAI_MODEL if you need a non-default model (defaults to gpt-4o)"],"tags":["configuration","csharp","openai","semantic-kernel","environment-variables","agent-framework-migration"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}