LykosAI/StabilityMatrix · error · InvalidDataException

Invalid metadata JSON.

Error message

Invalid metadata JSON.

What it means

This is the fall-through guard after the while(reader.Read()) loop finishes: if the loop exits without hitting EndObject and returning, the header JSON was malformed (e.g. never closed). ParseAsync throws InvalidDataException to signal the header never completed.

Solutions

  1. Re-download or re-export the model file.
  2. Verify the 8-byte length prefix matches the actual JSON header size.
  3. Sanity-check the file with a safetensors inspector tool.
  4. Reject the file earlier by validating that the header JSON closes before the tensor data begins.
Defensive patterns

Strategy: try-catch

Validate before calling

var len = ReadHeaderLength(fileStream);
if (fileStream.Length < 8 + (long)len)
    throw new InvalidDataException("File truncated: header extends past end of file.");

Try / catch

try { var meta = await SafetensorMetadata.ParseAsync(stream); }
catch (InvalidDataException ex) { _logger.LogError(ex, "Safetensors header truncated"); throw new CorruptModelFileException(path, ex); }

Prevention

When it happens

Trigger: Truncated JSON header that ends before EndObject is reached, jsonLength larger than the actual JSON content so trailing garbage/EOF ends the loop early.

Common situations: Cut-off downloads, converter bugs writing a header length longer than the JSON actually present.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Core/Models/SafetensorMetadata.cs:113

                if (reader.ValueTextEquals("__metadata__"))
                {
                    if (JsonSerializer.Deserialize<Dictionary<string, string>>(ref reader) is { } dict)
                    {
                        return FromDictionary(dict);
                    }

                    // got null from Deserialize
                    throw new InvalidDataException("Failed to deserialize metadata.");
                }
                else
                {
                    // skip the property value
                    reader.Skip();
                }
            }
            // should not reach here, json is malformed
            throw new InvalidDataException("Invalid metadata JSON.");
        }
        finally
        {
            ArrayPool<byte>.Shared.Return(jsonBytes);
        }
    }

    private static readonly HashSet<string> MetadataKeys =
    [
        // "ss_network_module",
        // "modelspec.architecture",
        "ss_tag_frequency",
    ];

    internal static SafetensorMetadata FromDictionary(Dictionary<string, string> metadataDict)
    {
        // equivalent to the following code, rewitten manually for performance
        // otherMetadata = metadataDict

View on GitHub (pinned to af93d6ef57)