LykosAI/StabilityMatrix · error · InvalidDataException

Invalid metadata JSON, expected property name but got

Error message

Invalid metadata JSON, expected property name but got {reader.TokenType}.

What it means

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.

Solutions

  1. Validate the header JSON with a standard parser to find the malformed region.
  2. Re-export the file with a compliant safetensors writer.
  3. Update parsing to skip unexpected token types instead of throwing, if tolerable.
  4. Inspect reader.TokenType in the message to identify what the stray token is.
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-validate header JSON structure with System.Text.Json
var json = System.Text.Encoding.UTF8.GetString(headerBytes, 8, headerBytes.Length - 8);
using var doc = System.Text.Json.JsonDocument.Parse(json);
if (doc.RootElement.ValueKind != System.Text.Json.JsonValueKind.Object)
    throw new InvalidDataException("Safetensors header is not a JSON object.");

Try / catch

try { var meta = await SafetensorMetadata.ParseAsync(stream); }
catch (InvalidDataException ex) { _logger.LogWarning(ex, "Skipping nonstandard metadata"); return SafetensorMetadata.Empty; }

Prevention

When it happens

Trigger: Header JSON containing unexpected tokens (e.g. bare values, nested arrays at top level) after skipping entries — typically malformed or nonstandard safetensors headers.

Common situations: Files produced by nonstandard exporters, corrupted headers, hand-edited metadata blocks.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

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

                // expecting a JSON object
                throw new InvalidDataException("JSON does not start with '{'.");
            }

            while (reader.Read())
            {
                // for each property in the object
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    // end of the object, no "__metadata__" found
                    // return true to indicate that we successfully read the JSON
                    // but it does not contain metadata
                    return null;
                }

                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();

View on GitHub (pinned to af93d6ef57)