microsoft/semantic-kernel · error · KernelException
An error occurred while initializing the {nameof(BedrockText
Error message
An error occurred while initializing the {nameof(BedrockTextGenerationService)}: {ex.Message} What it means
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.
Source
Thrown at dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.cs:101
services.TryAddAWSService<IAmazonBedrockRuntime>();
}
services.AddKeyedSingleton<ITextGenerationService>(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 BedrockTextGenerationService(modelId, runtime, loggerFactory);
}
catch (Exception ex)
{
throw new KernelException($"An error occurred while initializing the {nameof(BedrockTextGenerationService)}: {ex.Message}", ex);
}
});
return services;
}
/// <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>
[Obsolete("Use AddBedrockEmbeddingGenerator instead.")]
public static IServiceCollection AddBedrockTextEmbeddingGenerationService(
this IServiceCollection services,
string modelId,View on GitHub (pinned to c028a0c7dc)
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.
Example fix
// before
services.AddBedrockTextGeneration("stability.stable-image-core");
// wraps 'Unsupported model provider: stability'
// after
services.AddAWSService<IAmazonBedrockRuntime>();
services.AddBedrockTextGeneration("amazon.titan-text-premier-v1:0"); Defensive patterns
Strategy: validation
Validate before calling
static readonly string[] s_textProviders = { "ai21", "amazon", "anthropic", "cohere", "meta", "mistral" };
bool IsValidTextModelId(string id) => s_textProviders.Contains(id.Split('.')[0], StringComparer.OrdinalIgnoreCase); Try / catch
try { services.AddBedrockTextGeneration(modelId); }
catch (KernelException ex) { throw new InvalidOperationException($"Bedrock text-gen init failed: {ex.InnerException?.Message ?? ex.Message}", ex); } Prevention
- Register IAmazonBedrockRuntime first.
- Use a text-generation provider (ai21/amazon/anthropic/cohere/meta/mistral).
- Inspect KernelException.InnerException for the wrapped factory error.
When it happens
Trigger: 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).
Common situations: Missing services.AddAWSService<IAmazonBedrockRuntime>(). Passing an unsupported provider/model id (e.g. a provider not in AI21/AMAZON/ANTHROPIC/COHERE/META/MISTRAL).
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(BedrockChat
- An error occurred while initializing the {nameof(BedrockText
- Response is null
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/04f2810851d9c970.
Report an issue: GitHub.