dotnet/machinelearning · error · ArgumentException

unknown option type

Error message

unknown option type

What it means

OptionConverter.Read decides which concrete option type to deserialize by peeking at the JSON (trying SearchSpace and UniformNumericOption shapes). When the JSON matches neither — e.g. it's a categorical/choice option or arbitrary object — the inner deserialization throws and is replaced with ArgumentException 'unknown option type'.

Source

Thrown at src/Microsoft.ML.SearchSpace/Converter/OptionConverter.cs:42

                // try choice option
            }

            try
            {
                return JsonSerializer.Deserialize<ChoiceOption>(ref reader, options);
            }
            catch (Exception)
            {
                // try numeric option
            }

            try
            {
                return JsonSerializer.Deserialize<UniformNumericOption>(ref reader, options);
            }
            catch (Exception)
            {
                throw new ArgumentException("unknown option type");
            }
        }

        public override void Write(Utf8JsonWriter writer, OptionBase value, JsonSerializerOptions options)
        {
            if (value is SearchSpace ss)
            {
                JsonSerializer.Serialize(writer, ss, options);
            }
            else if (value is ChoiceOption choiceOption)
            {
                JsonSerializer.Serialize(writer, choiceOption, options);
            }
            else if (value is UniformNumericOption uniformNumericOption)
            {
                JsonSerializer.Serialize(writer, uniformNumericOption, options);
            }
            else

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Inspect the JSON payload: numeric options should have min/max/log_base/default; a search space is an object of named options.
  2. If the payload is a categorical option, deserialize with the appropriate converter or store type information alongside the JSON.
  3. Upgrade/align ML.NET versions on both writer and reader so the JSON shapes match.
  4. Fix malformed JSON so it matches one of the supported option schemas.

Example fix

// before: choice option JSON fed to OptionBase deserialization
{ "choices": ["a", "b"] }  // -> ArgumentException: unknown option type
// after: deserialize as its concrete type
JsonSerializer.Deserialize<ChoiceOption>(json, options);
Defensive patterns

Strategy: validation

Validate before calling

// C#: peek at JSON to confirm it's a supported option shape before deserializing
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
bool isSearchSpace = root.ValueKind == JsonValueKind.Object && !root.TryGetProperty("choices", out _);
bool isNumeric = root.TryGetProperty("min", out _) && root.TryGetProperty("max", out _);
if (!isSearchSpace && !isNumeric)
    throw new ArgumentException("JSON is neither a search space nor a numeric option");

Type guard

static bool IsNumericOptionJson(JsonElement e) =>
    e.ValueKind == JsonValueKind.Object && e.TryGetProperty("min", out _) && e.TryGetProperty("max", out _);

Try / catch

try { opt = JsonSerializer.Deserialize<OptionBase>(json, options); }
catch (ArgumentException ex) when (ex.Message == "unknown option type")
{
    // deserialize with the concrete option type (e.g. ChoiceOption) instead
}

Prevention

When it happens

Trigger: Deserializing JSON into OptionBase where the payload is a non-numeric option (NestOption/ChoiceOption/Schema_ or custom option) or malformed JSON, so neither the SearchSpace nor UniformNumericOption Deserialize branch succeeds.

Common situations: Round-tripping search spaces containing categorical (choice) options through a serializer that lost type info; JSON written by an older/newer version with different shape; hand-edited config JSON that doesn't match either expected schema.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


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