{"record":{"id":"40fcb1ac1182cc10","repo":"microsoft/semantic-kernel","slug":"openai-api-key-is-not-set-40fcb1","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/OpenAIResponses/Step01_Basics/Program.cs","lineNumber":12,"sourceCode":"﻿// Copyright (c) Microsoft. All rights reserved.\n\nusing Microsoft.Agents.AI;\nusing Microsoft.Extensions.AI;\nusing Microsoft.SemanticKernel.Agents.OpenAI;\nusing OpenAI;\nusing OpenAI.Responses;\n\n#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\n#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\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\nasync Task SKAgentAsync()\n{\n    Console.WriteLine(\"\\n=== SK Agent ===\\n\");\n\n    var responseClient = new OpenAIClient(apiKey).GetResponsesClient();\n    OpenAIResponseAgent agent = new(responseClient)\n    {\n        Name = \"Joker\",\n        Instructions = \"You are good at telling jokes.\",","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/AgentFrameworkMigration/OpenAIResponses/Step01_Basics/Program.cs#L1-L30","documentation":"The OpenAI Responses Basics migration sample throws InvalidOperationException when OPENAI_API_KEY is missing. This sample targets the OpenAI Responses API (OpenAI.Responses namespace) rather than chat completions, comparing SK's OpenAIResponseAgent with the Agent Framework. The OPENAI001 and SKEXP0110 pragmas suppress preview-API warnings for the Responses API and experimental agent types.","triggerScenarios":"Running OpenAIResponses/Step01_Basics without OPENAI_API_KEY. The throw at line 12 fires before SKAgentAsync, SKAgent_As_AFAgentAsync, or AFAgentAsync execute.","commonSituations":"Confusing the Responses API sample with the chat-completion sample (same env var, different agent type and API surface); the API key does not have Responses API access; the Responses API is in preview and may not be available for all accounts.","solutions":["Set OPENAI_API_KEY to your OpenAI API key","Optionally set OPENAI_MODEL (defaults to gpt-4o)","Verify your OpenAI account has access to the Responses API (preview feature)","Ensure the model supports the Responses API"],"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. OpenAI Responses API samples require it.\");","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. Required for Responses API samples.\");\n    Console.Error.WriteLine(\"Verify your account has Responses API access (preview).\");\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    return;\n}","preventionTips":["Verify your OpenAI account has access to the Responses API (currently a preview feature)","Suppress OPENAI001 and SKEXP0110 warnings as the samples do, since Responses types are experimental","Distinguish the Responses API sample from the chat-completion sample — same key, different agent type","Ensure the model (default gpt-4o) supports the Responses API"],"tags":["configuration","csharp","openai","semantic-kernel","environment-variables","responses-api","agent-framework-migration"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}