LykosAI/StabilityMatrix · error · InvalidDataException

Failed to deserialize metadata.

Error message

Failed to deserialize metadata.

What it means

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.

Solutions

  1. Inspect the __metadata__ value in the file's header and fix or remove it.
  2. Re-export the model ensuring __metadata__ is an object of string values.
  3. Loosen the deserialization target type (e.g. Dictionary<string, JsonElement>) if non-string metadata is expected.
  4. Treat null result as 'no metadata' instead of throwing, if acceptable.

Example fix

// before
throw new InvalidDataException("Failed to deserialize metadata.");
// after
return new Dictionary<string, string>(); // treat null __metadata__ as empty
Defensive patterns

Strategy: validation

Validate before calling

// Ensure __metadata__ is a string->string map before parsing
using var doc = System.Text.Json.JsonDocument.Parse(headerJson);
if (doc.RootElement.TryGetProperty("__metadata__", out var md) &&
    md.ValueKind != System.Text.Json.JsonValueKind.Object)
    throw new InvalidDataException("__metadata__ is not an object.");

Try / catch

try { var meta = await SafetensorMetadata.ParseAsync(stream); }
catch (InvalidDataException ex) { _logger.LogWarning("Metadata unusable: {Msg}", ex.Message); return null; }

Prevention

When it happens

Trigger: __metadata__ value being JSON null, or a value whose shape cannot bind to Dictionary<string,string> causing the deserializer to return null.

Common situations: Model files whose __metadata__ block was written as null or with non-string values by custom training/export tools.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

                }

                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    // expecting a property name
                    throw new InvalidDataException(
                        $"Invalid metadata JSON, expected property name but got {reader.TokenType}."
                    );
                }

                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 =
    [

View on GitHub (pinned to af93d6ef57)