dotnet/machinelearning · error · ArgumentException

Unsupported data type of

Error message

Unsupported data type of {typeof(T)} to convert to Tensor.

What it means

CastDataAndReturnAsTensor converts a scalar value of type T into a TensorFlow Tensor. It supports float, double, and ReadOnlyMemory<char>; any other T reaches the final throw of ArgumentException naming the unsupported type.

Solutions

  1. Map the input column to float, double, or text types that CastData supports (e.g. ChangeColumnType/Convert to float).
  2. Add an explicit cast in the pipeline (ColumnCopying/TypeConverting estimator) so only supported types reach the TF runner.
  3. Extend CastDataAndReturnAsTensor to handle the missing type (e.g. int -> new Tensor((int)(object)data)) in a custom fork.
  4. Validate the IDataView schema against the model's expected input dtypes before calling Fit/Transform.

Example fix

// before
int value = 3;
CastDataAndReturnAsTensor<int>(value); // ArgumentException
// after
float value = 3f;
CastDataAndReturnAsTensor<float>(value);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = data is float || data is double || data is ReadOnlyMemory<char>;
if (!ok) throw new InvalidOperationException($"Column type {typeof(T)} unsupported for TF scalar input");

Type guard

bool IsCastable<T>(T v) => v is float || v is double || (v is ReadOnlyMemory<char>);

Try / catch

try { tensor = CastDataAndReturnAsTensor<T>(data); }
catch (ArgumentException) { tensor = CastDataAndReturnAsTensor(Convert.ToSingle(data)); }

Prevention

When it happens

Trigger: Passing a column value whose .NET type is not float, double, or ReadOnlyMemory<char> into the TF runner path — e.g. int, bool, or vector-typed data bound as a scalar tensor input in the TensorFlowTransformer input mapping.

Common situations: IDataView schema declares an int or bool key/number column but the TF model input is fed through the scalar cast path; misconfigured input column mappings after schema changes; using TF inputs expecting int32 where ML.NET stored float.

Related errors


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

Appendix: source

Thrown at src/Microsoft.ML.TensorFlow/TensorflowUtils.cs:515

            else if (typeof(T) == typeof(ulong))
                return new Tensor((ulong)(object)data);
            else if (typeof(T) == typeof(UInt32))
                return new Tensor((UInt32)(object)data);
            else if (typeof(T) == typeof(UInt16))
#pragma warning disable IDE0055
                // Tensorflow.NET v2.7 has no constructor for UInt16 so using the array version
                return new Tensor(new UInt16[]{(UInt16)(object)data});
#pragma warning restore IDE0055
            else if (typeof(T) == typeof(bool))
                return new Tensor((bool)(object)data);
            else if (typeof(T) == typeof(float))
                return new Tensor((float)(object)data);
            else if (typeof(T) == typeof(double))
                return new Tensor((double)(object)data);
            else if (typeof(T) == typeof(ReadOnlyMemory<char>))
                return new Tensor(data.ToString());

            throw new ArgumentException($"Unsupported data type of {typeof(T)} to convert to Tensor.");
        }

        /// <summary>
        /// Use the runner class to easily configure inputs, outputs and targets to be passed to the session runner.
        /// </summary>
        public class Runner : IDisposable
        {
            private readonly TF_Output[] _inputs;
            private readonly TF_Output[] _outputs;
            private readonly IntPtr[] _outputValues;
            private readonly IntPtr[] _inputValues;
            private readonly Tensor[] _inputTensors;
            private readonly IntPtr[] _operations;
            private readonly Session _session;
            private readonly Tensor[] _outputTensors;
            private readonly Status _status;

            internal Runner(Session session, TF_Output[] inputs = null, TF_Output[] outputs = null, IntPtr[] operations = null)

View on GitHub (pinned to 7b76e69cf9)