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
- Ensure the DataKind is numeric (I1/I2/I4/I8/U1/U2/U4/U8/R4/R8) before requesting a NumberDataViewType.
- For non-numeric kinds use the appropriate DataView type (TextDataViewType.Instance, BooleanDataViewType.Instance, DateTimeDataViewType.Instance) instead.
- Validate kind strings via InternalDataKind.TryParse and whitelist numeric kinds before conversion.
- 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
- Check kind is numeric before requesting a NumberDataViewType.
- Use TextDataViewType/BooleanDataViewType/DateTimeDataViewType for non-numeric kinds.
- Keep Microsoft.Data.DataView and Microsoft.ML versions aligned to avoid kind-value drift.
- Never construct InternalDataKind from unchecked user input.
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
- Bad type in ColumnTypeExtensions.NumberTypeFromType: {type}
- The data type '{labelType}' is not handled currently.
- The data type '{dataKind}' is not handled currently.
- Invalid call to 'GetGetter'
- The provided model file {filePath} doesn't exist.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/5638b127452b697b.
Report an issue: GitHub.