{"record":{"id":"636b7a1bdfb82117","repo":"microsoft/semantic-kernel","slug":"please-provide-valid-openai-configuration-in-appse","errorCode":null,"errorMessage":"Please provide valid OpenAI configuration in appsettings.Development.json file.","messagePattern":"Please provide valid OpenAI configuration in appsettings\\.Development\\.json file\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/DemoCommand.cs","lineNumber":307,"sourceCode":"                    options: new FunctionChoiceBehaviorOptions\n                    {\n                        AllowStrictSchemaAdherence = true\n                    }\n                )\n                });\n#pragma warning restore SKEXP0001\n    }\n\n    public static (Kernel, PromptExecutionSettings) InitializeOpenAiKernel(IConfiguration configuration, bool enableLogging)\n    {\n        // Extract configuration settings specific to OpenAI\n        var openAIConfig = configuration.GetSection(\"OpenAI\");\n        var apiKey = openAIConfig[\"ApiKey\"];\n        var modelId = openAIConfig[\"ModelId\"];\n\n        if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(modelId))\n        {\n            throw new InvalidOperationException(\"Please provide valid OpenAI configuration in appsettings.Development.json file.\");\n        }\n\n        var builder = Kernel.CreateBuilder();\n        if (enableLogging)\n        {\n            builder.Services.AddLogging(loggingBuilder =>\n                {\n                    loggingBuilder.AddFilter(level => true);\n                    loggingBuilder.AddProvider(new SemanticKernelLoggerProvider());\n                });\n        }\n\n        return (builder.AddOpenAIChatCompletion(\n            apiKey: apiKey,\n            modelId: modelId).Build(),\n#pragma warning disable SKEXP0001\n            new OpenAIPromptExecutionSettings\n            {","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/DemoCommand.cs#L289-L325","documentation":"InitializeOpenAiKernel reads OpenAI:ApiKey and OpenAI:ModelId and throws InvalidOperationException if either is null/empty. This is the non-Azure OpenAI path; it only needs an API key and a model id (no deployment/endpoint).","triggerScenarios":"configuration[\"OpenAI:ApiKey\"] or [\"OpenAI:ModelId\"] is null or empty string.","commonSituations":"OpenAI section missing from appsettings.Development.json, key expired/revoked (though that fails later at call time, not here), or user copied Azure settings into the OpenAI section.","solutions":["Add OpenAI:ApiKey and OpenAI:ModelId to appsettings.Development.json.","Use a current API key from platform.openai.com.","Set a valid model id (e.g. gpt-4o-mini) that your org/key can access.","Prefer user secrets over plaintext for the key: dotnet user-secrets set OpenAI:ApiKey <key>."],"exampleFix":"// before\nif (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(modelId))\n    throw new InvalidOperationException(\"Please provide valid OpenAI configuration...\");\n\n// after (appsettings.Development.json)\n{\n  \"OpenAI\": { \"ApiKey\": \"sk-...\", \"ModelId\": \"gpt-4o-mini\" }\n}","handlingStrategy":"validation","validationCode":"var apiKey = configuration[\"OpenAI:ApiKey\"];\nvar modelId = configuration[\"OpenAI:ModelId\"];\nif (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(modelId))\n    throw new InvalidOperationException(\"Set OpenAI:ApiKey and OpenAI:ModelId.\");","typeGuard":"static bool HasOpenAiConfig(IConfiguration c) =>\n    !string.IsNullOrEmpty(c[\"OpenAI:ApiKey\"]) && !string.IsNullOrEmpty(c[\"OpenAI:ModelId\"]);","tryCatchPattern":null,"preventionTips":["Store the key in user secrets, not plaintext.","Confirm the model id is accessible by your org/key.","Don't mix Azure settings into the OpenAI section."],"tags":["configuration","openai","kernel","samples"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}