dotnet/machinelearning · error · ArgumentOutOfRangeException

Label column index ({args.LabelColumnIndex}) is >= than # of

Error message

Label column index ({args.LabelColumnIndex}) is >= than # of inferred columns ({cols.Count()}).

What it means

During column type inference, if the label is specified by zero-based index, ColumnTypeInference validates the index against the number of columns actually inferred from the file. An index greater than or equal to cols.Count() means the label points past the end of the parsed schema, so it throws ArgumentOutOfRangeException. Typically the file parsed into fewer columns than expected.

Source

Thrown at src/Microsoft.ML.AutoML/ColumnInference/ColumnTypeInference.cs:387

            return InferenceResult.Success(outCols, args.HasHeader, cols.Select(col => col.RawData).ToArray());
        }

        private static string SuggestName(IntermediateColumn column, bool hasHeader)
        {
            var header = column.RawData[0].ToString();
            return (hasHeader && !string.IsNullOrWhiteSpace(header)) ? header : string.Format("col{0}", column.ColumnId);
        }

        private static IntermediateColumn GetAndValidateLabelColumn(Arguments args, IntermediateColumn[] cols)
        {
            IntermediateColumn labelColumn = null;
            if (args.LabelColumnIndex != null)
            {
                // if label column index > inferred # of columns, throw error
                if (args.LabelColumnIndex >= cols.Count())
                {
                    throw new ArgumentOutOfRangeException(nameof(args.LabelColumnIndex), $"Label column index ({args.LabelColumnIndex}) is >= than # of inferred columns ({cols.Count()}).");
                }

                labelColumn = cols[args.LabelColumnIndex.Value];
            }
            else
            {
                labelColumn = cols.FirstOrDefault(c => c.Name == args.Label);
                if (labelColumn == null)
                {
                    throw new ArgumentException($"Specified label column '{args.Label}' was not found.");
                }
            }

            return labelColumn;
        }

        public static TextLoader.Column[] GenerateLoaderColumns(Column[] columns)
        {

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Subtract 1 if you counted columns starting at 1 — the index is zero-based
  2. Check how many columns were actually inferred (fix delimiter/quoting if rows collapse into fewer columns)
  3. Prefer specifying the label by column NAME instead of index
  4. Validate the index against the parsed column count before running inference

Example fix

// before
args.LabelColumnIndex = 20; // assuming 21 columns
// after
args.LabelColumnIndex = cols.Count() - 1; // last column, zero-based, validated at runtime
Defensive patterns

Strategy: validation

Validate before calling

if (args.LabelColumnIndex >= parsedColumnCount)
    throw new ArgumentException("LabelColumnIndex out of range");

Try / catch

try { var res = ColumnTypeInference.InferTypes(args); }
catch (ArgumentOutOfRangeException) { /* recompute index from actual column count */ }

Prevention

When it happens

Trigger: Setting LabelColumnIndex (e.g. in ColumnInferenceArguments or via settings) to a value >= the parsed column count; a file whose rows parsed into fewer columns than assumed (delimiter mismatch); one-based vs zero-based index confusion.

Common situations: Assuming a 20-column CSV when quoting problems collapse rows into fewer columns; hard-coded label index that breaks when the file gains/loses a column; off-by-one from using a 1-based column number in a 0-based API.

Related errors


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