dotnet/machinelearning · error · ArgumentException

string.Format(Strings.MismatchedValueType, typeof(VBuffer<T>

Error message

string.Format(Strings.MismatchedValueType, typeof(VBuffer<T>))

What it means

VBufferDataFrameColumn<T>.SetValue only accepts values that are exactly VBuffer<T>; anything else (including null or a plain T) throws ArgumentException formatted from Strings.MismatchedValueType, with 'value' as the paramName. The column's storage is buffer-based and cannot coerce arbitrary objects into VBuffer<T>.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumns/VBufferDataFrameColumn.cs:132

                bufferIndex++;
                bufferOffset = 0;
            }
            return ret;
        }

        protected override void SetValue(long rowIndex, object value)
        {
            if (value == null)
            {
                throw new NotSupportedException("Null values are not supported by VBufferDataFrameColumn");
            }
            else if (value is VBuffer<T> vbuffer)
            {
                SetTypedValue(rowIndex, vbuffer);
            }
            else
            {
                throw new ArgumentException(string.Format(Strings.MismatchedValueType, typeof(VBuffer<T>)), nameof(value));
            }
        }

        protected void SetTypedValue(long rowIndex, VBuffer<T> value)
        {
            int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);
            _vBuffers[bufferIndex][(int)(rowIndex % MaxCapacity)] = value;
        }

        public new VBuffer<T> this[long rowIndex]
        {
            get => GetTypedValue(rowIndex);
            set => SetTypedValue(rowIndex, value);
        }

        /// <summary>
        /// Returns an enumerator that iterates through the VBuffer values in this column.
        /// </summary>

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Wrap the value in a VBuffer<T> before calling SetValue (new VBuffer<T>(length, values) or VBuffer.Editor).
  2. Verify the runtime type with 'value is VBuffer<T> v' before the call and handle the else branch.
  3. Ensure the buffer's element type matches the column's T; a VBuffer<U> with U != T fails the pattern match.
  4. Convert dense data with VBuffer.CreateDense(length, values).

Example fix

// before
column.SetValue(rowIndex, myArray); // myArray is T[]
// after
if (myArray is VBuffer<T> v)
    column.SetValue(rowIndex, v);
else
    column.SetValue(rowIndex, VBuffer.CreateDense(myArray.Length, myArray));
Defensive patterns

Strategy: type-guard

Validate before calling

bool ok = value is VBuffer<T>;
if (!ok) throw new ArgumentException($"Expected VBuffer<{typeof(T).Name}>, got {value?.GetType().Name ?? "null"}", nameof(value));

Type guard

static bool IsVBuffer<T>(object value) => value is VBuffer<T>;

Try / catch

try { column.SetValue(rowIndex, value); }
catch (ArgumentException ex) when (ex.ParamName == "value")
{ /* value was not VBuffer<T>: convert or log */ }

Prevention

When it happens

Trigger: Calling SetValue(rowIndex, obj) (directly or via DataFrame indexing) on a VBufferDataFrameColumn<T> with a value that is not an instance of VBuffer<T> — e.g. a plain T[], a scalar T, or a null boxed in object.

Common situations: Populating columns via reflection or object arrays where element types degrade to object; mixing T and VBuffer<T> columns; passing raw values read from an IDataView without type checks.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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