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 AddOllamaTextGeneration DI factory (legacy ITextGenerationService path) when no OllamaApiClient can be resolved. All four DI resolution attempts (keyed OllamaApiClient, keyed IOllamaApiClient, non-keyed OllamaApiClient, IOllamaApiClient) returned null.

Source

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

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

        return services.AddKeyedSingleton<ITextGenerationService>(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.");
            }

            return new OllamaTextGenerationService(
                ollamaClient: ollamaClient,
                loggerFactory: loggerFactory);
        });
    }

    #endregion

    #region Chat Completion

    /// <summary>
    /// Add Ollama Chat Completion and Text Generation services to the specified service collection.
    /// </summary>
    /// <param name="services">The target service collection.</param>
    /// <param name="modelId">The model for text generation.</param>
    /// <param name="endpoint">The endpoint to Ollama hosted service.</param>

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Register the OllamaApiClient before calling AddOllamaTextGeneration.
  2. Pass a client instance directly to the method if you manage lifecycle externally.
  3. Switch to the newer AddOllamaChatClient API if using the current Ollama library.

Example fix

// before
builder.Services.AddOllamaTextGeneration(modelId: "llama3");

// after
builder.Services.AddOllamaApiClient(new Uri("http://localhost:11434"));
builder.Services.AddOllamaTextGeneration(modelId: "llama3");
Defensive patterns

Strategy: validation

Validate before calling

using var scope = app.Services.CreateScope();
if (scope.ServiceProvider.GetService<OllamaApiClient>() is null)
    throw new InvalidOperationException("OllamaApiClient not registered.");

Prevention

When it happens

Trigger: Calling services.AddOllamaTextGeneration(...) without registering an OllamaApiClient or IOllamaApiClient, and without passing a client instance argument.

Common situations: Using the older ITextGenerationService API path without registering the client; migration from a custom registration that was removed; Ollama not running so client factory returned null.

Related errors


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