microsoft/semantic-kernel · error · InvalidOperationException

No IOllamaApiClient implementations found in the service col

Error message

No IOllamaApiClient implementations found in the service collection.

What it means

Thrown as an InvalidOperationException inside the AddOllamaChatClient DI factory when none of the resolution attempts (keyed OllamaApiClient, keyed IOllamaApiClient, non-keyed OllamaApiClient, or IOllamaApiClient) yield a non-null OllamaApiClient instance. The extension tries multiple DI resolution paths and fails only when all return null.

Source

Thrown at dotnet/src/Connectors/Connectors.Ollama/Extensions/OllamaServiceCollectionExtensions.DependencyInjection.cs:114

    /// <returns>The updated service collection.</returns>
    public static IServiceCollection AddOllamaChatClient(
        this IServiceCollection services,
        OllamaApiClient? ollamaClient = null,
        string? serviceId = null)
    {
        Verify.NotNull(services);

        return services.AddKeyedSingleton<IChatClient>(serviceId, (serviceProvider, _) =>
        {
            var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
            ollamaClient ??= serviceProvider.GetKeyedService<OllamaApiClient>(serviceId);
            ollamaClient ??= serviceProvider.GetKeyedService<IOllamaApiClient>(serviceId) as OllamaApiClient;
            ollamaClient ??= serviceProvider.GetService<OllamaApiClient>();
            ollamaClient ??= serviceProvider.GetRequiredService<IOllamaApiClient>() as OllamaApiClient;

            if (ollamaClient is null)
            {
                throw new InvalidOperationException($"No {nameof(IOllamaApiClient)} implementations found in the service collection.");
            }

            var builder = ((IChatClient)ollamaClient).AsBuilder();
            if (loggerFactory is not null)
            {
                builder.UseLogging(loggerFactory);
            }

            return builder
                .UseKernelFunctionInvocation(loggerFactory)
                .Build(serviceProvider);
        });
    }

    #endregion

    #region Text Embeddings

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Register an OllamaApiClient before calling AddOllamaChatClient: services.AddOllamaApiClient(new Uri("http://localhost:11434")).
  2. Pass an existing OllamaApiClient instance directly to AddOllamaChatClient if using a custom configuration.
  3. Verify the service ID/key matches between registration and resolution if using keyed services.

Example fix

// before
builder.Services.AddOllamaChatClient("myChat");

// after
builder.Services.AddOllamaApiClient(new Uri("http://localhost:11434"));
builder.Services.AddOllamaChatClient("myChat");
Defensive patterns

Strategy: validation

Validate before calling

// Validate at startup after building the service provider
using var scope = app.Services.CreateScope();
var client = scope.ServiceProvider.GetService<OllamaApiClient>();
if (client is null)
    throw new InvalidOperationException("OllamaApiClient not registered.");

Prevention

When it happens

Trigger: Calling services.AddOllamaChatClient(...) without first registering an OllamaApiClient or IOllamaApiClient in the service collection, and without passing a pre-built ollamaClient argument.

Common situations: Forgot to call AddOllamaApiClient or register the client; using a keyed service with a different key than the one passed; the Ollama endpoint configuration is missing so the client was never constructed.

Related errors


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