{"record":{"id":"8d8b2b2d158d289f","repo":"danny-avila/LibreChat","slug":"unexpected-output-while-reading-image-bytes-from-t","errorCode":null,"errorMessage":"Unexpected output while reading image bytes from the sandbox: ${String(result.stdout).slice(0, 120)}","messagePattern":"Unexpected output while reading image bytes from the sandbox: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/server/services/Files/Code/process.js","lineNumber":1418,"sourceCode":"      );\n    }\n    if (result.stderr && (result.stdout == null || result.stdout === '')) {\n      throw new Error(String(result.stderr).trim());\n    }\n    if (result.stdout == null || String(result.stdout).trim() === '') {\n      return {};\n    }\n    /* Parse the LAST non-empty line: the reader's JSON is the final thing it\n     * prints, so anything a shell profile or library emitted ahead of it\n     * (banners, warnings) must not break the read. */\n    const lines = String(result.stdout)\n      .split('\\n')\n      .map((line) => line.trim())\n      .filter(Boolean);\n    try {\n      return JSON.parse(lines[lines.length - 1]);\n    } catch {\n      throw new Error(\n        `Unexpected output while reading image bytes from the sandbox: ${String(result.stdout).slice(0, 120)}`,\n      );\n    }\n  } catch (error) {\n    logAxiosError({\n      message: `Error reading sandbox image \"${file_path}\"`,\n      error,\n    });\n    throw error;\n  }\n}\n\n/**\n * Writes a UTF-8 text file into the code-execution sandbox by running a\n * small Python writer through the sandbox `/exec` endpoint. The payload is\n * base64-encoded JSON so neither the file path nor the content is\n * interpolated into shell syntax.\n *","sourceCodeStart":1400,"sourceCodeEnd":1436,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/server/services/Files/Code/process.js#L1400-L1436","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Capture the first 120 chars of stdout from the error message to see what was actually printed.","Ensure the sandbox image's shell profiles do not emit to stdout (redirect banner/motd to stderr or remove).","Confirm the reader script in the sandbox image matches the version the API server expects.","If a banner is the culprit, set a non-interactive shell invocation that skips profiles."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const chunk = await execSandboxImageChunk(params);\n} catch (err) {\n  if (/Unexpected output while reading image bytes/.test(err.message)) {\n    logger.error('Sandbox stdout not JSON — likely a shell-profile banner', { stdout: err.message });\n  }\n  throw err;\n}","preventionTips":["Ensure sandbox shell profiles (.bashrc/.profile) do not write to stdout.","Keep the in-sandbox reader script version aligned with the API server.","Inspect the 120-char stdout preview in the message to identify the noise source."],"tags":["code-env","sandbox","image","parsing","config"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}