{"record":{"id":"ec7b8e02fccc6b38","repo":"LykosAI/StabilityMatrix","slug":"invalid-metadata-json-expected-property-name-but-got-reader","errorCode":null,"errorMessage":"Invalid metadata JSON, expected property name but got {reader.TokenType}.","messagePattern":"Invalid metadata JSON, expected property name but got (.+?)\\.","errorType":"validation","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Models/SafetensorMetadata.cs","lineNumber":91,"sourceCode":"                // expecting a JSON object\n                throw new InvalidDataException(\"JSON does not start with '{'.\");\n            }\n\n            while (reader.Read())\n            {\n                // for each property in the object\n                if (reader.TokenType == JsonTokenType.EndObject)\n                {\n                    // end of the object, no \"__metadata__\" found\n                    // return true to indicate that we successfully read the JSON\n                    // but it does not contain metadata\n                    return null;\n                }\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();","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Models/SafetensorMetadata.cs#L73-L109","documentation":"While iterating the JSON header, every top-level entry must be a property (name/value pair). This throw indicates the reader encountered a token other than PropertyName where an object key was expected, meaning the header JSON structure deviates from the safetensors metadata schema.","triggerScenarios":"Header JSON containing unexpected tokens (e.g. bare values, nested arrays at top level) after skipping entries — typically malformed or nonstandard safetensors headers.","commonSituations":"Files produced by nonstandard exporters, corrupted headers, hand-edited metadata blocks.","solutions":["Validate the header JSON with a standard parser to find the malformed region.","Re-export the file with a compliant safetensors writer.","Update parsing to skip unexpected token types instead of throwing, if tolerable.","Inspect reader.TokenType in the message to identify what the stray token is."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-validate header JSON structure with System.Text.Json\nvar json = System.Text.Encoding.UTF8.GetString(headerBytes, 8, headerBytes.Length - 8);\nusing var doc = System.Text.Json.JsonDocument.Parse(json);\nif (doc.RootElement.ValueKind != System.Text.Json.JsonValueKind.Object)\n    throw new InvalidDataException(\"Safetensors header is not a JSON object.\");","typeGuard":null,"tryCatchPattern":"try { var meta = await SafetensorMetadata.ParseAsync(stream); }\ncatch (InvalidDataException ex) { _logger.LogWarning(ex, \"Skipping nonstandard metadata\"); return SafetensorMetadata.Empty; }","preventionTips":["Reject model files from untrusted converters","Validate header JSON schema before deep parsing","Keep converter tooling up to date"],"tags":["safetensors","json","schema"],"backgroundTag":"schema-validation-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"}