dotnet/machinelearning · error · InvalidOperationException

Unsupported type used in mapping.

Error message

Unsupported type {type} used in mapping.

What it means

ValueMapping's GetPrimitiveType derives the column type for mapping keys/values from the .NET type of the mapping dictionary. When the type cannot be converted to an InternalDataKind (TryGetDataKind fails), it throws InvalidOperationException because that type is not supported in a value mapping.

Solutions

  1. Change the mapping dictionary's key/value types to ML.NET-supported primitives (int, float, string, bool, DateTime, etc.)
  2. Convert unsupported values to strings or numeric codes before constructing the mapping
  3. Split complex objects into multiple primitive-valued mappings (one per field)

Example fix

// before
var map = new Dictionary<string, MyObject>();
// after
var map = new Dictionary<string, string>(); // map id -> serialized/selected primitive value
Defensive patterns

Strategy: type-guard

Type guard

static bool IsMlnetPrimitive(Type t) =>
    t == typeof(bool) || t == typeof(char) || t == typeof(string) ||
    t == typeof(int) || t == typeof(uint) || t == typeof(long) || t == typeof(ulong) ||
    t == typeof(short) || t == typeof(ushort) || t == typeof(byte) || t == typeof(sbyte) ||
    t == typeof(float) || t == typeof(double) || t == typeof(decimal) ||
    t == typeof(DateTime) || t == typeof(TimeSpan) || t == typeof(Guid);
var kvType = typeof(KeyValuePair<,>).MakeGenericType(map.GetType().GetGenericArguments());
foreach (var t in map.GetType().GetGenericArguments())
    if (!IsMlnetPrimitive(t)) throw new InvalidOperationException($"Unsupported mapping type {t}");

Prevention

When it happens

Trigger: Building a ValueMappingEstimator with a dictionary whose key or value type is not a primitive ML.NET type (e.g. custom object, nullable type without support, enum handled improperly, multi-dimensional arrays); called via keyType/valueType helpers.

Common situations: Passing Dictionary<CustomClass, string> or Dictionary<string, MyClass?> to ValueMapping; using types not in ML.NET's supported primitive set (bool, char, numerics, string, DateTime, TimeSpan, etc.); arrays/element types of unsupported kind.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/1b4505902054e1e8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.ML.Data/Transforms/ValueMapping.cs:202

    /// The DataViewHelper provides a set of static functions to create a DataView given a list of keys and values.
    /// </summary>
    internal class DataViewHelper
    {
        /// <summary>
        /// Helper function to retrieve the Primitive type given a Type
        /// </summary>
        internal static PrimitiveDataViewType GetPrimitiveType(Type rawType, out bool isVectorType)
        {
            Type type = rawType;
            isVectorType = false;
            if (type.IsArray)
            {
                type = rawType.GetElementType();
                isVectorType = true;
            }

            if (!type.TryGetDataKind(out InternalDataKind kind))
                throw new InvalidOperationException($"Unsupported type {type} used in mapping.");

            return ColumnTypeExtensions.PrimitiveTypeFromKind(kind);
        }

        /// <summary>
        /// Helper function for a reverse lookup given value. This is used for generating the metadata of the value column.
        /// </summary>

        private static ValueGetter<VBuffer<ReadOnlyMemory<char>>> GetKeyValueGetter<TKey>(TKey[] keys)
        {
            return
                (ref VBuffer<ReadOnlyMemory<char>> dst) =>
                {
                    var editor = VBufferEditor.Create(ref dst, keys.Length);
                    for (int i = 0; i < keys.Length; i++)
                        editor.Values[i] = keys[i].ToString().AsMemory();
                    dst = editor.Commit();
                };

View on GitHub (pinned to 7b76e69cf9)