microsoft/semantic-kernel · error · ArgumentNullException
runtimeClient
Error message
runtimeClient
What it means
This is a constructor guard on BedrockAgentThread. The thread requires an IAmazonBedrockAgentRuntime client to create, delete, and interact with sessions, so a null client would cause NullReferenceException later during any operation. The check fails fast with the parameter name 'runtimeClient' so the caller knows exactly which argument was null.
Source
Thrown at dotnet/src/Agents/Bedrock/BedrockAgentThread.cs:24
using Amazon.BedrockAgentRuntime;
namespace Microsoft.SemanticKernel.Agents.Bedrock;
/// <summary>
/// Represents a conversation thread for a Bedrock agent.
/// </summary>
public sealed class BedrockAgentThread : AgentThread
{
private readonly IAmazonBedrockAgentRuntime _runtimeClient;
/// <summary>
/// Initializes a new instance of the <see cref="BedrockAgentThread"/> class.
/// </summary>
/// <param name="runtimeClient">A client used to interact with the Bedrock Agent runtime service.</param>
/// <param name="sessionId">An optional session Id to continue an existing session.</param>
/// <exception cref="ArgumentNullException"></exception>
public BedrockAgentThread(IAmazonBedrockAgentRuntime runtimeClient, string? sessionId = null)
{
this._runtimeClient = runtimeClient ?? throw new ArgumentNullException(nameof(runtimeClient));
this.Id = sessionId;
}
/// <summary>
/// Creates the thread and returns the thread id.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that completes when the thread has been created.</returns>
public new Task CreateAsync(CancellationToken cancellationToken = default)
{
return base.CreateAsync(cancellationToken);
}
/// <inheritdoc />
protected override async Task<string?> CreateInternalAsync(CancellationToken cancellationToken)
{
const string ErrorMessage = "The thread could not be created due to an error response from the service.";
View on GitHub (pinned to c028a0c7dc)
Solutions
- Construct the client before the thread: `new AmazonBedrockAgentRuntimeClient(credentials, region)` and pass the non-null instance.
- If using DI, register the AWS service: `services.AddAWSService<IAmazonBedrockAgentRuntime>()` and inject it into the component that creates the thread.
- Add a null-check in your own factory/build method so the thread is never constructed with a null client.
Example fix
// before var thread = new BedrockAgentThread(runtimeClient: null, sessionId); // after var client = new AmazonBedrockAgentRuntimeClient(awsCredentials, RegionEndpoint.USEast1); var thread = new BedrockAgentThread(client, sessionId);
Defensive patterns
Strategy: validation
Validate before calling
IAmazonBedrockAgentRuntime? runtimeClient = ResolveClient();
if (runtimeClient is null)
throw new InvalidOperationException("Bedrock runtime client could not be resolved; check DI/AWS config.");
var thread = new BedrockAgentThread(runtimeClient, sessionId); Type guard
static bool IsValidBedrockRuntimeClient(IAmazonBedrockAgentRuntime? c) => c is not null;
Try / catch
try { var thread = new BedrockAgentThread(client, sessionId); }
catch (ArgumentNullException ex) when (ex.ParamName == "runtimeClient")
{
logger.LogError("Bedrock runtime client was null. Register AddAWSService<IAmazonBedrockAgentRuntime>().");
throw;
} Prevention
- Register AWS services in DI at startup and fail fast if resolution returns null.
- Use a factory method that guarantees a non-null client or throws a descriptive error.
- Add an integration smoke test that constructs the thread at app startup.
When it happens
Trigger: Instantiating `new BedrockAgentThread(null)` or `new BedrockAgentThread(runtimeClient: null, sessionId)`. Passing a DI-resolved client that resolved to null because the AWS service registration was missing or misconfigured.
Common situations: Forgot to register `AddAWSService<IAmazonBedrockAgentRuntime>()` in the DI container; the client field was never assigned before thread construction; a factory method returned null under an error path.
Related errors
- client
- Message role must be either Assistant or User.
- The thread could not be created due to an error response fro
- The thread could not be deleted due to an error response fro
- Agent did not reach status {status} within the specified tim
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/3607ef806be11d2d.
Report an issue: GitHub.