LykosAI/StabilityMatrix · error · ArgumentException

Invalid version format

Error message

Invalid version format: {versionString}

What it means

PyVersion.Parse splits the version string on '.' and throws ArgumentException when it does not have 2–3 dot-separated parts. This is the strict parser used by TryParse, so it signals a malformed Python version string.

Solutions

  1. Pass a 'major.minor' or 'major.minor.micro' string, e.g. "3.11" or "3.11.9".
  2. Use PyVersion.TryParse when input may be malformed.
  3. Strip non-numeric prefixes/suffixes (e.g. 'Python ', '-rc1') before parsing.
  4. Normalize short forms like '3' to '3.0' before calling Parse.

Example fix

// before
var v = PyVersion.Parse(versionString); // throws on "3"
// after
if (!PyVersion.TryParse(versionString, out var v)) { v = new PyVersion(3, 11, 0); }
Defensive patterns

Strategy: validation

Validate before calling

bool IsPyVersionString(string s) =>
    s.Split('.').Length is >= 2 and <= 3 &&
    s.Split('.').All(p => p.All(char.IsDigit) && p.Length > 0);

Type guard

bool TryNormalizeVersion(string s, out string normalized) {
    normalized = "";
    var parts = s.Split('.');
    if (parts.Length is < 2 or > 3) return false;
    normalized = string.Join('.', parts);
    return true;
}

Try / catch

try { version = PyVersion.Parse(s); }
catch (ArgumentException) { version = PyVersion.Default; }

Prevention

When it happens

Trigger: PyVersion.Parse("3") (only 1 part) or PyVersion.Parse("3.11.1.0") (4 parts) — parts.Length < 2 or > 3.

Common situations: Parsing raw python executable output like 'Python 3.10.9' without trimming the prefix (resulting in 3 parts containing words, caught later); config entries like '3' for major-only; build strings like '3.11.9-rc1' handled elsewhere.

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/8b3217e500b6f2ba. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Python/PyVersion.cs:44

    /// <summary>
    /// Creates a new PyVersion
    /// </summary>
    public PyVersion(int major, int minor, int micro)
    {
        Major = major;
        Minor = minor;
        Micro = micro;
    }

    /// <summary>
    /// Parses a version string in the format "major.minor.micro"
    /// </summary>
    public static PyVersion Parse(string versionString)
    {
        var parts = versionString.Split('.');
        if (parts.Length is < 2 or > 3)
        {
            throw new ArgumentException($"Invalid version format: {versionString}", nameof(versionString));
        }

        if (!int.TryParse(parts[0], out var major) || !int.TryParse(parts[1], out var minor))
        {
            throw new ArgumentException($"Invalid version format: {versionString}", nameof(versionString));
        }

        var micro = 0;
        if (parts.Length <= 2)
            return new PyVersion(major, minor, micro);

        if (!int.TryParse(parts[2], out micro))
        {
            throw new ArgumentException($"Invalid version format: {versionString}", nameof(versionString));
        }

        return new PyVersion(major, minor, micro);
    }

View on GitHub (pinned to af93d6ef57)