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
- Check the parameter name matches the exact field/property on the options type.
- Ensure RawValue's runtime type matches the option field type (cast/convert before adding to the ParameterSet).
- Update Microsoft.ML.AutoML and trainer packages to matching versions.
- 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
- Match parameter names exactly to option fields
- Store RawValues in their declared types
- Keep AutoML and trainer package versions in sync
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
- columns
- Type IPredictionTransformer not implemented by provided type
- {nameof(ChannelMessageKind)}.{e.Kind} is not yet implemented
- Training failed with the exception: {_history.Last().Excepti
- Metric {typeof(TMetrics)} not implemented
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/f40579ba1782026a.
Report an issue: GitHub.