LykosAI/StabilityMatrix · error · ArgumentException

Invalid package specifier

Error message

Invalid package specifier: {value}

What it means

PipPackageSpecifier.TryParse validates a pip package specifier (e.g. 'numpy==1.26.0', 'torch>=2.0') against a regex. When the string does not match the expected specifier grammar, it either returns false or throws ArgumentException with throwOnFailure=true. This guards callers from passing malformed package names into pip operations.

Solutions

  1. Fix the package specifier string to a valid pip format like 'name', 'name==1.2.3', or 'name>=1.0,<2'
  2. Trim whitespace and remove stray quotes or characters before parsing
  3. Use the non-throwing TryParse and handle the false return instead of the throwing overload
  4. Log the offending value to identify the source of the malformed input

Example fix

// before
var spec = PipPackageSpecifier.Parse("torch >=2, <2.4 !!");
// after
var spec = PipPackageSpecifier.Parse("torch>=2.0,<2.4");
Defensive patterns

Strategy: validation

Validate before calling

var isValid = !string.IsNullOrWhiteSpace(value) && System.Text.RegularExpressions.Regex.IsMatch(value, "^[A-Za-z0-9_.-]+(==|>=|<=|>|<|~=)?[A-Za-z0-9_.!*]*$");
if (!isValid) throw new ArgumentException($"Invalid pip package specifier: {value}");

Type guard

bool IsValidPipSpecifier(string? value) =>
    !string.IsNullOrWhiteSpace(value) && PipPackageSpecifier.TryParse(value, out _);

Try / catch

try
{
    var spec = PipPackageSpecifier.Parse(value);
}
catch (ArgumentException ex)
{
    logger.LogWarning(ex, "Bad package specifier: {Value}", value);
    // surface a validation message to the user
}

Prevention

When it happens

Trigger: Calling TryParse(value, ...) or the throwing overload with a string that fails PackageSpecifierRegex(), e.g. empty string, whitespace, illegal characters like 'numpy 1.0', or a bare '==' without a version.

Common situations: User-typed package names from a UI or config file containing typos, spaces, or extra characters; package strings copied with stray quotes; localized or URL-encoded inputs.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/65f689efdc0184ff. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Python/PipPackageSpecifier.cs:47

    }

    public static bool TryParse(string value, [NotNullWhen(true)] out PipPackageSpecifier? packageSpecifier)
    {
        return TryParse(value, false, out packageSpecifier);
    }

    private static bool TryParse(
        string value,
        bool throwOnFailure,
        [NotNullWhen(true)] out PipPackageSpecifier? packageSpecifier
    )
    {
        var match = PackageSpecifierRegex().Match(value);
        if (!match.Success)
        {
            if (throwOnFailure)
            {
                throw new ArgumentException($"Invalid package specifier: {value}");
            }

            packageSpecifier = null;
            return false;
        }

        packageSpecifier = new PipPackageSpecifier
        {
            Name = match.Groups["package_name"].Value,
            Constraint = match.Groups["version_constraint"].Value,
            Version = match.Groups["version"].Value
        };

        return true;
    }

    /// <inheritdoc />
    public override string ToString()

View on GitHub (pinned to af93d6ef57)