{"record":{"id":"3fbd1030386cb6e6","repo":"dotnet/machinelearning","slug":"result-must-be-of-type-typeof-trialresult-tmetric","errorCode":null,"errorMessage":"result must be of type {typeof(TrialResult<TMetrics>)}","messagePattern":"result must be of type (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.AutoML/AutoMLExperiment/IMonitor.cs","lineNumber":95,"sourceCode":"            this.RunDetails = new List<TrialResult<TMetrics>>();\n        }\n\n        public event EventHandler<TrialResult<TMetrics>> OnTrialCompleted;\n\n        public List<TrialResult<TMetrics>> RunDetails { get; }\n\n        public TrialResult<TMetrics> BestRun { get; private set; }\n\n        public override void ReportBestTrial(TrialResult result)\n        {\n            base.ReportBestTrial(result);\n            if (result is TrialResult<TMetrics> binaryClassificationResult)\n            {\n                BestRun = binaryClassificationResult;\n            }\n            else\n            {\n                throw new ArgumentException($\"result must be of type {typeof(TrialResult<TMetrics>)}\");\n            }\n        }\n\n        public override void ReportCompletedTrial(TrialResult result)\n        {\n            base.ReportCompletedTrial(result);\n            if (result is TrialResult<TMetrics> metricResult)\n            {\n                RunDetails.Add(metricResult);\n                OnTrialCompleted?.Invoke(this, metricResult);\n            }\n            else\n            {\n                throw new ArgumentException($\"result must be of type {typeof(TrialResult<TMetrics>)}\");\n            }\n        }\n\n        public override void ReportFailTrial(TrialSettings settings, Exception exp)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.AutoML/AutoMLExperiment/IMonitor.cs#L77-L113","documentation":"The generic monitor's ReportBestTrial expects the incoming result to be a TrialResult<TMetrics>; if the runtime type does not match, the pattern-match fails and ArgumentException is thrown. It guards the best-run tracking against results produced by a differently-typed runner.","triggerScenarios":"Calling ReportBestTrial on a TrialHTMonitor<TMetrics> (or subclass) with a base TrialResult or a TrialResult<OtherTMetrics> - typically when the runner and monitor are generic-parameterized inconsistently.","commonSituations":"Mixing monitors/runners built for different metric types (e.g. binary vs regression TrialResult); custom runner code raising best-trial callbacks with non-generic results; refactorings that erased generic type arguments.","solutions":["Ensure the runner paired with this monitor produces TrialResult<TMetrics> with the same TMetrics as the monitor.","Pass the typed result from the generic evaluation path, not a base TrialResult.","Add an is TrialResult<TMetrics> check at the call site before invoking ReportBestTrial.","Fix generic type inference so monitor and runner share the same TMetrics."],"exampleFix":"// before\nTrialResult result = runner.Evaluate(); // base type\nmonitor.ReportBestTrial(result); // ArgumentException\n// after\nif (runner.Evaluate() is TrialResult<BinaryClassificationMetrics> typed)\n{\n    monitor.ReportBestTrial(typed);\n}","handlingStrategy":"type-guard","validationCode":"// before raising the callback:\nif (result is not TrialResult<TMetrics> typed)\n    throw new InvalidOperationException($\"Runner must emit TrialResult<{typeof(TMetrics).Name}> for this monitor\");","typeGuard":"bool IsTypedResult<TMetrics>(TrialResult r) => r is TrialResult<TMetrics>;","tryCatchPattern":"try { monitor.ReportBestTrial(result); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must be of type\")) { log.LogError(ex, \"Runner/monitor generic type mismatch\"); throw; }","preventionTips":["Pair runners and monitors with identical TMetrics generic arguments","Never callback with a base TrialResult into generic monitors","Add compile-time generics constraints linking runner and monitor types"],"tags":["argument-exception","type-mismatch","generics","automl","monitor"],"backgroundTag":"type-mismatch","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"}