dotnet/machinelearning · error · ArgumentException

You need to configure a default, not named, model before you

Error message

You need to configure a default, not named, model before you use this method.

What it means

GetPredictionEngine treats an empty/null modelName as 'the default model'. If no default pool was configured via options (only named models were registered), _defaultEnginePool is null and this ArgumentException is thrown.

Source

Thrown at src/Microsoft.Extensions.ML/PredictionEnginePool.cs:107

        {
            if (Volatile.Read(ref _disposed) != 0)
            {
                throw new ObjectDisposedException(nameof(PredictionEnginePool<TData, TPrediction>));
            }

            if (_namedPools.TryGetValue(modelName, out var existingPool))
            {
                return existingPool.Get();
            }

            //This is the case where someone has used string.Empty to get the default model.
            //We can throw all the time, but it seems reasonable that we would just do what
            //they are expecting if they know that an empty string means default.
            if (string.IsNullOrEmpty(modelName))
            {
                if (_defaultEnginePool == null)
                {
                    throw new ArgumentException("You need to configure a default, not named, model before you use this method.");
                }

                return _defaultEnginePool.Get();
            }

            var pool = AddPool(modelName);
            return pool.Get();
        }

        private PoolLoader<TData, TPrediction> AddPool(string modelName)
        {
            //Here we are in the world of named models where the model hasn't been built yet.
            var options = _predictionEngineOptions.Create(modelName);
            var pool = new PoolLoader<TData, TPrediction>(_serviceProvider, options);
            pool = _namedPools.GetOrAdd(modelName, pool);
            return pool;
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Configure the default model in options (set the model name/file in AddPredictionEnginePool<TData,TPrediction>().SetModelFile(...) without a name)
  2. Pass a valid non-empty registered model name
  3. Validate modelName is non-empty before calling

Example fix

// before
services.AddPredictionEnginePool<MData,Pred>().FromFile("modelA", "a.zip");
var eng = pool.GetPredictionEngine(); // throws: no default
// after
services.AddPredictionEnginePool<MData,Pred>().FromFile("modelA", "a.zip");
var eng = pool.GetPredictionEngine("modelA"); // or register a default model
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(modelName)) throw new InvalidOperationException("Configure a default model or pass a registered name");

Type guard

bool HasDefault<TData,TPred>(PredictionEnginePool<TData,TPred> p) { try { p.GetPredictionEngine(); return true; } catch (ArgumentException) { return false; } } // prefer validating config at startup

Try / catch

try { var e = pool.GetPredictionEngine(modelName); } catch (ArgumentException ex) when (ex.Message.Contains("configure a default")) { // fall back to a named model or fix registration
    throw; }

Prevention

When it happens

Trigger: Calling GetPredictionEngine() or GetPredictionEngine("") when registration only used AddPredictionEnginePool<TData,TPrediction>().WithName("x") style named models and no default model/options were supplied.

Common situations: Forgetting to configure ModelName/default in AddPredictionEnginePool options; passing an unset/empty string variable as the model name.

Understand the failure class

Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.

Related errors


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