microsoft/semantic-kernel · critical · InvalidOperationException

Please provide valid OpenAI configuration in appsettings.Dev

Error message

Please provide valid OpenAI configuration in appsettings.Development.json file.

What it means

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).

Source

Thrown at dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/DemoCommand.cs:307

                    options: new FunctionChoiceBehaviorOptions
                    {
                        AllowStrictSchemaAdherence = true
                    }
                )
                });
#pragma warning restore SKEXP0001
    }

    public static (Kernel, PromptExecutionSettings) InitializeOpenAiKernel(IConfiguration configuration, bool enableLogging)
    {
        // Extract configuration settings specific to OpenAI
        var openAIConfig = configuration.GetSection("OpenAI");
        var apiKey = openAIConfig["ApiKey"];
        var modelId = openAIConfig["ModelId"];

        if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(modelId))
        {
            throw new InvalidOperationException("Please provide valid OpenAI configuration in appsettings.Development.json file.");
        }

        var builder = Kernel.CreateBuilder();
        if (enableLogging)
        {
            builder.Services.AddLogging(loggingBuilder =>
                {
                    loggingBuilder.AddFilter(level => true);
                    loggingBuilder.AddProvider(new SemanticKernelLoggerProvider());
                });
        }

        return (builder.AddOpenAIChatCompletion(
            apiKey: apiKey,
            modelId: modelId).Build(),
#pragma warning disable SKEXP0001
            new OpenAIPromptExecutionSettings
            {

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Add OpenAI:ApiKey and OpenAI:ModelId to appsettings.Development.json.
  2. Use a current API key from platform.openai.com.
  3. Set a valid model id (e.g. gpt-4o-mini) that your org/key can access.
  4. Prefer user secrets over plaintext for the key: dotnet user-secrets set OpenAI:ApiKey <key>.

Example fix

// before
if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(modelId))
    throw new InvalidOperationException("Please provide valid OpenAI configuration...");

// after (appsettings.Development.json)
{
  "OpenAI": { "ApiKey": "sk-...", "ModelId": "gpt-4o-mini" }
}
Defensive patterns

Strategy: validation

Validate before calling

var apiKey = configuration["OpenAI:ApiKey"];
var modelId = configuration["OpenAI:ModelId"];
if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(modelId))
    throw new InvalidOperationException("Set OpenAI:ApiKey and OpenAI:ModelId.");

Type guard

static bool HasOpenAiConfig(IConfiguration c) =>
    !string.IsNullOrEmpty(c["OpenAI:ApiKey"]) && !string.IsNullOrEmpty(c["OpenAI:ModelId"]);

Prevention

When it happens

Trigger: configuration["OpenAI:ApiKey"] or ["OpenAI:ModelId"] is null or empty string.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/636b7a1bdfb82117. Report an issue: GitHub.