dotnet/machinelearning · error · InvalidOperationException

Cannot set parameter {param.Name} for {obj.GetType()}

Error message

Cannot set parameter {param.Name} for {obj.GetType()}

What it means

UpdateFields reflects over trainer option objects and sets fields/properties from raw parameter values; if reflection-based assignment fails (missing field, type mismatch, null value) it rethrows as InvalidOperationException naming the parameter and target type.

Source

Thrown at src/Microsoft.ML.AutoML/TrainerExtensions/TrainerExtensionUtil.cs:296

                                fi.SetValue(obj, null);
                            else if (fi.FieldType.IsEnum)
                            {
                                // Check if there is an enum option named Auto
                                var enumDict = fi.FieldType.GetEnumValues().Cast<int>()
                                    .ToDictionary(v => Enum.GetName(fi.FieldType, v), v => v);
                                if (enumDict.ContainsKey("Auto"))
                                    fi.SetValue(obj, enumDict["Auto"]);
                            }
                        }
                        else
                            SetValue(fi, (IComparable)dp.Options[optIndex], obj, propType);
                    }
                    else
                        SetValue(fi, param.RawValue, obj, propType);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException($"Cannot set parameter {param.Name} for {obj.GetType()}");
                }
            }
        }

        public static TrainerName GetTrainerName(BinaryClassificationTrainer binaryTrainer)
        {
            switch (binaryTrainer)
            {
                case BinaryClassificationTrainer.FastForest:
                    return TrainerName.FastForestBinary;
                case BinaryClassificationTrainer.FastTree:
                    return TrainerName.FastTreeBinary;
                case BinaryClassificationTrainer.LightGbm:
                    return TrainerName.LightGbmBinary;
                case BinaryClassificationTrainer.LbfgsLogisticRegression:
                    return TrainerName.LbfgsLogisticRegressionBinary;
                case BinaryClassificationTrainer.SdcaLogisticRegression:
                    return TrainerName.SdcaLogisticRegressionBinary;

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check the parameter name matches the exact field/property on the options type.
  2. Ensure RawValue's runtime type matches the option field type (cast/convert before adding to the ParameterSet).
  3. Update Microsoft.ML.AutoML and trainer packages to matching versions.
  4. Catch InvalidOperationException and log obj.GetType() plus param.Name to identify the mismatch.

Example fix

// before
paramSet.Add(new Parameter("NumLeaves", "32")); // string into int field
// after
paramSet.Add(new Parameter("NumLeaves", 32)); // correct type
Defensive patterns

Strategy: validation

Validate before calling

var field = typeof(TOpts).GetField(param.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null) throw new ArgumentException($"Unknown option field {param.Name}");
if (!field.FieldType.IsInstanceOfType(param.RawValue)) throw new ArgumentException($"Type mismatch for {param.Name}");

Try / catch

try { TrainerExtensionUtil.UpdateFields(options, paramSet); }
catch (InvalidOperationException ex) { logger.LogError(ex, "Failed applying parameter to {OptionsType}", options.GetType()); throw; }

Prevention

When it happens

Trigger: CreateOptions/CreateLightGbmOptions building trainer options from a parameter set where a parameter name doesn't match an option field or its value type is incompatible (e.g. a bool parameter applied to an int field).

Common situations: Version drift between AutoML trainer-extension parameter names and the underlying trainer option classes; custom trainer registrations supplying wrong parameter names or raw value types.

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/f40579ba1782026a. Report an issue: GitHub.