{"record":{"id":"1af58aaf4f1a1d17","repo":"LykosAI/StabilityMatrix","slug":"failed-to-deserialize-metadata","errorCode":null,"errorMessage":"Failed to deserialize metadata.","messagePattern":"Failed to deserialize metadata\\.","errorType":"validation","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Models/SafetensorMetadata.cs","lineNumber":104,"sourceCode":"                }\n\n                if (reader.TokenType != JsonTokenType.PropertyName)\n                {\n                    // expecting a property name\n                    throw new InvalidDataException(\n                        $\"Invalid metadata JSON, expected property name but got {reader.TokenType}.\"\n                    );\n                }\n\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    [","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Models/SafetensorMetadata.cs#L86-L122","documentation":"When the header contains the '__metadata__' property, its value is deserialized into Dictionary<string,string>. If JsonSerializer.Deserialize returns null, ParseAsync throws InvalidDataException, since __metadata__ must be a non-null string-to-string map.","triggerScenarios":"__metadata__ value being JSON null, or a value whose shape cannot bind to Dictionary<string,string> causing the deserializer to return null.","commonSituations":"Model files whose __metadata__ block was written as null or with non-string values by custom training/export tools.","solutions":["Inspect the __metadata__ value in the file's header and fix or remove it.","Re-export the model ensuring __metadata__ is an object of string values.","Loosen the deserialization target type (e.g. Dictionary<string, JsonElement>) if non-string metadata is expected.","Treat null result as 'no metadata' instead of throwing, if acceptable."],"exampleFix":"// before\nthrow new InvalidDataException(\"Failed to deserialize metadata.\");\n// after\nreturn new Dictionary<string, string>(); // treat null __metadata__ as empty","handlingStrategy":"validation","validationCode":"// Ensure __metadata__ is a string->string map before parsing\nusing var doc = System.Text.Json.JsonDocument.Parse(headerJson);\nif (doc.RootElement.TryGetProperty(\"__metadata__\", out var md) &&\n    md.ValueKind != System.Text.Json.JsonValueKind.Object)\n    throw new InvalidDataException(\"__metadata__ is not an object.\");","typeGuard":null,"tryCatchPattern":"try { var meta = await SafetensorMetadata.ParseAsync(stream); }\ncatch (InvalidDataException ex) { _logger.LogWarning(\"Metadata unusable: {Msg}\", ex.Message); return null; }","preventionTips":["Inspect __metadata__ with a safetensors inspector before importing","Prefer files exported with standard tooling (e.g. official safetensors library)","Treat null/unshaped metadata as empty rather than fatal where possible"],"tags":["safetensors","json","deserialization"],"backgroundTag":"json-unmarshal-failed","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"}