{"record":{"id":"c59bcdcc02875f2f","repo":"microsoft/autogen","slug":"failed-to-deserialize","errorCode":null,"errorMessage":"Failed to deserialize","messagePattern":"Failed to deserialize","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/ProtobufMessageSerializer.cs","lineNumber":28,"sourceCode":"/// Interface for serializing and deserializing agent messages.\n/// </summary>\npublic class ProtobufMessageSerializer : IProtobufMessageSerializer\n{\n    private System.Type _concreteType;\n\n    public ProtobufMessageSerializer(System.Type concreteType)\n    {\n        _concreteType = concreteType;\n    }\n\n    public object Deserialize(Any message)\n    {\n        // Check if the concrete type is a proto IMessage\n        if (typeof(IMessage).IsAssignableFrom(_concreteType))\n        {\n            var nameOfMethod = nameof(Any.Unpack);\n            var result = message.GetType().GetMethods().Where(m => m.Name == nameOfMethod && m.IsGenericMethod).First().MakeGenericMethod(_concreteType).Invoke(message, null);\n            return result as IMessage ?? throw new ArgumentException(\"Failed to deserialize\", nameof(message));\n        }\n\n        // Raise an exception if the concrete type is not a proto IMessage\n        throw new ArgumentException(\"Concrete type must be a proto IMessage\", nameof(_concreteType));\n    }\n\n    public Any Serialize(object message)\n    {\n        // Check if message is a proto IMessage\n        if (message is IMessage protoMessage)\n        {\n            return Any.Pack(protoMessage);\n        }\n\n        // Raise an exception if the message is not a proto IMessage\n        throw new ArgumentException(\"Message must be a proto IMessage\", nameof(message));\n    }\n}","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/ProtobufMessageSerializer.cs#L10-L46","documentation":"Thrown by ProtobufMessageSerializer.Deserialize when reflection-based Any.Unpack<T> for the configured concrete type returns null. Unpack fails (or returns null after casting) when the Any payload's type URL does not match the concrete proto type, or the payload bytes cannot be parsed as that message type.","triggerScenarios":"Calling Deserialize with an Any whose type URL names a different message than _concreteType; truncated or corrupted payload bytes; a type-name collision where two messages share a full name across assemblies.","commonSituations":"Sender and receiver generated protos with different package names; a message type renamed on one side; payload corrupted in transit or re-packed with the wrong descriptor.","solutions":["Confirm the Any.TypeUrl matches the target message's full proto name (Any.Unpack requires an exact descriptor match)","Regenerate protobuf C# classes from the same .proto files on both sender and receiver","If you must re-map types, unpack to the original type and convert, rather than unpacking bytes as a mismatched type"],"exampleFix":"// before\nvar msg = (MyMessage)serializer.Deserialize(anyPackedAsOtherType); // type URL mismatch\n\n// after\nif (any.Is(MyMessage.Descriptor))\n{\n    var msg = any.Unpack<MyMessage>();\n}\nelse\n{\n    // resolve correct serializer based on any.TypeUrl\n}","handlingStrategy":"type-guard","validationCode":"if (!message.Is(_concreteType)) throw new ArgumentException($\"Any type URL {message.TypeUrl} does not match target type\");","typeGuard":"static bool CanUnpack(Any any, Type t) => typeof(IMessage).IsAssignableFrom(t) && any.Is((IMessage)Activator.CreateInstance(t)!);","tryCatchPattern":"try { return serializer.Deserialize(any); } catch (ArgumentException ex) when (ex.Message == \"Failed to deserialize\") { throw new InvalidDataException($\"Payload {any.TypeUrl} does not match {serializer.ConcreteType}\", ex); }","preventionTips":["Check Any.Is(TargetType.Descriptor) before unpacking","Share one .proto source of truth across services and regenerate together"],"tags":["protobuf","serialization","type-mismatch"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}