LykosAI/StabilityMatrix · error · InvalidDataException

Invalid RIFF header

Error message

Invalid RIFF header

What it means

WebpReader.ReadHeader parses the first bytes of a WEBP container. It expects a 'RIFF' magic signature and throws InvalidDataException when the first 4 bytes differ, meaning the stream is not a RIFF container and therefore cannot be a WEBP image.

Solutions

  1. Validate the file starts with 'RIFF'....'WEBP' before using WebpReader
  2. Ensure you are reading the raw file bytes from offset 0
  3. Treat the file as non-animated or skip it when the header is invalid
  4. Re-obtain the file if it is truncated/corrupt

Example fix

// before
using var reader = new WebpReader(stream);
var animated = reader.GetIsAnimatedFlag();
// after
Span<byte> magic = stackalloc byte[4];
stream.ReadExactly(magic);
stream.Position = 0;
if (!magic.SequenceEqual("RIFF"u8)) return false;
using var reader = new WebpReader(stream);
var animated = reader.GetIsAnimatedFlag();
Defensive patterns

Strategy: validation

Validate before calling

byte[] head = new byte[4];
using (var fs = File.OpenRead(path)) fs.ReadExactly(head);
bool isRiff = head.AsSpan().SequenceEqual("RIFF"u8);

Type guard

static bool LooksLikeRiff(Stream s) { var b = new byte[4]; long pos = s.Position; s.ReadExactly(b); s.Position = pos; return b.AsSpan().SequenceEqual("RIFF"u8); }

Try / catch

try { animated = new WebpReader(stream).GetIsAnimatedFlag(); }
catch (InvalidDataException) { animated = false; /* not a WEBP/RIFF file */ }

Prevention

When it happens

Trigger: Calling GetIsAnimatedFlag on a file/stream whose first bytes are not 'RIFF' — a PNG/JPEG/GIF misnamed as .webp, a truncated or zero-byte file, or reading from an offset that skips the header.

Common situations: Inspecting user-imported images with wrong extensions; metadata scrapers reading partially downloaded files; passing an already-decompressed bitmap stream instead of the raw file.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Core/Helper/Webp/WebpReader.cs:30

        while (BaseStream.Position < headerFileSize)
        {
            if (ReadVoidChunk() is "ANMF" or "ANIM")
            {
                return true;
            }
        }

        return false;
    }

    private void ReadHeader()
    {
        // RIFF
        var riff = ReadBytes(4);
        if (!riff.SequenceEqual([.."RIFF"u8]))
        {
            throw new InvalidDataException("Invalid RIFF header");
        }

        // Size: uint32
        headerFileSize = ReadUInt32();

        // WEBP
        var webp = ReadBytes(4);
        if (!webp.SequenceEqual([.."WEBP"u8]))
        {
            throw new InvalidDataException("Invalid WEBP header");
        }
    }

    // Read a single chunk and discard its contents
    private string ReadVoidChunk()
    {
        // FourCC: 4 bytes in ASCII
        var result = ReadBytes(4);

View on GitHub (pinned to af93d6ef57)