microsoft/semantic-kernel · critical · InvalidOperationException

Please provide valid Ollama configuration in appsettings.Dev

Error message

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

What it means

InitializeKernelForOllama reads Ollama:ChatModelId and Ollama:Endpoint from configuration and throws InvalidOperationException if either is null/empty before building the Ollama-backed kernel. Endpoint is required so the connector can reach the local/remote Ollama instance.

Source

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

        }
    }

    private async Task ExecuteGoalAsync(Kernel kernel, PromptExecutionSettings promptExecutionSettings)
    {
        var goal = AnsiConsole.Ask<string>("Enter your goal:");
        var result = await kernel.InvokePromptAsync(goal, new KernelArguments(promptExecutionSettings)).ConfigureAwait(false);
        var panel = new Panel($"[bold]Result[/]{Environment.NewLine}{Environment.NewLine}[green italic]{Markup.Escape(result.ToString())}[/]");
        AnsiConsole.Write(panel);
    }

    private static (Kernel, PromptExecutionSettings) InitializeKernelForOllama(IConfiguration configuration, bool enableLogging)
    {
        var engineConfig = configuration.GetSection("Ollama");
        var chatModelId = engineConfig["ChatModelId"];
        var endpoint = engineConfig["Endpoint"];
        if (string.IsNullOrEmpty(chatModelId) || string.IsNullOrEmpty(endpoint))
        {
            throw new InvalidOperationException("Please provide valid Ollama configuration in appsettings.Development.json file.");
        }

        var builder = Kernel.CreateBuilder();
        if (enableLogging)
        {
            builder.Services.AddLogging(loggingBuilder =>
                {
                    loggingBuilder.AddFilter(level => true);
                    loggingBuilder.AddProvider(new SemanticKernelLoggerProvider());
                });
        }
#pragma warning disable SKEXP0001
        return (builder.AddOllamaChatCompletion(
                chatModelId,
                new Uri(endpoint)).Build(),
                new OllamaPromptExecutionSettings
                {
                    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Add an Ollama section: { "Ollama": { "ChatModelId": "llama3.2", "Endpoint": "http://localhost:11434" } }.
  2. Ensure Ollama is installed and the named model is pulled (`ollama pull <model>`).
  3. Verify the endpoint URL is reachable from the host running the sample.
  4. Set via environment variables Ollama__ChatModelId / Ollama__Endpoint if you prefer not to edit JSON.

Example fix

// before
if (string.IsNullOrEmpty(chatModelId) || string.IsNullOrEmpty(endpoint))
    throw new InvalidOperationException("Please provide valid Ollama configuration...");

// after (appsettings.Development.json)
{
  "Ollama": { "ChatModelId": "llama3.2", "Endpoint": "http://localhost:11434" }
}
Defensive patterns

Strategy: validation

Validate before calling

var chatModelId = configuration["Ollama:ChatModelId"];
var endpoint = configuration["Ollama:Endpoint"];
if (string.IsNullOrWhiteSpace(chatModelId) || string.IsNullOrWhiteSpace(endpoint))
    throw new InvalidOperationException("Set Ollama:ChatModelId and Ollama:Endpoint (e.g. http://localhost:11434).");

Type guard

static bool HasOllamaConfig(IConfiguration c) =>
    !string.IsNullOrEmpty(c["Ollama:ChatModelId"]) && !string.IsNullOrEmpty(c["Ollama:Endpoint"]);

Prevention

When it happens

Trigger: configuration.GetSection("Ollama")["ChatModelId"] or ["Endpoint"] is null or empty string.

Common situations: Ollama section missing from appsettings.Development.json, Ollama not running locally so the user skipped config, or endpoint/model keys renamed.

Related errors


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