LykosAI/StabilityMatrix · error · ArgumentException

Model file name must contain a valid file name.

Error message

Model file name must contain a valid file name.

What it means

DoCustomImport in ModelImportService throws this ArgumentException when, after splitting the model file name into sanitized path segments, no non-empty segment remains — meaning the provided modelFileName contains no usable file name (only separators, traversal segments, or whitespace).

Solutions

  1. Pass a concrete model file name with an extension (e.g. "model.safetensors") to DoCustomImport
  2. Strip query strings/URL fragments and take the last real path segment before calling
  3. Validate that the file name contains non-whitespace, non-separator characters before importing
  4. For Civitai/URL downloads, derive the file name from the download URL or metadata first

Example fix

// before
await importService.DoCustomImport(uri, "///", localModelFile, modelDbModel);
// after
var fileName = new Uri(uri).Segments[^1].Trim('/', '?');
if (string.IsNullOrWhiteSpace(fileName))
    throw new ArgumentException("Provide a valid model file name");
await importService.DoCustomImport(uri, fileName, localModelFile, modelDbModel);
Defensive patterns

Strategy: validation

Validate before calling

var segments = modelFileName.Split('/', '\\', Path.DirectorySeparatorChar)
    .Select(s => s.Trim()).Where(s => !string.IsNullOrWhiteSpace(s) && s != "." && s != "..").ToArray();
if (segments.Length == 0)
    throw new ArgumentException("Provide a valid model file name");

Type guard

bool IsValidModelFileName(string name) =>
    !string.IsNullOrWhiteSpace(name) &&
    name.Split('/', '\\').Select(s => s.Trim())
        .Any(s => !string.IsNullOrWhiteSpace(s) && s != "." && s != "..");

Try / catch

try
{
    await importService.DoCustomImport(downloadUri, modelFileName, localModelFile, modelDbModel);
}
catch (ArgumentException ex) when (ex.Message.Contains("valid file name"))
{
    // derive name from URL or ask user for a file name
}

Prevention

When it happens

Trigger: Calling DoCustomImport with modelFileName like "", "///", "..", ".", or a string of only invalid path characters; also when a custom pattern strips everything out.

Common situations: Programmatic imports passing an empty name from upstream metadata; URLs whose last path segment is empty or a query fragment; user-supplied names that sanitize to nothing; copy-paste of directory-only paths.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Avalonia/Services/ModelImportService.cs:291

        Uri? previewImageUri = null,
        string? previewImageFileExtension = null,
        ConnectedModelInfo? connectedModelInfo = null,
        Action<TrackedDownload>? configureDownload = null
    )
    {
        // Subfolder support for user-defined patterns such as
        // "{base_model}/{model_name}/{file_name}". Treat every component as relative so
        // rooted or traversal input cannot escape the selected models folder.
        var pathSegments = modelFileName
            .Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
            .Where(segment => segment is not "." and not "..")
            .Select(SanitizePathSegment)
            .Where(segment => !string.IsNullOrWhiteSpace(segment))
            .ToArray();

        if (pathSegments.Length == 0)
        {
            throw new ArgumentException(
                "Model file name must contain a valid file name.",
                nameof(modelFileName)
            );
        }

        modelFileName = pathSegments[^1];
        if (pathSegments.Length > 1)
        {
            downloadFolder = new DirectoryPath(
                [downloadFolder.FullPath, .. pathSegments.Take(pathSegments.Length - 1)]
            );
        }

        // Folders might be missing if user didn't install any packages yet
        downloadFolder.Create();

        // Fix invalid chars in FileName
        var modelBaseFileName = SanitizePathSegment(Path.GetFileNameWithoutExtension(modelFileName));

View on GitHub (pinned to af93d6ef57)