LykosAI/StabilityMatrix · error · InvalidDataException
Invalid WEBP header
Error message
Invalid WEBP header
What it means
WebpReader.ReadHeader validates the WEBP signature at bytes 8-11 (after the RIFF marker and size field). If those bytes are not 'WEBP' it throws InvalidDataException: the stream is a RIFF container but not a WEBP file (e.g. a WAV or AVI), or the header is corrupt.
Solutions
- Check bytes 8-11 equal 'WEBP' (and bytes 0-3 equal 'RIFF') before parsing
- Only feed WebpReader streams known to be actual WEBP files
- Catch InvalidDataException and classify the file as unsupported
- Re-download/reconvert the file if it is genuinely meant to be WEBP
Example fix
// before
var animated = new WebpReader(stream).GetIsAnimatedFlag();
// after
var bytes = new byte[12];
stream.ReadExactly(bytes);
stream.Position = 0;
bool isWebp = bytes.AsSpan(0, 4).SequenceEqual("RIFF"u8)
&& bytes.AsSpan(8, 4).SequenceEqual("WEBP"u8);
var animated = isWebp ? new WebpReader(stream).GetIsAnimatedFlag() : false; Defensive patterns
Strategy: validation
Validate before calling
byte[] head = new byte[12];
using (var fs = File.OpenRead(path)) { if (fs.Length < 12) return false; fs.ReadExactly(head); }
bool isWebp = head.AsSpan(0,4).SequenceEqual("RIFF"u8) && head.AsSpan(8,4).SequenceEqual("WEBP"u8); Type guard
static bool IsWebpFile(Stream s) { var b = new byte[12]; long pos = s.Position; if (s.Length - pos < 12) return false; s.ReadExactly(b); s.Position = pos; return b.AsSpan(0,4).SequenceEqual("RIFF"u8) && b.AsSpan(8,4).SequenceEqual("WEBP"u8); } Try / catch
try { animated = new WebpReader(stream).GetIsAnimatedFlag(); }
catch (InvalidDataException ex) { Logger.Debug("Not a WEBP: {Msg}", ex.Message); animated = false; } Prevention
- Check both RIFF and WEBP signatures before reading
- Reject files shorter than the 12-byte header
- Scan directories with extension+magic validation
- Re-download files whose headers look corrupt
When it happens
Trigger: Calling GetIsAnimatedFlag on a non-WEBP RIFF file (WAV/AVI), a file with a corrupted or tampered header, or a stream shorter than 12 bytes so the WEBP read pulls garbage.
Common situations: Files renamed to .webp without conversion; truncated downloads where the size field parsed but format bytes are wrong; batch metadata scans over mixed image/audio directories.
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
- Invalid RIFF header
- Fields line not found
- Unable to locate starting marker of last line
- Invalid Token
- Service type is not assignable to
AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12).
Data as JSON: /api/errors/98e0bbda12c26105.
Report an issue: GitHub.
Appendix: source
Thrown at StabilityMatrix.Core/Helper/Webp/WebpReader.cs:40
}
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);
// Size: uint32
var size = ReadUInt32();
BaseStream.Seek(size, SeekOrigin.Current);
return Encoding.ASCII.GetString(result);
}
}
View on GitHub (pinned to af93d6ef57)