jackwener/OpenCLI · error · CommandExecutionError

Gemini image export returned a malformed result

Error message

Gemini image export returned a malformed result

What it means

CommandExecutionError thrown during Gemini image export when an in-page asset record fails structural validation. The library requires each asset to be an object with string url, string dataUrl matching a strict data:image/...;base64 pattern, and an image/* mimeType; anything else is treated as a malformed export result.

Source

Thrown at clis/gemini/utils.js:2509

        // any truthy string: an HTML error body reaches here as text/html, and
        // an empty canvas export as a payload-less data URL (#2245).
        if (/^data:[^;,]+;base64,[A-Za-z0-9+/]+=*$/.test(dataUrl) && String(mimeType).startsWith('image/')) {
          results.push({ url: String(targetUrl), dataUrl, mimeType, width, height });
        }
      }

      return results;
    })(${urlsJson})
  `);
    const assets = requireGeminiArrayResult(result, 'Gemini image export');
    for (const asset of assets) {
        if (!isObjectRecord(asset)
            || typeof asset.url !== 'string'
            || typeof asset.dataUrl !== 'string'
            || !/^data:[^;,]+;base64,[A-Za-z0-9+/]+=*$/.test(asset.dataUrl)
            || typeof asset.mimeType !== 'string'
            || !asset.mimeType.startsWith('image/')) {
            throw new CommandExecutionError('Gemini image export returned a malformed result');
        }
    }
    return assets;
}
export async function waitForGeminiResponse(page, baseline, promptText, timeoutSeconds) {
    if (timeoutSeconds <= 0)
        return '';
    // Reply ownership must survive Gemini prepending older history later.
    // Re-anchor on the submitted user turn when possible, and otherwise only
    // accept assistants that are appended to the exact submission snapshot.
    const pickStructuredReplyCandidate = (current) => {
        if (!current.structuredTurnsTrusted)
            return '';
        const userAnchorTurnIndex = findLastMatchingGeminiTurnIndex(current.turns, baseline.userAnchorTurn);
        if (userAnchorTurnIndex !== null) {
            const candidate = current.turns
                .slice(userAnchorTurnIndex + 1)
                .filter((turn) => turn.Role === 'Assistant')

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-run the export after confirming the Gemini page fully loaded
  2. Update the CLI to a version matching the current Gemini DOM
  3. Log/dump the raw evaluate result to inspect the actual asset shape
Defensive patterns

Strategy: type-guard

Validate before calling

function isValidAsset(a) {
  return !!a && typeof a === 'object'
    && typeof a.url === 'string'
    && typeof a.dataUrl === 'string'
    && /^data:[^;,]+;base64,[A-Za-z0-9+/]+=*$/.test(a.dataUrl)
    && typeof a.mimeType === 'string'
    && a.mimeType.startsWith('image/');
}

Type guard

function isImageAsset(a): a is { url: string; dataUrl: string; mimeType: string } {
  return typeof a === 'object' && a !== null
    && typeof (a as any).url === 'string'
    && typeof (a as any).dataUrl === 'string'
    && /^data:[^;,]+;base64,[A-Za-z0-9+/]+=*$/.test((a as any).dataUrl)
    && typeof (a as any).mimeType === 'string'
    && (a as any).mimeType.startsWith('image/');
}

Try / catch

try {
  const assets = await exportGeminiImages(page, urls);
} catch (err) {
  if (err.message.includes('malformed result')) {
    console.error('Gemini DOM changed or page not fully loaded; re-run after full load or update CLI');
  }
  throw err;
}

Prevention

When it happens

Trigger: The page.evaluate-based export script in clis/gemini/utils.js:2509 returns an asset that is not an object, lacks url/dataUrl/mimeType, has a non-image mimeType, or whose dataUrl is not valid base64 data URI form.

Common situations: Gemini DOM changes altering the shape of the export payload; partial page load; bot-detection or consent screens injecting unexpected DOM; automation running against an outdated Gemini UI layout.

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 jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/1bffe3459a91c168. Report an issue: GitHub.