{"record":{"id":"749ed9549ac285ff","repo":"microsoft/autogen","slug":"response-is-null","errorCode":null,"errorMessage":"Response is null.","messagePattern":"Response is null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs","lineNumber":479,"sourceCode":"        Dictionary<string, JsonElement> state = new();\n        foreach (var agent in this._agentsContainer.LiveAgents)\n        {\n            var agentState = await agent.SaveStateAsync();\n            state[agent.Id.ToString()] = JsonSerializer.SerializeToElement(agentState);\n        }\n        return JsonSerializer.SerializeToElement(state);\n    }\n\n    public async ValueTask OnMessageAsync(Message message, CancellationToken cancellation = default)\n    {\n        switch (message.MessageCase)\n        {\n            case Message.MessageOneofCase.Request:\n                var request = message.Request ?? throw new InvalidOperationException(\"Request is null.\");\n                await HandleRequest(request);\n                break;\n            case Message.MessageOneofCase.Response:\n                var response = message.Response ?? throw new InvalidOperationException(\"Response is null.\");\n                await HandleResponse(response);\n                break;\n            case Message.MessageOneofCase.CloudEvent:\n                var cloudEvent = message.CloudEvent ?? throw new InvalidOperationException(\"CloudEvent is null.\");\n                await HandlePublish(cloudEvent);\n                break;\n            default:\n                throw new InvalidOperationException($\"Unexpected message '{message}'.\");\n        }\n    }\n}\n\n","sourceCodeStart":461,"sourceCodeEnd":492,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs#L461-L492","documentation":"Thrown in OnMessageAsync when the Message oneof case is Response but message.Response is null. As with other oneof guards, protobuf normally keeps the case and payload consistent, so this fires only when the message was constructed or mutated abnormally (reflection, partial parse, or nulling the field after the case was set).","triggerScenarios":"Setting message.Response = null after the oneof case was already set to Response; building Message via reflection without the payload; a peer sending a hand-crafted message with the response case set but no payload.","commonSituations":"Unit tests faking Message instances; client code mutating pooled/reused Message objects; proto schema drift between sender and receiver.","solutions":["Assign a non-null Response when constructing the message: new Message { Response = response }","Use ClearResponse() instead of Response = null when you need to unset the field","Avoid reusing and mutating Message instances across requests; create a fresh Message per send"],"exampleFix":"// before\nmsg.Response = null; // case stays Response -> throws on next dispatch\n\n// after\nmsg.ClearResponse();","handlingStrategy":"validation","validationCode":"if (message.MessageCase == Message.MessageOneofCase.Response && message.Response is null) throw new InvalidOperationException(\"Malformed message\");","typeGuard":"static bool HasResponsePayload(Message m) => m.MessageCase != Message.MessageOneofCase.Response || m.Response is not null;","tryCatchPattern":"try { await runtime.OnMessageAsync(msg); } catch (InvalidOperationException ex) when (ex.Message == \"Response is null.\") { _logger.LogWarning(ex, \"Dropping malformed message\"); }","preventionTips":["Use ClearResponse() to unset oneof fields","Do not reuse/pool Message instances across sends"],"tags":["grpc","protobuf","oneof","null-check"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}