microsoft/semantic-kernel · error · KernelException
An error occurred while initializing the Bedrock {nameof(ICh
Error message
An error occurred while initializing the Bedrock {nameof(IChatClient)}: {ex.Message} What it means
Thrown by the AddBedrockChatCompletionAsync DI extension (Microsoft.Extensions.DependencyInjection factory) when any exception occurs while building the Bedrock IChatClient via the MEAI client builder pipeline (UseLogging, UseOpenTelemetry, UseKernelFunctionInvocation, Build). The inner exception is wrapped in a KernelException and surfaced as the Message, so 'ex.Message' becomes the actionable detail.
Source
Thrown at dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.DependencyInjection.cs:72
}
var builder = runtime
.AsIChatClient(modelId)
.AsBuilder();
if (loggerFactory is not null)
{
builder.UseLogging(loggerFactory);
}
builder.UseOpenTelemetry(loggerFactory, openTelemetrySourceName, openTelemetryConfig);
return builder
.UseKernelFunctionInvocation(loggerFactory)
.Build(serviceProvider);
}
catch (Exception ex)
{
throw new KernelException($"An error occurred while initializing the Bedrock {nameof(IChatClient)}: {ex.Message}", ex);
}
});
return services;
}
/// <summary>
/// Add Amazon Bedrock <see cref="IEmbeddingGenerator"/> to the <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="modelId">The model for embedding 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>
/// <param name="openTelemetrySourceName">An optional source name that will be used on the telemetry data.</param>
/// <param name="openTelemetryConfig">An optional callback that can be used to configure the <see cref="OpenTelemetryEmbeddingGenerator{String, Embedding}"/> instance.</param>
/// <returns>Returns back <see cref="IServiceCollection"/> with a configured <see cref="IEmbeddingGenerator"/>.</returns>
public static IServiceCollection AddBedrockEmbeddingGenerator(
this IServiceCollection services,View on GitHub (pinned to c028a0c7dc)
Solutions
- Read ex.Message / ex.InnerException of the KernelException — it contains the real cause (e.g. credential failure, version mismatch).
- Ensure AWS credentials are resolvable (environment, profile, or explicit BasicAWSCredentials) before registering the client.
- Align package versions of Microsoft.Extensions.AI, Microsoft.Extensions.AI.OpenAI/Abstractions, and Connectors.Amazon.
- If you passed openTelemetrySourceName, ensure it is non-null and the OTel provider is configured.
Example fix
// before
services.AddBedrockChatCompletionAsync("anthropic.claude-3-5-sonnet-20240620-v1:0");
// KernelException: An error occurred while initializing the Bedrock IChatClient: ...
// after
services.AddBedrockChatCompletionAsync("anthropic.claude-3-5-sonnet-20240620-v1:0",
settings: null, openTelemetrySourceName: "MyApp");
// and ensure AWS SDK credentials are set before host build Defensive patterns
Strategy: validation
Validate before calling
// ensure AWS credentials resolve and packages align before DI registration
var creds = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain().TryGetProfile("default", out _);
if (!creds && Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID") is null)
throw new InvalidOperationException("AWS credentials not configured."); Try / catch
try { services.AddBedrockChatCompletionAsync(modelId, settings, openTelemetrySourceName: "MyApp"); }
catch (KernelException ex) { throw new InvalidOperationException($"Bedrock chat client init failed: {ex.InnerException?.Message ?? ex.Message}", ex); } Prevention
- Inspect KernelException.InnerException for the true cause.
- Pin matching versions of Connectors.Amazon and Microsoft.Extensions.AI packages.
- Ensure AWS credentials are resolvable and pass a non-null openTelemetrySourceName only with OTel configured.
When it happens
Trigger: Calling services.AddBedrockChatCompletionAsync(modelId, ...) and the underlying client Build throws: missing AWS credentials, invalid OpenTelemetry/source-name config, version mismatch between Connectors.Amazon and the MEAI/OpenAI packages, or a null loggerFactory passed to UseLogging. Any throw inside the builder is re-wrapped here.
Common situations: No AWS credentials configured (AWS_PROFILE/credential chain failure). Package version mismatch between Connectors.Amazon, Microsoft.Extensions.AI, and Microsoft.Extensions.AI.OpenAI. Passing a null openTelemetrySourceName. Specifying a modelId the factory can't resolve (which throws NotSupportedException upstream, then gets wrapped here).
Related errors
- An error occurred while initializing the {nameof(IEmbeddingG
- An error occurred while initializing the {nameof(BedrockChat
- 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/a4a55ed16efedc85.
Report an issue: GitHub.