microsoft/autogen · error · InvalidOperationException

CloudEvent is null.

Error message

CloudEvent is null.

What it means

GrpcAgentRuntime.HandlePublish throws InvalidOperationException when an incoming CloudEvent is null. Published messages arrive as CloudEvent envelopes over the gRPC stream; a null envelope fails the handler's defensive validation before topic/sender extraction (TopicId, Attributes) begins.

Source

Thrown at dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs:218

        }
        if (request.RequestId is null)
        {
            throw new InvalidOperationException("RequestId is null.");
        }

        if (_pendingRequests.TryRemove(request.RequestId, out var resultSink))
        {
            var payload = request.Payload;
            var message = payload.ToObject(SerializationRegistry);
            resultSink.SetResult(message);
        }
    }

    private async ValueTask HandlePublish(CloudEvent evt, CancellationToken cancellationToken = default)
    {
        if (evt is null)
        {
            throw new InvalidOperationException("CloudEvent is null.");
        }
        if (evt.ProtoData is null)
        {
            throw new InvalidOperationException("ProtoData is null.");
        }
        if (evt.Attributes is null)
        {
            throw new InvalidOperationException("Attributes is null.");
        }

        var topic = new TopicId(evt.Type, evt.Source);
        Contracts.AgentId? sender = null;
        if (evt.Attributes.TryGetValue(Constants.AGENT_SENDER_TYPE_ATTR, out var typeValue) && evt.Attributes.TryGetValue(Constants.AGENT_SENDER_KEY_ATTR, out var keyValue))
        {
            sender = new Contracts.AgentId
            {
                Type = typeValue.CeString,
                Key = keyValue.CeString

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pin matching Microsoft.AutoGen package versions across all publishing and subscribing processes.
  2. Check custom message-sink/router glue for paths that can forward a null event.
  3. In tests, construct a CloudEvent with Type, Source, ProtoData, and Attributes set.

Example fix

// before (test double)
await runtime.HandlePublish(null); // throws

// after
var evt = new CloudEvent
{
    Type = topic.Type,
    Source = topic.Source,
    ProtoData = message.ToProtobuf(registry),
    Attributes = { /* ... */ },
};
await runtime.HandlePublish(evt);
Defensive patterns

Strategy: validation

Validate before calling

if (cloudEvent is null) { /* drop/log the malformed frame; verify proto version alignment */ }

Type guard

static bool IsValidCloudEvent(CloudEvent? e) => e is not null && e.ProtoData is not null && e.Attributes is not null;

Try / catch

try { await runtime.HandlePublish(evt); }
catch (InvalidOperationException) { /* log envelope; check contract/version alignment */ }

Prevention

When it happens

Trigger: The message router delivers a null CloudEvent to HandlePublish — malformed stream data, incompatible proto versions between publisher and runtime, or direct internal invocation with null (custom sinks/tests).

Common situations: Version skew of the AgentRpc/CloudEvent contract between processes; a custom IMessageSink<Message> implementation forwarding null; test harnesses exercising HandlePublish directly.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/b0dbe1789a72da83. Report an issue: GitHub.