microsoft/semantic-kernel · error · KernelException
An error occurred while initializing the {nameof(BedrockChat
Error message
An error occurred while initializing the {nameof(BedrockChatCompletionService)}: {ex.Message} What it means
Thrown by AddBedrockChatCompletion (the classic Kernel service DI extension, not the MEAI one) when the factory that constructs BedrockChatCompletionService throws. The factory resolves IAmazonBedrockRuntime from the provider, optionally casts to AmazonServiceClient to hook BeforeRequestEvent, then constructs the service. Any exception (most often a missing runtime registration) is wrapped in a KernelException.
Source
Thrown at dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.cs:59
}
service.AddKeyedSingleton<IChatCompletionService>(serviceId, (serviceProvider, _) =>
{
try
{
IAmazonBedrockRuntime runtime = bedrockRuntime ?? serviceProvider.GetRequiredService<IAmazonBedrockRuntime>();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
// Check if the runtime instance is a proxy object
if (runtime.GetType().BaseType == typeof(AmazonServiceClient))
{
// Cast to AmazonServiceClient and subscribe to the event
((AmazonServiceClient)runtime).BeforeRequestEvent += BedrockClientUtilities.BedrockServiceClientRequestHandler;
}
return new BedrockChatCompletionService(modelId, runtime, loggerFactory);
}
catch (Exception ex)
{
throw new KernelException($"An error occurred while initializing the {nameof(BedrockChatCompletionService)}: {ex.Message}", ex);
}
});
return service;
}
/// <summary>
/// Add Amazon Bedrock Text Generation service to the <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="modelId">The model for text generation.</param>
/// <param name="bedrockRuntime">The optional <see cref="IAmazonBedrockRuntime" /> to use. If not provided will be retrieved from the Service Collection.</param>
/// <param name="serviceId">The optional service ID.</param>
/// <returns>Returns back <see cref="IServiceCollection"/> with a configured service.</returns>
public static IServiceCollection AddBedrockTextGenerationService(
this IServiceCollection services,
string modelId,
IAmazonBedrockRuntime? bedrockRuntime = null,View on GitHub (pinned to c028a0c7dc)
Solutions
- Register the Bedrock runtime first: services.AddAWSService<IAmazonBedrockRuntime>(); then services.AddBedrockChatCompletion(modelId);.
- Pass an explicit IAmazonBedrockRuntime instance to AddBedrockChatCompletion if you construct it manually.
- Inspect ex.InnerException for the real cause (typically 'No service for type IAmazonBedrockRuntime').
- Validate the modelId is Converse-compatible.
Example fix
// before
services.AddBedrockChatCompletion("anthropic.claude-3-5-sonnet-20240620-v1:0");
// KernelException: ... initializing the BedrockChatCompletionService: No service for type 'Amazon.BedrockRuntime.IAmazonBedrockRuntime'
// after
services.AddAWSService<IAmazonBedrockRuntime>();
services.AddBedrockChatCompletion("anthropic.claude-3-5-sonnet-20240620-v1:0"); Defensive patterns
Strategy: validation
Validate before calling
// register the runtime before the chat completion service services.AddAWSService<IAmazonBedrockRuntime>(); services.AddBedrockChatCompletion(modelId);
Try / catch
try { services.AddBedrockChatCompletion(modelId); }
catch (KernelException ex) when (ex.InnerException?.Message.Contains("IAmazonBedrockRuntime") == true)
{ throw new InvalidOperationException("Register IAmazonBedrockRuntime (e.g. AddAWSService<IAmazonBedrockRuntime>()) first.", ex); } Prevention
- Always register the Bedrock runtime before adding SK Bedrock services.
- Pass an explicit IAmazonBedrockRuntime when you construct it manually.
- Inspect KernelException.InnerException for the missing-service detail.
When it happens
Trigger: Calling services.AddBedrockChatCompletion(modelId, ...) without first registering an IAmazonBedrockRuntime in the container, so serviceProvider.GetRequiredService<IAmazonBedrockRuntime>() throws InvalidOperationException. Also when an explicit bedrockRuntime arg is a proxy/mock whose BaseType is not AmazonServiceClient (the cast is guarded, so this is benign) or the BedrockChatCompletionService ctor throws.
Common situations: Forgetting services.AddAWSService<IAmazonBedrockRuntime>() (or AddAmazonBedrockRuntime) before AddBedrockChatCompletion. Passing an invalid modelId. Container scoped-service issues in Blazor/async disposal.
Related errors
- An error occurred while initializing the Bedrock {nameof(ICh
- An error occurred while initializing the {nameof(IEmbeddingG
- An error occurred while initializing the {nameof(BedrockText
- An error occurred while initializing the {nameof(BedrockText
- AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/2d528844aa637dc7.
Report an issue: GitHub.