{"record":{"id":"3607ef806be11d2d","repo":"microsoft/semantic-kernel","slug":"runtimeclient","errorCode":null,"errorMessage":"runtimeClient","messagePattern":"runtimeClient","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Bedrock/BedrockAgentThread.cs","lineNumber":24,"sourceCode":"using Amazon.BedrockAgentRuntime;\n\nnamespace Microsoft.SemanticKernel.Agents.Bedrock;\n/// <summary>\n/// Represents a conversation thread for a Bedrock agent.\n/// </summary>\npublic sealed class BedrockAgentThread : AgentThread\n{\n    private readonly IAmazonBedrockAgentRuntime _runtimeClient;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"BedrockAgentThread\"/> class.\n    /// </summary>\n    /// <param name=\"runtimeClient\">A client used to interact with the Bedrock Agent runtime service.</param>\n    /// <param name=\"sessionId\">An optional session Id to continue an existing session.</param>\n    /// <exception cref=\"ArgumentNullException\"></exception>\n    public BedrockAgentThread(IAmazonBedrockAgentRuntime runtimeClient, string? sessionId = null)\n    {\n        this._runtimeClient = runtimeClient ?? throw new ArgumentNullException(nameof(runtimeClient));\n        this.Id = sessionId;\n    }\n\n    /// <summary>\n    /// Creates the thread and returns the thread id.\n    /// </summary>\n    /// <param name=\"cancellationToken\">The <see cref=\"CancellationToken\"/> to monitor for cancellation requests. The default is <see cref=\"CancellationToken.None\"/>.</param>\n    /// <returns>A task that completes when the thread has been created.</returns>\n    public new Task CreateAsync(CancellationToken cancellationToken = default)\n    {\n        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","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Bedrock/BedrockAgentThread.cs#L6-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar thread = new BedrockAgentThread(runtimeClient: null, sessionId);\n\n// after\nvar client = new AmazonBedrockAgentRuntimeClient(awsCredentials, RegionEndpoint.USEast1);\nvar thread = new BedrockAgentThread(client, sessionId);","handlingStrategy":"validation","validationCode":"IAmazonBedrockAgentRuntime? runtimeClient = ResolveClient();\nif (runtimeClient is null)\n    throw new InvalidOperationException(\"Bedrock runtime client could not be resolved; check DI/AWS config.\");\nvar thread = new BedrockAgentThread(runtimeClient, sessionId);","typeGuard":"static bool IsValidBedrockRuntimeClient(IAmazonBedrockAgentRuntime? c) => c is not null;","tryCatchPattern":"try { var thread = new BedrockAgentThread(client, sessionId); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"runtimeClient\")\n{\n    logger.LogError(\"Bedrock runtime client was null. Register AddAWSService<IAmazonBedrockAgentRuntime>().\");\n    throw;\n}","preventionTips":["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."],"tags":["bedrock","agents","null-argument","constructor","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}