microsoft/semantic-kernel · error · KernelException

Impossible to create type for '{dataContractMetadata.Name}'

Error message

Impossible to create type for '{dataContractMetadata.Name}' data contract.

What it means

Thrown at the end of BuildGrpcOperationDataContractType when `typeBuilder.CreateTypeInfo()` returns null after emitting a dynamic assembly with ProtoContract/ProtoMember attributes. CreateTypeInfo returning null is rare and indicates the CLR refused to materialize the emitted type (e.g. invalid IL, conflicting members).

Source

Thrown at dotnet/src/Functions/Functions.Grpc/GrpcOperationRunner.cs:325

            setterIl.Emit(OpCodes.Ldarg_1);
            setterIl.Emit(OpCodes.Stfld, fieldBuilder);
            setterIl.Emit(OpCodes.Ret);

            //Registering the property get and set methods.
            propertyBuilder.SetGetMethod(getterBuilder);
            propertyBuilder.SetSetMethod(setterBuilder);

            //Add ProtoMember attribute to the data contract with tag/number
            var dataMemberAttributeBuilder = new CustomAttributeBuilder(typeof(ProtoMemberAttribute).GetConstructor([typeof(int)])!, [field.Number]);
            propertyBuilder.SetCustomAttribute(dataMemberAttributeBuilder);
        }

        //Add ProtoContract attribute to the data contract
        var dataContractAttributeBuilder = new CustomAttributeBuilder(typeof(ProtoContractAttribute).GetConstructor(Type.EmptyTypes)!, []);
        typeBuilder.SetCustomAttribute(dataContractAttributeBuilder);

        return typeBuilder.CreateTypeInfo() ??
            throw new KernelException($"Impossible to create type for '{dataContractMetadata.Name}' data contract.");
    }

    /// <summary>
    /// Returns .net type that corresponds to protobuf data type name.
    /// </summary>
    /// <param name="type">The protobuf data type name.</param>
    /// <returns>The .net type.</returns>
    private static Type GetNetType(string type) =>
        type switch
        {
            "TYPE_DOUBLE" => typeof(double),
            "TYPE_FLOAT" => typeof(float),
            "TYPE_INT64" => typeof(long),
            "TYPE_UINT64" => typeof(ulong),
            "TYPE_INT32" => typeof(int),
            "TYPE_FIXED64" => typeof(ulong),
            "TYPE_FIXED32" => typeof(uint),
            "TYPE_BOOL" => typeof(bool),

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the .proto data contract named in the message for unusual or duplicate field definitions.
  2. Simplify the message (rename duplicate fields, remove unsupported types) and retry.
  3. Report the schema if it is valid protobuf but still fails — this is effectively an internal emit failure.
Defensive patterns

Strategy: try-catch

Try / catch

try { /* register gRPC plugin */ }
catch (KernelException ex) when (ex.Message.Contains("Impossible to create type")) {
    logger.LogError(ex, "Dynamic data-contract emit failed for {name}"); throw;
}

Prevention

When it happens

Trigger: A .proto data contract whose field set or types produce an unbuildable dynamic type — duplicate member names, unsupported field types in the emit path, or a reflection-emit constraint violation.

Common situations: Edge-case .proto schemas the dynamic type builder wasn't designed for; protobuf field types that map to incompatible CLR types during emit.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/82ba9d75bfeace3e. Report an issue: GitHub.