dotnet/machinelearning · error · InvalidOperationException

Invalid call to 'GetValue'

Error message

Invalid call to 'GetValue'

What it means

Annotations.GetValue<TValue>(kind,...) looks up the annotation column by its kind string on the parent schema; if no column with that name exists, it throws InvalidOperationException instead of returning null. It signals that you requested an annotation kind this column's annotations simply do not carry.

Solutions

  1. Call Schema.GetColumnOrNull(kind) (or the TryGet pattern) yourself and skip the annotation when absent
  2. Guard with column.Type judgment: only ask for SlotNames on vector types and KeyValues on key types
  3. If writing a transform, copy annotations selectively with Annotations.Add(source, selector) instead of assuming kinds exist

Example fix

// before
annotations.GetValue<VBuffer<ReadOnlyMemory<char>>>(AnnotationUtils.Kinds.SlotNames, ref slotNames);
// after
if (Schema.GetColumnOrNull(AnnotationUtils.Kinds.SlotNames) != null)
    annotations.GetValue<VBuffer<ReadOnlyMemory<char>>>(AnnotationUtils.Kinds.SlotNames, ref slotNames);
Defensive patterns

Strategy: validation

Validate before calling

bool hasKind = Schema.GetColumnOrNull(kind) != null;
if (!hasKind) return; // or substitute a default value

Type guard

bool HasAnnotation(DataViewSchema schema, string kind) =>
    schema.GetColumnOrNull(kind) != null;

Try / catch

try { annotations.GetValue<TValue>(kind, ref value); }
catch (InvalidOperationException)
{
    value = default; // annotation kind not present on this column
}

Prevention

When it happens

Trigger: Requesting AnnotationUtils.Kinds.SlotNames on a scalar (non-vector) column; requesting KeyValues on a column that is not a key type; reading a custom annotation kind that the producing transform never added; querying annotations through the Builder copy path (GetAnnotationInfo) on a schema missing that kind.

Common situations: Assuming every vector column has slot names; copying annotations in a custom transform without checking the source actually contains the kind; renaming columns so the annotation lookup by kind fails.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.ML.DataView/DataViewSchema.cs:241

                if (column.Index >= _getters.Length)
                    throw new ArgumentException(nameof(column));
                var typedGetter = _getters[column.Index] as ValueGetter<TValue>;
                if (typedGetter == null)
                {
                    Debug.Assert(_getters[column.Index] != null);
                    throw new InvalidOperationException($"Invalid call to '{nameof(GetGetter)}'");
                }
                return typedGetter;
            }

            /// <summary>
            /// Get the value of an annotation, by annotation kind (aka column name).
            /// </summary>
            public void GetValue<TValue>(string kind, ref TValue value)
            {
                var column = Schema.GetColumnOrNull(kind);
                if (column == null)
                    throw new InvalidOperationException($"Invalid call to '{nameof(GetValue)}'");
                GetGetter<TValue>(column.Value)(ref value);
            }

            public override string ToString() => string.Join(", ", Schema.Select(x => x.Name));

            internal Delegate GetGetterInternal(int index)
            {
                Debug.Assert(0 <= index && index < Schema.Count);
                return _getters[index];
            }

            /// <summary>
            /// Class containing operations to build an <see cref="Annotations"/>.
            /// </summary>
            public sealed class Builder
            {
                private readonly List<(string Name, DataViewType Type, Delegate Getter, Annotations Annotations)> _items;

View on GitHub (pinned to 7b76e69cf9)