BigPizzaV3/CodexPlusPlus · error · anyhow::Error
Page.captureScreenshot returned invalid PNG data
Error message
Page.captureScreenshot returned invalid PNG data
What it means
After Page.captureScreenshot succeeds, the bridge base64-decodes result.data and verifies the decoded bytes start with the 8-byte PNG signature (\x89PNG\r\n\x1a\n). If the decoded payload does not begin with the magic bytes, the bridge assumes the browser returned corrupt or non-PNG data and bails with this error, refusing to write a broken file to output_path.
Solutions
- Verify the target browser is Chromium-based and honors Page.captureScreenshot format:'png'
- Retry the capture — transient renderer issues can yield bad frames
- Capture the decoded payload prefix and inspect it (e.g. is it JPEG \xFF\xD8? then the format param is being ignored)
- Remove any proxy/instrumentation between the bridge and the CDP websocket and retry
Example fix
// before
let bytes = base64::engine::general_purpose::STANDARD.decode(encoded)?;
// after (diagnose unexpected format)
let bytes = ...decode...;
assert!(bytes.starts_with(&[137,80,78,71,13,10,26,10]), "first bytes: {:02x?}", &bytes[..8.min(bytes.len())]); Defensive patterns
Strategy: validation
Validate before calling
const PNG_MAGIC: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
// after decoding the base64 payload yourself:
if !bytes.starts_with(&PNG_MAGIC) { eprintln!("non-PNG payload, first bytes: {:02x?}", &bytes[..bytes.len().min(8)]); } Type guard
fn is_png(bytes: &[u8]) -> bool {
bytes.starts_with(&[137, 80, 78, 71, 13, 10, 26, 10])
} Try / catch
match capture_page_screenshot(&ws, &path).await {
Ok(len) => println!("saved {len} bytes"),
Err(e) if e.to_string().contains("invalid PNG data") => {
eprintln!("target returned non-PNG data; check browser/format support");
}
Err(e) => return Err(e),
} Prevention
- Verify the CDP target is a Chromium build honoring format:'png'
- Strip proxies/instrumentation that could re-encode capture responses
- Validate the PNG magic before writing files to avoid corrupt artifacts
When it happens
Trigger: A CDP endpoint returns captureScreenshot data that decodes but is not PNG — e.g. an older/nonstandard browser, a remote-debugging proxy re-encoding the image, or mocked/intercepted responses in embedded/patched Chromium builds.
Common situations: Pointing the bridge at a non-Chromium CDP implementation that ignores the format:'png' parameter; a middlebox or instrumentation layer altering the response payload; memory pressure in the target causing truncated frames.
Understand the failure class
Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.
Related errors
- CDP command id failed
- CDP websocket closed before response for
- Page.captureScreenshot returned no image data
- browser WebSocket URL has no Browser ID
- browser WebSocket URL has no path
AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19).
Data as JSON: /api/errors/edacc26e4f9be470.
Report an issue: GitHub.
Appendix: source
Thrown at crates/codex-plus-core/src/bridge.rs:211
output_path: &Path,
) -> anyhow::Result<u64> {
let response = send_cdp_command(
websocket_url,
"Page.captureScreenshot",
capture_screenshot_params(),
)
.await?;
let encoded = response
.get("result")
.and_then(|result| result.get("data"))
.and_then(Value::as_str)
.filter(|data| !data.is_empty())
.ok_or_else(|| anyhow::anyhow!("Page.captureScreenshot returned no image data"))?;
let bytes = base64::engine::general_purpose::STANDARD
.decode(encoded)
.context("failed to decode screenshot PNG")?;
if !bytes.starts_with(&[137, 80, 78, 71, 13, 10, 26, 10]) {
bail!("Page.captureScreenshot returned invalid PNG data");
}
crate::settings::atomic_write(output_path, &bytes)
.with_context(|| format!("failed to save screenshot {}", output_path.display()))?;
Ok(bytes.len() as u64)
}
pub async fn run_periodic_evaluations<F>(
websocket_url: &str,
period: Duration,
mut next_expression: F,
) -> anyhow::Result<()>
where
F: FnMut() -> anyhow::Result<Option<String>>,
{
let socket = connect_cdp_websocket(websocket_url).await?;
let mut session = CdpSession::new(socket);
let mut interval = tokio::time::interval(period);
loop {View on GitHub (pinned to b1ed92e5e4)