dotnet/machinelearning · error · ArgumentException

Training data and validation data schemas do not match. Trai

Error message

Training data and validation data schemas do not match. Train data has '{trainData.Schema.Count}' columns,and validation data has '{validationData.Schema.Count}' columns.

What it means

AutoML requires validation data to be schema-compatible with training data. This ArgumentException is thrown when the counts of non-hidden columns differ between trainData and validationData, with both counts embedded in the message.

Source

Thrown at src/Microsoft.ML.AutoML/Utils/UserInputValidationUtil.cs:200

        }

        private static void ValidateValidationData(IDataView trainData, IDataView validationData)
        {
            if (validationData == null)
            {
                return;
            }

            if (DatasetDimensionsUtil.IsDataViewEmpty(validationData))
            {
                throw new ArgumentException("Validation data has 0 rows", nameof(validationData));
            }

            const string schemaMismatchError = "Training data and validation data schemas do not match.";

            if (trainData.Schema.Count(c => !c.IsHidden) != validationData.Schema.Count(c => !c.IsHidden))
            {
                throw new ArgumentException($"{schemaMismatchError} Train data has '{trainData.Schema.Count}' columns," +
                    $"and validation data has '{validationData.Schema.Count}' columns.", nameof(validationData));
            }

            // Validate that every active column in the train data corresponds to an active column in the validation data.
            // (Indirectly, since we asserted above that the train and validation data have the same number of active columns, this also
            // ensures the reverse -- that every active column in the validation data corresponds to an active column in the train data.)
            foreach (var trainCol in trainData.Schema)
            {
                if (trainCol.IsHidden)
                {
                    continue;
                }

                var validCol = validationData.Schema.GetColumnOrNull(trainCol.Name);
                if (validCol == null)
                {
                    throw new ArgumentException($"{schemaMismatchError} Column '{trainCol.Name}' exists in train data, but not in validation data.", nameof(validationData));
                }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Compare the two schemas and align columns before calling Execute (add/remove columns).
  2. Load both files with the same TextLoader.Options so column sets match.
  3. Use InferColumns on the training file and reuse the resulting ColumnInformation/loader settings for validation data.

Example fix

// before
var valData = mlContext.Data.LoadFromTextFile<Row>("val.csv", hasHeader: false); // one column short
// after
var valData = mlContext.Data.LoadFromTextFile<Row>("val.csv", hasHeader: true);
if (valData.Schema.Count(c => !c.IsHidden) != trainData.Schema.Count(c => !c.IsHidden))
    throw new InvalidOperationException("align validation schema before Execute");
Defensive patterns

Strategy: validation

Validate before calling

var t = trainData.Schema.Count(c => !c.IsHidden);
var v = validationData.Schema.Count(c => !c.IsHidden);
if (t != v) throw new InvalidOperationException($"column count mismatch: {t} vs {v}");

Try / catch

try { result = experiment.Execute(trainData, validationData, ...); }
catch (ArgumentException ex) when (ex.Message.Contains("schemas do not match")) { /* re-align schema and retry */ }

Prevention

When it happens

Trigger: Calling Execute with validationData whose schema has a different number of visible columns than trainData — e.g. validation file lacks a column, has extra columns, or was loaded with different settings (header/drop-options).

Common situations: Validation CSV missing a column due to schema drift upstream; one file has header row and the other does not; columns dropped in one loader but not the other.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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