microsoft/semantic-kernel · error · KernelException

An error occurred while initializing the {nameof(IEmbeddingG

Error message

An error occurred while initializing the {nameof(IEmbeddingGenerator)}: {ex.Message}

What it means

Thrown by the AddBedrockTextEmbeddingGenerator DI extension when the MEAI embedding-generator Build pipeline (UseLogging, UseOpenTelemetry, Build) throws. Wrapped as a KernelException whose Message embeds the inner ex.Message. Mirrors error 307 but for the IEmbeddingGenerator registration.

Source

Thrown at dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.DependencyInjection.cs:131

                {
                    // Cast to AmazonServiceClient and subscribe to the event
                    ((AmazonServiceClient)runtime).BeforeRequestEvent += BedrockClientUtilities.BedrockServiceClientRequestHandler;
                }

                var builder = runtime.AsIEmbeddingGenerator(modelId).AsBuilder();

                if (loggerFactory is not null)
                {
                    builder.UseLogging(loggerFactory);
                }

                builder.UseOpenTelemetry(loggerFactory, openTelemetrySourceName, openTelemetryConfig);

                return builder.Build(serviceProvider);
            }
            catch (Exception ex)
            {
                throw new KernelException($"An error occurred while initializing the {nameof(IEmbeddingGenerator)}: {ex.Message}", ex);
            }
        });

        return services;
    }
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect ex.InnerException of the KernelException for the true cause.
  2. Use a valid embedding modelId ('amazon.titan-embed-text-v2:0' or 'cohere.embed-*').
  3. Resolve AWS credentials and align MEAI package versions.
  4. Provide a non-null openTelemetrySourceName only when OTel is configured.

Example fix

// before
services.AddBedrockTextEmbeddingGenerator("anthropic.claude-3-5-sonnet-20240620-v1:0");
// wraps 'Unsupported model provider: anthropic'

// after
services.AddBedrockTextEmbeddingGenerator("amazon.titan-embed-text-v2:0");
Defensive patterns

Strategy: validation

Validate before calling

// validate modelId is a supported embedding id before registration
static bool IsEmbeddingId(string id)
{
    var parts = id.Split('.');
    return parts.Length > 1 && (parts[0].Equals("amazon", StringComparison.OrdinalIgnoreCase) && parts[1].StartsWith("titan-embed-text", StringComparison.OrdinalIgnoreCase))
         || (parts[0].Equals("cohere", StringComparison.OrdinalIgnoreCase) && parts[1].StartsWith("embed-", StringComparison.OrdinalIgnoreCase));
}

Try / catch

try { services.AddBedrockTextEmbeddingGenerator(modelId); }
catch (KernelException ex) { throw new InvalidOperationException($"Bedrock embedding generator init failed: {ex.InnerException?.Message ?? ex.Message}", ex); }

Prevention

When it happens

Trigger: Calling services.AddBedrockTextEmbeddingGenerator(...) and the Build throws: missing AWS credentials, OpenTelemetry misconfiguration, package version mismatch, or a null loggerFactory. The embedding-specific service factory also runs, so an unsupported embedding modelId (NotSupportedException from CreateTextEmbeddingService) gets wrapped here too.

Common situations: Same as 307 plus: passing a non-embedding modelId (e.g. a chat model) to the embedding generator registration, which makes BedrockServiceFactory throw 'Unsupported model provider' that then surfaces wrapped here.

Related errors


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