danny-avila/LibreChat · error

Unexpected output while reading image bytes from the sandbox

Error message

Unexpected output while reading image bytes from the sandbox: ${String(result.stdout).slice(0, 120)}

What it means

Thrown by execSandboxImageChunk (Code/process.js) when the last non-empty line of stdout cannot be JSON.parse'd — the expected `{b64, n, total}` payload is missing or garbled. The error includes the first 120 chars of stdout to aid diagnosis.

Source

Thrown at api/server/services/Files/Code/process.js:1418

      );
    }
    if (result.stderr && (result.stdout == null || result.stdout === '')) {
      throw new Error(String(result.stderr).trim());
    }
    if (result.stdout == null || String(result.stdout).trim() === '') {
      return {};
    }
    /* Parse the LAST non-empty line: the reader's JSON is the final thing it
     * prints, so anything a shell profile or library emitted ahead of it
     * (banners, warnings) must not break the read. */
    const lines = String(result.stdout)
      .split('\n')
      .map((line) => line.trim())
      .filter(Boolean);
    try {
      return JSON.parse(lines[lines.length - 1]);
    } catch {
      throw new Error(
        `Unexpected output while reading image bytes from the sandbox: ${String(result.stdout).slice(0, 120)}`,
      );
    }
  } catch (error) {
    logAxiosError({
      message: `Error reading sandbox image "${file_path}"`,
      error,
    });
    throw error;
  }
}

/**
 * Writes a UTF-8 text file into the code-execution sandbox by running a
 * small Python writer through the sandbox `/exec` endpoint. The payload is
 * base64-encoded JSON so neither the file path nor the content is
 * interpolated into shell syntax.
 *

View on GitHub (pinned to 5ff282f900)

Solutions

  1. Capture the first 120 chars of stdout from the error message to see what was actually printed.
  2. Ensure the sandbox image's shell profiles do not emit to stdout (redirect banner/motd to stderr or remove).
  3. Confirm the reader script in the sandbox image matches the version the API server expects.
  4. If a banner is the culprit, set a non-interactive shell invocation that skips profiles.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const chunk = await execSandboxImageChunk(params);
} catch (err) {
  if (/Unexpected output while reading image bytes/.test(err.message)) {
    logger.error('Sandbox stdout not JSON — likely a shell-profile banner', { stdout: err.message });
  }
  throw err;
}

Prevention

When it happens

Trigger: A shell profile, motd, or library emitted non-JSON text after the reader's final JSON line (the code already filters banners by taking the last line, so this means the reader itself didn't print JSON); or the reader crashed leaving only noise on stdout.

Common situations: Sandbox image has a .bashrc/.profile that prints banners that slip past the last-line filter; reader script version mismatch; sandbox image replaced the reader entrypoint; partial output due to an uncaught OL-adjacent truncation.

Related errors


AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12). Data as JSON: /api/errors/8d8b2b2d158d289f. Report an issue: GitHub.