can1357/oh-my-pi · error
resizeImage: failed to decode image and cannot honor exclude
Error message
resizeImage: failed to decode image and cannot honor excludeWebP for a WebP source
What it means
resizeImage falls back to returning the original buffer when Bun.Image cannot decode the input — but if the caller set excludeWebP and the source is (or might be) WebP, honoring that contract is impossible without decoding, so it throws explicitly rather than silently returning WebP data.
Source
Thrown at packages/coding-agent/src/utils/image-resize.ts:388
mimeType: best.mimeType,
originalWidth,
originalHeight,
width: finalWidth,
height: finalHeight,
wasResized: true,
get data() {
return Buffer.from(best.buffer).toBase64();
},
};
} catch {
const headerDimensions = readImageHeaderDimensions(inputBuffer);
const fallbackMimeType = img.mimeType ?? headerDimensions?.mimeType ?? "application/octet-stream";
// Bun.Image rejected the input — we cannot decode/re-encode it.
// When the caller demanded WebP exclusion AND the source might be WebP,
// returning the original buffer would silently violate that contract,
// so surface an explicit error instead.
if (excludeWebP && (fallbackMimeType === "image/webp" || (!img.mimeType && !headerDimensions))) {
throw new Error("resizeImage: failed to decode image and cannot honor excludeWebP for a WebP source");
}
return {
buffer: inputBuffer,
mimeType: fallbackMimeType,
originalWidth: headerDimensions?.width ?? 0,
originalHeight: headerDimensions?.height ?? 0,
width: headerDimensions?.width ?? 0,
height: headerDimensions?.height ?? 0,
wasResized: false,
decodeFailed: true,
get data() {
return img.data;
},
};
}
}
/**View on GitHub (pinned to 9690622007)
Solutions
- Repair or re-export the image (convert to PNG/JPEG externally) so decoding succeeds.
- Drop the excludeWebP flag if returning the original (possibly WebP) buffer is acceptable.
- Verify the file is not truncated or corrupt — decode it in an image viewer or tool.
- Pre-detect the format with a header check and transcode non-PNG/JPEG sources before calling resizeImage.
Example fix
// before: await resizeImage({ data: webpB64, mimeType: "image/webp" }, { excludeWebP: true }); // corrupt webp // after: transcode/repair externally (e.g. magick corrupt.webp out.png), then resizeImage with mimeType image/png Defensive patterns
Strategy: try-catch
Validate before calling
function looksWebp(buf: Uint8Array): boolean { return buf[0] === 0x52 && buf[1] === 0x49 && buf[8] === 0x57 && buf[9] === 0x45; } if (excludeWebP && looksWebp(sourceBytes)) { await transcodeToPng(source); } Type guard
function looksWebp(buf: Uint8Array): boolean { return buf.length > 12 && buf[0] === 0x52 && buf[1] === 0x49 && buf[8] === 0x57 && buf[9] === 0x45; } // RIFF....WEBP Try / catch
try { const resized = await resizeImage(img, { excludeWebP: true }); } catch (err) { if (err instanceof Error && err.message.includes("cannot honor excludeWebP")) { const png = await externalTranscode(source, "png"); return loadImageInput(png); } throw err; } Prevention
- Verify source integrity (magic bytes, decodability) before passing excludeWebP.
- Only set excludeWebP when the provider truly rejects WebP and your pipeline can transcode.
- Keep an external transcoder (sharp/magick) for sources Bun.Image cannot decode.
- Pre-convert WebP/HEIC/AVIF sources to PNG or JPEG at ingestion.
When it happens
Trigger: Calling resizeImage (directly or via loadInMemoryImageInput with excludeWebP:true) with data Bun.Image fails to decode, where the declared mimeType is image/webp or the format could not be identified from headers either.
Common situations: Providers reject WebP so callers pass excludeWebP; source file is corrupt or truncated WebP; file has unknown format and undecodable bytes; exotic codecs (HEIC, AVIF) that cannot be transcoded.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- imageKind === "svg" ? "The ':img' selector only supports .sv
- error.message (ImageInputTooLargeError or InvalidImageDataEr
- ${options.resolvedPath} is not a decodable ${options.image.m
- invalid byte sequence: {:02x?}
- Unknown image type: ${mimeType}
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/2ecb5a94dae9f654.
Report an issue: GitHub.