dotnet/machinelearning · error · InvalidOperationException

Bad data kind in ColumnTypeExtensions.NumberTypeFromKind: {k

Error message

Bad data kind in ColumnTypeExtensions.NumberTypeFromKind: {kind}

What it means

NumberTypeFromKind is the internal switch mapping InternalDataKind values (numeric kinds like I1..R8) to NumberDataViewType instances. If the kind is not a numeric kind (or is an undefined kind value), it asserts the invariant and throws InvalidOperationException 'Bad data kind in ColumnTypeExtensions.NumberTypeFromKind: {kind}'. It signals a non-numeric or corrupt kind reached a numeric-only code path.

Source

Thrown at src/Microsoft.ML.Core/Data/ColumnTypeExtensions.cs:166

                return NumberDataViewType.Int16;
            case InternalDataKind.U2:
                return NumberDataViewType.UInt16;
            case InternalDataKind.I4:
                return NumberDataViewType.Int32;
            case InternalDataKind.U4:
                return NumberDataViewType.UInt32;
            case InternalDataKind.I8:
                return NumberDataViewType.Int64;
            case InternalDataKind.U8:
                return NumberDataViewType.UInt64;
            case InternalDataKind.R4:
                return NumberDataViewType.Single;
            case InternalDataKind.R8:
                return NumberDataViewType.Double;
        }

        Contracts.Assert(false);
        throw new InvalidOperationException($"Bad data kind in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromKind)}: {kind}");
    }
}

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure the DataKind is numeric (I1/I2/I4/I8/U1/U2/U4/U8/R4/R8) before requesting a NumberDataViewType.
  2. For non-numeric kinds use the appropriate DataView type (TextDataViewType.Instance, BooleanDataViewType.Instance, DateTimeDataViewType.Instance) instead.
  3. Validate kind strings via InternalDataKind.TryParse and whitelist numeric kinds before conversion.
  4. Align Microsoft.Data.DataView and Microsoft.ML package versions so kind values are interpreted consistently.

Example fix

// before
var type = ColumnTypeExtensions.NumberTypeFromType(typeof(string));
// after
var type = typeof(string) == t
    ? TextDataViewType.Instance
    : ColumnTypeExtensions.NumberTypeFromType(t);
Defensive patterns

Strategy: type-guard

Validate before calling

var numericKinds = new HashSet<InternalDataKind> { InternalDataKind.I1, InternalDataKind.I2,
    InternalDataKind.I4, InternalDataKind.I8, InternalDataKind.U1, InternalDataKind.U2,
    InternalDataKind.U4, InternalDataKind.U8, InternalDataKind.R4, InternalDataKind.R8 };
if (!numericKinds.Contains(kind))
    throw new ArgumentException($"{kind} is not a numeric DataKind");

Type guard

static bool IsNumericKind(InternalDataKind kind) =>
    kind is InternalDataKind.I1 or InternalDataKind.I2 or InternalDataKind.I4
        or InternalDataKind.I8 or InternalDataKind.U1 or InternalDataKind.U2
        or InternalDataKind.U4 or InternalDataKind.U8 or InternalDataKind.R4
        or InternalDataKind.R8;

Try / catch

try
{
    numberType = ColumnTypeExtensions.NumberTypeFromType(t);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("NumberTypeFromKind"))
{
    // kind is non-numeric; map manually
    numberType = null;
}

Prevention

When it happens

Trigger: Calling NumberTypeFromType with a type that TryGetDataKind maps to a non-numeric InternalDataKind (e.g. TX (text), BL (bool), DT (DateTime), TS (TimeSpan)), or passing a raw out-of-range InternalDataKind value into the kind path of PrimitiveTypeFromKind.

Common situations: Attempting to build a number column type from a string/bool/datetime kind; stale serialized kind values from a different Microsoft.Data.DataView version; constructing DataView types programmatically from user-supplied kind strings.

Related errors


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