dotnet/machinelearning · error · ArgumentException

Type IPredictionTransformer not implemented by provided type

Error message

Type IPredictionTransformer not implemented by provided type, {type}

What it means

GetImplementedIPredictionTransformer reflects over a type looking for an implementation of the generic IPredictionTransformer<> interface; if no closed generic interface is found it throws ArgumentException naming the offending type. PermutationFeatureImportance only knows how to work with prediction transformers, so other transformer kinds are rejected.

Source

Thrown at src/Microsoft.ML.Transforms/PermutationFeatureImportanceExtensions.cs:734

                    name = $"Slot {i}";
                }
                output.Add(name, permutationFeatureImportance[i]);
            }

            return output.ToImmutableDictionary();
        }

        private static Type GetImplementedIPredictionTransformer(Type type)
        {
            foreach (Type iType in type.GetInterfaces())
            {
                if (iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IPredictionTransformer<>))
                {
                    return iType;
                }
            }

            throw new ArgumentException($"Type IPredictionTransformer not implemented by provided type, {type}", nameof(type));
        }

        #endregion
    }
}

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Pass the trained prediction transformer from the model (e.g., the result of `transformer.Model` or the fitted pipeline's final prediction transformer), not an arbitrary ITransformer.
  2. Guard before calling: reflect or check `transformer.GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPredictionTransformer<>))`.
  3. If it's a custom transformer, implement IPredictionTransformer<TData> on it before using it with PFI.
  4. Wrap the PFI call in try-catch on ArgumentException to report which type was rejected.

Example fix

// before
pfi = mlContext.BinaryClassification.PermutationFeatureImportance(model, data, ...); // model is a raw ITransformer

// after
var predModel = ((ISingleFeaturePredictionTransformer<object>)model); // ensure it's a prediction transformer
pfi = mlContext.BinaryClassification.PermutationFeatureImportance(predModel, data, ...);
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsPredictionTransformer(ITransformer t) =>
    t.GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPredictionTransformer<>));

Type guard

bool IsPredictionTransformer(ITransformer t) =>
    t.GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPredictionTransformer<>));

Try / catch

try { var results = mlContext.BinaryClassification.PermutationFeatureImportance(model, data, labelColumnName: "Label"); }
catch (ArgumentException ex) { log.LogError(ex, "Transformer does not implement IPredictionTransformer"); throw new UnsupportedModelException(...); }

Prevention

When it happens

Trigger: Calling PermutationFeatureImportance APIs (e.g., PermutationFeatureImportance<TMetrics> over multiclass/binary/ranking models) with a model/transformer type that does not implement IPredictionTransformer<TData> — e.g., passing a generic ITransformer like a ColumnCopyingTransformer's output or a custom transformer implementation.

Common situations: Feeding the output of a non-prediction transform stage into PFI instead of the trained predictor, implementing a custom ITransformer that forgot to implement IPredictionTransformer<TData>, or using a legacy/community transformer built against a different ML.NET interface version.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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