{"record":{"id":"a597644f1999111c","repo":"dotnet/machinelearning","slug":"if-provided-nameof-samplingkeycolumnname-must","errorCode":null,"errorMessage":"If provided, {nameof(samplingKeyColumnName)} must be the same as {nameof(groupIdColumnName)} for Ranking Experiments","messagePattern":"If provided, (.+?) must be the same as (.+?) for Ranking Experiments","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.AutoML/Utils/UserInputValidationUtil.cs","lineNumber":65,"sourceCode":"\n        public static void ValidateInferColumnsArgs(string path)\n        {\n            ValidatePath(path);\n        }\n\n        public static void ValidateNumberOfCVFoldsArg(uint numberOfCVFolds)\n        {\n            if (numberOfCVFolds <= 1)\n            {\n                throw new ArgumentException($\"{nameof(numberOfCVFolds)} must be at least 2\", nameof(numberOfCVFolds));\n            }\n        }\n\n        public static void ValidateSamplingKey(string samplingKeyColumnName, string groupIdColumnName, TaskKind task)\n        {\n            if (task == TaskKind.Ranking && samplingKeyColumnName != null && samplingKeyColumnName != groupIdColumnName)\n            {\n                throw new ArgumentException($\"If provided, {nameof(samplingKeyColumnName)} must be the same as {nameof(groupIdColumnName)} for Ranking Experiments\", samplingKeyColumnName);\n            }\n        }\n\n        private static void ValidateTrainData(IDataView trainData, ColumnInformation columnInformation)\n        {\n            if (trainData == null)\n            {\n                throw new ArgumentNullException(nameof(trainData), \"Training data cannot be null\");\n            }\n\n            if (DatasetDimensionsUtil.IsDataViewEmpty(trainData))\n            {\n                throw new ArgumentException(\"Training data has 0 rows\", nameof(trainData));\n            }\n\n            foreach (var column in trainData.Schema)\n            {\n                if (column.Name == DefaultColumnNames.Features && column.Type.GetItemType() != NumberDataViewType.Single)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.AutoML/Utils/UserInputValidationUtil.cs#L47-L83","documentation":"Thrown by UserInputValidationUtil.ValidateSamplingKey when a ranking experiment specifies a samplingKeyColumnName that differs from the groupIdColumnName. In ML.NET AutoML ranking experiments, the sampling key controls stratified sampling and must align with the group identifier so each query group is sampled as a unit. Passing a different column would break ranking group semantics, so it is rejected eagerly.","triggerScenarios":"Calling any AutoML experiment Execute/ExecuteAsync with TaskKind.Ranking while samplingKeyColumnName is non-null and not string-equal to groupIdColumnName.","commonSituations":"Setting a sampling key for variance reduction in binary classification and reusing the same settings in a ranking experiment; copy-pasted experiment configs where samplingKey was tuned independently of the group column; misunderstanding that sampling key and ranking group id must coincide.","solutions":["Set samplingKeyColumnName equal to groupIdColumnName for ranking experiments","Pass null (or omit) samplingKeyColumnName when running a ranking experiment","If the intent was independent sampling, run the experiment as a different TaskKind (e.g. binary classification) where samplingKey may differ"],"exampleFix":"// before\nexperimentSettings.SamplingKeyColumnName = \"UserId\";\nexperimentSettings.GroupIdColumnName = \"QueryId\"; // ranking: mismatch -> ArgumentException\n\n// after\nexperimentSettings.GroupIdColumnName = \"QueryId\";\nexperimentSettings.SamplingKeyColumnName = experimentSettings.GroupIdColumnName; // must match","handlingStrategy":"validation","validationCode":"if (taskKind == TaskKind.Ranking && samplingKey != null && samplingKey != groupId)\n    throw new ArgumentException(\"samplingKeyColumnName must equal groupIdColumnName for ranking\");","typeGuard":null,"tryCatchPattern":"try { experiment.Execute(trainData, colInfo); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"SamplingKey\")) { /* fall back to groupId as sampling key */ }","preventionTips":["Always derive samplingKeyColumnName from groupIdColumnName for ranking tasks","Centralize experiment settings construction in one factory method per TaskKind","Validate ColumnInformation consistency in unit tests"],"tags":["dotnet","mlnet","automl","ranking","argument-validation"],"backgroundTag":"conflicting-config-options","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}