microsoft/semantic-kernel · warning · InvalidOperationException
Configuration is not provided.
Error message
Configuration is not provided.
What it means
The null-coalescing throw checks whether ConfigurationBuilder().Build() returns null, then throws InvalidOperationException. In practice, Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() never returns null — it always returns a non-null IConfigurationRoot — making this throw effectively unreachable dead code. The check is defensive but cannot fire under normal library behavior.
Source
Thrown at dotnet/samples/Demos/TimePlugin/Program.cs:16
// Copyright (c) Microsoft. All rights reserved.
#pragma warning disable VSTHRD111 // Use ConfigureAwait(bool)
#pragma warning disable CA1050 // Declare types in namespaces
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
using System.ComponentModel;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build()
?? throw new InvalidOperationException("Configuration is not provided.");
ArgumentNullException.ThrowIfNull(config["OpenAI:ChatModelId"], "OpenAI:ChatModelId");
ArgumentNullException.ThrowIfNull(config["OpenAI:ApiKey"], "OpenAI:ApiKey");
var kernelBuilder = Kernel.CreateBuilder().AddOpenAIChatCompletion(
modelId: config["OpenAI:ChatModelId"]!,
apiKey: config["OpenAI:ApiKey"]!);
kernelBuilder.Plugins.AddFromType<TimeInformationPlugin>();
var kernel = kernelBuilder.Build();
// Get chat completion service
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Enable auto function calling
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()View on GitHub (pinned to c028a0c7dc)
Solutions
- Recognize this line is effectively dead code; the real validation happens on the next two lines via ArgumentNullException.ThrowIfNull.
- Ensure OpenAI:ChatModelId and OpenAI:ApiKey are set in user secrets or environment variables.
- Remove the misleading null-coalescing throw or replace it with a meaningful check on specific keys.
- If you encounter this exact message in a stack trace, suspect a custom IConfigurationBuilder or a framework version anomaly — otherwise look at the ArgumentNullException lines below it.
Example fix
// before — unreachable check on Build()
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build()
?? throw new InvalidOperationException("Configuration is not provided.");
// after — Build() never returns null; validate the keys that matter
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build();
ArgumentNullException.ThrowIfNull(config["OpenAI:ChatModelId"], "OpenAI:ChatModelId");
ArgumentNullException.ThrowIfNull(config["OpenAI:ApiKey"], "OpenAI:ApiKey"); Defensive patterns
Strategy: validation
Validate before calling
// Skip the dead Build() null-check; validate the keys that matter var modelId = config["OpenAI:ChatModelId"]; var apiKey = config["OpenAI:ApiKey"]; if (string.IsNullOrWhiteSpace(modelId)) throw new ArgumentNullException(nameof(modelId), "Set OpenAI:ChatModelId in user secrets or env."); if (string.IsNullOrWhiteSpace(apiKey)) throw new ArgumentNullException(nameof(apiKey), "Set OpenAI:ApiKey in user secrets or env.");
Prevention
- Don't rely on Build() returning null — it never does in Microsoft.Extensions.Configuration.
- Validate specific required keys immediately after Build().
- Use user secrets consistently and verify the UserSecretsId in the .csproj.
When it happens
Trigger: This throw is unreachable in practice: Build() returns a non-null IConfigurationRoot even when no providers are configured. It would only fire if a custom IConfigurationBuilder implementation overrode Build() to return null, which is not the case here.
Common situations: Developers see this message and believe configuration is missing, but the real problem is that config keys (OpenAI:ChatModelId, OpenAI:ApiKey) are null — the subsequent ArgumentNullException.ThrowIfNull calls are what actually fire for missing keys, not this line.
Related errors
- OpenAI/Azure OpenAI configuration was not found.
- Could not find configuration section {caller}
- Configuration section '{section}' not found
- Configuration section '{section}' not found
- The DeepSeek API key is required.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/11f93389b5d5519f.
Report an issue: GitHub.