{"record":{"id":"04f2810851d9c970","repo":"microsoft/semantic-kernel","slug":"an-error-occurred-while-initializing-the-nameof-b-04f281","errorCode":null,"errorMessage":"An error occurred while initializing the {nameof(BedrockTextGenerationService)}: {ex.Message}","messagePattern":"An error occurred while initializing the (.+?): (.+?)","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.cs","lineNumber":101,"sourceCode":"            services.TryAddAWSService<IAmazonBedrockRuntime>();\n        }\n        services.AddKeyedSingleton<ITextGenerationService>(serviceId, (serviceProvider, _) =>\n        {\n            try\n            {\n                IAmazonBedrockRuntime runtime = bedrockRuntime ?? serviceProvider.GetRequiredService<IAmazonBedrockRuntime>();\n                var loggerFactory = serviceProvider.GetService<ILoggerFactory>();\n                // Check if the runtime instance is a proxy object\n                if (runtime.GetType().BaseType == typeof(AmazonServiceClient))\n                {\n                    // Cast to AmazonServiceClient and subscribe to the event\n                    ((AmazonServiceClient)runtime).BeforeRequestEvent += BedrockClientUtilities.BedrockServiceClientRequestHandler;\n                }\n                return new BedrockTextGenerationService(modelId, runtime, loggerFactory);\n            }\n            catch (Exception ex)\n            {\n                throw new KernelException($\"An error occurred while initializing the {nameof(BedrockTextGenerationService)}: {ex.Message}\", ex);\n            }\n        });\n\n        return services;\n    }\n\n    /// <summary>\n    /// Add Amazon Bedrock Text Generation service to the <see cref=\"IServiceCollection\" />.\n    /// </summary>\n    /// <param name=\"services\">The service collection.</param>\n    /// <param name=\"modelId\">The model for text 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    /// <returns>Returns back <see cref=\"IServiceCollection\"/> with a configured service.</returns>\n    [Obsolete(\"Use AddBedrockEmbeddingGenerator instead.\")]\n    public static IServiceCollection AddBedrockTextEmbeddingGenerationService(\n        this IServiceCollection services,\n        string modelId,","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.cs#L83-L119","documentation":"Thrown by AddBedrockTextGeneration (classic Kernel service DI) when the BedrockTextGenerationService factory throws. Same structure as 309: the factory resolves IAmazonBedrockRuntime from the container (GetRequiredService) and constructs the service; failure is wrapped in a KernelException.","triggerScenarios":"services.AddBedrockTextGeneration(modelId) without a registered IAmazonBedrockRuntime, so GetRequiredService throws. Also an invalid/unsupported text modelId that fails inside the BedrockTextGenerationService constructor (which internally calls BedrockServiceFactory.CreateTextGenerationService).","commonSituations":"Missing services.AddAWSService<IAmazonBedrockRuntime>(). Passing an unsupported provider/model id (e.g. a provider not in AI21/AMAZON/ANTHROPIC/COHERE/META/MISTRAL).","solutions":["Register the runtime first: services.AddAWSService<IAmazonBedrockRuntime>();.","Use a supported text-generation modelId (e.g. 'amazon.titan-text-premier-v1:0', 'anthropic.claude-...').","Inspect ex.InnerException for the root cause.","Pass an explicit IAmazonBedrockRuntime if you manage it manually."],"exampleFix":"// before\nservices.AddBedrockTextGeneration(\"stability.stable-image-core\");\n// wraps 'Unsupported model provider: stability'\n\n// after\nservices.AddAWSService<IAmazonBedrockRuntime>();\nservices.AddBedrockTextGeneration(\"amazon.titan-text-premier-v1:0\");","handlingStrategy":"validation","validationCode":"static readonly string[] s_textProviders = { \"ai21\", \"amazon\", \"anthropic\", \"cohere\", \"meta\", \"mistral\" };\nbool IsValidTextModelId(string id) => s_textProviders.Contains(id.Split('.')[0], StringComparer.OrdinalIgnoreCase);","typeGuard":null,"tryCatchPattern":"try { services.AddBedrockTextGeneration(modelId); }\ncatch (KernelException ex) { throw new InvalidOperationException($\"Bedrock text-gen init failed: {ex.InnerException?.Message ?? ex.Message}\", ex); }","preventionTips":["Register IAmazonBedrockRuntime first.","Use a text-generation provider (ai21/amazon/anthropic/cohere/meta/mistral).","Inspect KernelException.InnerException for the wrapped factory error."],"tags":["bedrock","dependency-injection","text-generation","startup","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}