dotnet/machinelearning · error · NotImplementedException

Not implemented type {typeof(T)}

Error message

Not implemented type {typeof(T)}

What it means

CreateScalarNamedOnnxValue wraps a single scalar value into a NamedOnnxValue/DenseTensor for ONNX Runtime. It only supports the types registered in _onnxTypeMap (float, double, int, long, string/ReadOnlyMemory<char>, etc.); any other T throws NotImplementedException.

Source

Thrown at src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs:537

                    { typeof(UInt32) , InternalDataKind.U4},
                    { typeof(UInt64) , InternalDataKind.U8},
                    { typeof(String) , InternalDataKind.TX},
                    { typeof(Boolean) , InternalDataKind.BL},
                    { typeof(SByte) , InternalDataKind.I1},
                    { typeof(Byte) , InternalDataKind.U1},
                };

        /// <summary>
        /// Creates a NamedOnnxValue from a scalar value.
        /// </summary>
        /// <typeparam name="T">The type of the Tensor contained in the NamedOnnxValue.</typeparam>
        /// <param name="name">The name of the NamedOnnxValue.</param>
        /// <param name="data">The data values of the Tensor.</param>
        /// <returns>NamedOnnxValue</returns>
        public static NamedOnnxValue CreateScalarNamedOnnxValue<T>(string name, T data)
        {
            if (!_onnxTypeMap.Contains(typeof(T)))
                throw new NotImplementedException($"Not implemented type {typeof(T)}");

            if (typeof(T) == typeof(ReadOnlyMemory<char>))
                return NamedOnnxValue.CreateFromTensor<string>(name, new DenseTensor<string>(new string[] { data.ToString() }, new int[] { 1, 1 }));

            return NamedOnnxValue.CreateFromTensor<T>(name, new DenseTensor<T>(new T[] { data }, new int[] { 1, 1 }));
        }

        /// <summary>
        /// Create a NamedOnnxValue from vbuffer span. Checks if the tensor type
        /// is supported by OnnxRuntime prior to execution.
        /// </summary>
        /// <typeparam name="T">The type of the Tensor contained in the NamedOnnxValue.</typeparam>
        /// <param name="name">The name of the NamedOnnxValue.</param>
        /// <param name="data">A span containing the data</param>
        /// <param name="shape">The shape of the Tensor being created.</param>
        /// <returns>NamedOnnxValue</returns>
        public static NamedOnnxValue CreateNamedOnnxValue<T>(string name, ReadOnlySpan<T> data, OnnxShape shape)
        {

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Convert the value to a supported type before calling (e.g. Convert.ToSingle / ToInt64) matching the model input's ONNX type.
  2. Check OnnxUtils._onnxTypeMap for the supported set and cast to one of those types.
  3. If strings are needed, pass ReadOnlyMemory<char> (or string) which has a special branch.
  4. Add a mapping/registration for the type in _onnxTypeMap if you control the library fork.

Example fix

// before
var v = OnnxUtils.CreateScalarNamedOnnxValue(name, myBool);
// after
var v = OnnxUtils.CreateScalarNamedOnnxValue(name, myBool ? 1L : 0L);
Defensive patterns

Strategy: type-guard

Validate before calling

// C#: check T is supported before calling
static bool IsOnnxSupported<T>() =>
    typeof(T) == typeof(float) || typeof(T) == typeof(double) ||
    typeof(T) == typeof(int) || typeof(T) == typeof(long) ||
    typeof(T) == typeof(ReadOnlyMemory<char>);

Type guard

static bool IsSupportedOnnxScalar<T>(T v) => IsOnnxSupported<T>();

Try / catch

try { var v = OnnxUtils.CreateScalarNamedOnnxValue(name, data); }
catch (NotImplementedException) { /* convert to float/long/string and retry */ }

Prevention

When it happens

Trigger: Calling OnnxUtils.CreateScalarNamedOnnxValue<T> with a T not in the type map — e.g. byte, short, bool, decimal, DateTime, or a user type — usually via custom code feeding values into ONNX scoring.

Common situations: Passing a C# bool or Int16 column into an ONNX model expecting int32; custom transform/feeder code using unsupported CLR types; type changed after a refactor so T no longer matches the model's expected input type.

Related errors


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