{"record":{"id":"2b8a01048a72a4e1","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-created-due-to-an-error-re-2b8a01","errorCode":null,"errorMessage":"The thread could not be created due to an error response from the service.","messagePattern":"The thread could not be created due to an error response from the service\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Bedrock/BedrockAgentThread.cs","lineNumber":53,"sourceCode":"        return base.CreateAsync(cancellationToken);\n    }\n\n    /// <inheritdoc />\n    protected override async Task<string?> CreateInternalAsync(CancellationToken cancellationToken)\n    {\n        const string ErrorMessage = \"The thread could not be created due to an error response from the service.\";\n\n        try\n        {\n            var response = await this._runtimeClient.CreateSessionAsync(\n                request: new(),\n                cancellationToken: cancellationToken).ConfigureAwait(false);\n\n            return response.SessionId;\n        }\n        catch (AmazonBedrockAgentRuntimeException ex)\n        {\n            throw new AgentThreadOperationException(ErrorMessage, ex);\n        }\n        catch (AggregateException ex)\n        {\n            throw new AgentThreadOperationException(ErrorMessage, ex);\n        }\n    }\n\n    /// <inheritdoc />\n    protected override async Task DeleteInternalAsync(CancellationToken cancellationToken)\n    {\n        const string ErrorMessage = \"The thread could not be deleted due to an error response from the service.\";\n\n        try\n        {\n            var endSessionResponse = await this._runtimeClient.EndSessionAsync(\n                request: new()\n                {\n                    SessionIdentifier = this.Id","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Bedrock/BedrockAgentThread.cs#L35-L71","documentation":"Thrown during thread creation when CreateSessionAsync on the Bedrock Agent runtime returns an AmazonBedrockAgentRuntimeException. This is a wrapper (AgentThreadOperationException) that preserves the original AWS exception as its InnerException, giving callers a single exception type to catch for thread lifecycle failures while retaining diagnostic detail.","triggerScenarios":"Calling CreateAsync on a BedrockAgentThread when the AWS Bedrock Agent Runtime service rejects the CreateSession call — invalid or expired AWS credentials, missing IAM permissions for `bedrock:CreateSession`, service throttling, or a regional outage.","commonSituations":"Credentials from a stale profile; the IAM role/policy lacks `bedrock-agent-runtime:CreateSession`; hitting a service quota; wrong region endpoint configured on the client.","solutions":["Inspect the InnerException (AmazonBedrockAgentRuntimeException) for the AWS ErrorCode and message to identify auth, permission, or throttling causes.","Verify the IAM principal has `bedrock-agent-runtime:CreateSession` permission and that credentials are valid and non-expired.","Confirm the IAmazonBedrockAgentRuntime client is configured with the correct region where the agent exists.","Implement retry with exponential backoff for transient throttling/errors (the SDK's built-in retry may already apply; raise MaxRetries if needed)."],"exampleFix":"// before\ntry { await thread.CreateAsync(cancellationToken); }\ncatch (Exception ex) { /* too broad, loses structure */ }\n\n// after\ntry { await thread.CreateAsync(cancellationToken); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is AmazonBedrockAgentRuntimeException aws)\n{\n    logger.LogError(aws, \"Session create failed: {ErrorCode}\", aws.ErrorCode);\n    throw;\n}","handlingStrategy":"try-catch","validationCode":"// Verify credentials and permissions before thread creation\nvar sts = new AmazonSecurityTokenServiceClient();\nvar identity = await sts.GetCallerIdentityAsync(new GetCallerIdentityRequest());\nlogger.LogInformation(\"Using AWS identity: {Arn}\", identity.Arn);\n// then proceed to create the thread","typeGuard":null,"tryCatchPattern":"try { await thread.CreateAsync(cancellationToken); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is AmazonBedrockAgentRuntimeException aws)\n{\n    logger.LogError(aws, \"CreateSession failed: {Code} {Message}\", aws.ErrorCode, aws.Message);\n    // branch on ErrorCode: AccessDenied -> fix IAM; Throttling -> retry; etc.\n}","preventionTips":["Grant `bedrock-agent-runtime:CreateSession` to the calling principal's IAM policy.","Use named/instance profile credentials with an explicit expiry check in long-running services.","Configure the AWS SDK client with an appropriate region and MaxRetries for transient errors."],"tags":["bedrock","agents","session","aws-service-error","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}