{"record":{"id":"a4a55ed16efedc85","repo":"microsoft/semantic-kernel","slug":"an-error-occurred-while-initializing-the-bedrock","errorCode":null,"errorMessage":"An error occurred while initializing the Bedrock {nameof(IChatClient)}: {ex.Message}","messagePattern":"An error occurred while initializing the Bedrock (.+?): (.+?)","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.DependencyInjection.cs","lineNumber":72,"sourceCode":"                }\n                var builder = runtime\n                    .AsIChatClient(modelId)\n                    .AsBuilder();\n\n                if (loggerFactory is not null)\n                {\n                    builder.UseLogging(loggerFactory);\n                }\n\n                builder.UseOpenTelemetry(loggerFactory, openTelemetrySourceName, openTelemetryConfig);\n\n                return builder\n                    .UseKernelFunctionInvocation(loggerFactory)\n                    .Build(serviceProvider);\n            }\n            catch (Exception ex)\n            {\n                throw new KernelException($\"An error occurred while initializing the Bedrock {nameof(IChatClient)}: {ex.Message}\", ex);\n            }\n        });\n\n        return services;\n    }\n\n    /// <summary>\n    /// Add Amazon Bedrock <see cref=\"IEmbeddingGenerator\"/> to the <see cref=\"IServiceCollection\" />.\n    /// </summary>\n    /// <param name=\"services\">The service collection.</param>\n    /// <param name=\"modelId\">The model for embedding generation.</param>\n    /// <param name=\"bedrockRuntime\">The optional <see cref=\"IAmazonBedrockRuntime\" /> to use. If not provided will be retrieved from the Service Collection.</param>\n    /// <param name=\"serviceId\">The optional service ID.</param>\n    /// <param name=\"openTelemetrySourceName\">An optional source name that will be used on the telemetry data.</param>\n    /// <param name=\"openTelemetryConfig\">An optional callback that can be used to configure the <see cref=\"OpenTelemetryEmbeddingGenerator{String, Embedding}\"/> instance.</param>\n    /// <returns>Returns back <see cref=\"IServiceCollection\"/> with a configured <see cref=\"IEmbeddingGenerator\"/>.</returns>\n    public static IServiceCollection AddBedrockEmbeddingGenerator(\n        this IServiceCollection services,","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.DependencyInjection.cs#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nservices.AddBedrockChatCompletionAsync(\"anthropic.claude-3-5-sonnet-20240620-v1:0\");\n// KernelException: An error occurred while initializing the Bedrock IChatClient: ...\n\n// after\nservices.AddBedrockChatCompletionAsync(\"anthropic.claude-3-5-sonnet-20240620-v1:0\",\n    settings: null, openTelemetrySourceName: \"MyApp\");\n// and ensure AWS SDK credentials are set before host build","handlingStrategy":"validation","validationCode":"// ensure AWS credentials resolve and packages align before DI registration\nvar creds = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain().TryGetProfile(\"default\", out _);\nif (!creds && Environment.GetEnvironmentVariable(\"AWS_ACCESS_KEY_ID\") is null)\n    throw new InvalidOperationException(\"AWS credentials not configured.\");","typeGuard":null,"tryCatchPattern":"try { services.AddBedrockChatCompletionAsync(modelId, settings, openTelemetrySourceName: \"MyApp\"); }\ncatch (KernelException ex) { throw new InvalidOperationException($\"Bedrock chat client init failed: {ex.InnerException?.Message ?? ex.Message}\", ex); }","preventionTips":["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."],"tags":["bedrock","dependency-injection","startup","aws-credentials","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}