jackwener/OpenCLI · error · CommandExecutionError

Midjourney original image returned invalid media bytes from

Error message

Midjourney original image returned invalid media bytes from ${resolved.url}

What it means

Even though the transfer completed, sniffing the actual magic bytes of the downloaded buffer showed it is not an image (actualMime does not start with 'image/'). The library validates file content, not just headers, so corrupt or wrongly-typed payloads are caught before writing the file.

Source

Thrown at clis/midjourney/utils.js:664

    }
  }

  let media = null;
  let resolved = null;
  let lastError = null;
  for (const candidate of candidates) {
    try {
      media = await fetchMediaThroughPage(page, candidate.url, 'image/');
      resolved = candidate;
      break;
    } catch (error) {
      lastError = error;
    }
  }
  if (!media || !resolved) throw lastError || new CommandExecutionError(`No Midjourney original image was available for ${jobId}`);
  const actualMime = sniffMediaMime(media.buffer);
  if (!actualMime?.startsWith('image/')) {
    throw new CommandExecutionError(`Midjourney original image returned invalid media bytes from ${resolved.url}`);
  }
  if (media.mime !== actualMime) {
    log.warn(`Midjourney CDN reported ${media.mime || 'unknown'} for ${resolved.url}; detected ${actualMime} from file bytes`);
  }
  const extension = actualMime === 'image/png'
    ? '.png'
    : actualMime === 'image/webp'
      ? '.webp'
      : actualMime === 'image/gif'
        ? '.gif'
        : '.jpg';
  const filePath = path.join(outputDir, `${jobId}_${index}${extension}`);

  const tempPath = `${filePath}.part-${process.pid}-${Date.now()}`;
  try {
    await fs.writeFile(tempPath, media.buffer);
    await fs.rename(tempPath, filePath);
  } catch (error) {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-download with a freshly resolved URL.
  2. Inspect the downloaded bytes (hexdump the first bytes) to see what was actually served.
  3. Check for proxies/VPNs injecting content and disable them.
  4. Confirm the job produced an original image (not a video) for this index.
Defensive patterns

Strategy: validation

Validate before calling

const res = await fetch(url);
const head = new Uint8Array(await res.arrayBuffer()).slice(0, 8);
const isPng = head[0] === 0x89 && head[1] === 0x50;
const isJpeg = head[0] === 0xff && head[1] === 0xd8;
if (!isPng && !isJpeg) throw new Error('URL does not serve image bytes');

Type guard

function isImageBuffer(buf) {
  return buf.length > 4 &&
    ((buf[0] === 0x89 && buf[1] === 0x50) || (buf[0] === 0xff && buf[1] === 0xd8));
}

Try / catch

try {
  await downloadOriginals(page, jobId, indices, outDir);
} catch (err) {
  if (/invalid media bytes/.test(err.message)) {
    const freshUrl = await resolveFreshUrl(jobId);
    await downloadOriginals(page, jobId, indices, outDir);
  } else throw err;
}

Prevention

When it happens

Trigger: The CDN (or an intermediary) served HTML/JSON/error text that happened to pass header checks; the downloaded bytes are truncated or corrupted so no known magic number matches; the URL resolves to a non-image resource.

Common situations: Proxy/cache serving a captive-portal or error page with a 200; partially flushed downloads; requesting a URL that is actually a webpage.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/4ddba3199d312a45. Report an issue: GitHub.