{"record":{"id":"2f523f61ac0e7906","repo":"dotnet/machinelearning","slug":"unsupported-data-type-of-typeof-t-to-convert-to","errorCode":null,"errorMessage":"Unsupported data type of {typeof(T)} to convert to Tensor.","messagePattern":"Unsupported data type of (.+?) to convert to Tensor\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.TensorFlow/TensorflowUtils.cs","lineNumber":515,"sourceCode":"            else if (typeof(T) == typeof(ulong))\n                return new Tensor((ulong)(object)data);\n            else if (typeof(T) == typeof(UInt32))\n                return new Tensor((UInt32)(object)data);\n            else if (typeof(T) == typeof(UInt16))\n#pragma warning disable IDE0055\n                // Tensorflow.NET v2.7 has no constructor for UInt16 so using the array version\n                return new Tensor(new UInt16[]{(UInt16)(object)data});\n#pragma warning restore IDE0055\n            else if (typeof(T) == typeof(bool))\n                return new Tensor((bool)(object)data);\n            else if (typeof(T) == typeof(float))\n                return new Tensor((float)(object)data);\n            else if (typeof(T) == typeof(double))\n                return new Tensor((double)(object)data);\n            else if (typeof(T) == typeof(ReadOnlyMemory<char>))\n                return new Tensor(data.ToString());\n\n            throw new ArgumentException($\"Unsupported data type of {typeof(T)} to convert to Tensor.\");\n        }\n\n        /// <summary>\n        /// Use the runner class to easily configure inputs, outputs and targets to be passed to the session runner.\n        /// </summary>\n        public class Runner : IDisposable\n        {\n            private readonly TF_Output[] _inputs;\n            private readonly TF_Output[] _outputs;\n            private readonly IntPtr[] _outputValues;\n            private readonly IntPtr[] _inputValues;\n            private readonly Tensor[] _inputTensors;\n            private readonly IntPtr[] _operations;\n            private readonly Session _session;\n            private readonly Tensor[] _outputTensors;\n            private readonly Status _status;\n\n            internal Runner(Session session, TF_Output[] inputs = null, TF_Output[] outputs = null, IntPtr[] operations = null)","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs#L497-L533","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Map the input column to float, double, or text types that CastData supports (e.g. ChangeColumnType/Convert to float).","Add an explicit cast in the pipeline (ColumnCopying/TypeConverting estimator) so only supported types reach the TF runner.","Extend CastDataAndReturnAsTensor to handle the missing type (e.g. int -> new Tensor((int)(object)data)) in a custom fork.","Validate the IDataView schema against the model's expected input dtypes before calling Fit/Transform."],"exampleFix":"// before\nint value = 3;\nCastDataAndReturnAsTensor<int>(value); // ArgumentException\n// after\nfloat value = 3f;\nCastDataAndReturnAsTensor<float>(value);","handlingStrategy":"validation","validationCode":"bool ok = data is float || data is double || data is ReadOnlyMemory<char>;\nif (!ok) throw new InvalidOperationException($\"Column type {typeof(T)} unsupported for TF scalar input\");","typeGuard":"bool IsCastable<T>(T v) => v is float || v is double || (v is ReadOnlyMemory<char>);","tryCatchPattern":"try { tensor = CastDataAndReturnAsTensor<T>(data); }\ncatch (ArgumentException) { tensor = CastDataAndReturnAsTensor(Convert.ToSingle(data)); }","preventionTips":["Keep TF input columns typed float, double, or ReadOnlyMemory<char> in the IDataView","Insert type-converting estimators before the TensorFlow estimator","Validate schema against model placeholder types before Fit"],"tags":["tensorflow","type-conversion","unsupported-type","scalar"],"backgroundTag":"unsupported-dtype","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}