{"record":{"id":"bfbd9eaf805929b0","repo":"LykosAI/StabilityMatrix","slug":"invalid-metadata-json","errorCode":null,"errorMessage":"Invalid metadata JSON.","messagePattern":"Invalid metadata JSON\\.","errorType":"validation","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Models/SafetensorMetadata.cs","lineNumber":113,"sourceCode":"\n                if (reader.ValueTextEquals(\"__metadata__\"))\n                {\n                    if (JsonSerializer.Deserialize<Dictionary<string, string>>(ref reader) is { } dict)\n                    {\n                        return FromDictionary(dict);\n                    }\n\n                    // got null from Deserialize\n                    throw new InvalidDataException(\"Failed to deserialize metadata.\");\n                }\n                else\n                {\n                    // skip the property value\n                    reader.Skip();\n                }\n            }\n            // should not reach here, json is malformed\n            throw new InvalidDataException(\"Invalid metadata JSON.\");\n        }\n        finally\n        {\n            ArrayPool<byte>.Shared.Return(jsonBytes);\n        }\n    }\n\n    private static readonly HashSet<string> MetadataKeys =\n    [\n        // \"ss_network_module\",\n        // \"modelspec.architecture\",\n        \"ss_tag_frequency\",\n    ];\n\n    internal static SafetensorMetadata FromDictionary(Dictionary<string, string> metadataDict)\n    {\n        // equivalent to the following code, rewitten manually for performance\n        // otherMetadata = metadataDict","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Models/SafetensorMetadata.cs#L95-L131","documentation":"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.","triggerScenarios":"Truncated JSON header that ends before EndObject is reached, jsonLength larger than the actual JSON content so trailing garbage/EOF ends the loop early.","commonSituations":"Cut-off downloads, converter bugs writing a header length longer than the JSON actually present.","solutions":["Re-download or re-export the model file.","Verify the 8-byte length prefix matches the actual JSON header size.","Sanity-check the file with a safetensors inspector tool.","Reject the file earlier by validating that the header JSON closes before the tensor data begins."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"var len = ReadHeaderLength(fileStream);\nif (fileStream.Length < 8 + (long)len)\n    throw new InvalidDataException(\"File truncated: header extends past end of file.\");","typeGuard":null,"tryCatchPattern":"try { var meta = await SafetensorMetadata.ParseAsync(stream); }\ncatch (InvalidDataException ex) { _logger.LogError(ex, \"Safetensors header truncated\"); throw new CorruptModelFileException(path, ex); }","preventionTips":["Confirm download completion (size + hash) before parsing","Check that 8-byte length prefix fits within the file","Re-download truncated files rather than retrying"],"tags":["safetensors","json","truncation"],"backgroundTag":"invalid-json-response","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}