FoundationAgents/OpenManus · error · RuntimeError
Failed to read file: {e}
Error message
Failed to read file: {e} What it means
Catch-all RuntimeError from DockerSandbox.read_file (app/sandbox/core/sandbox.py:196): the archive was fetched but processing failed — decoding the tar stream, reading members, or content.decode("utf-8"). NotFound is handled separately (error 28); everything else (UnicodeDecodeError, tar errors, Docker API errors mid-stream) lands here with the cause in {e}.
Source
Thrown at app/sandbox/core/sandbox.py:196
"""
if not self.container:
raise RuntimeError("Sandbox not initialized")
try:
# Get file archive
resolved_path = self._safe_resolve_path(path)
tar_stream, _ = await asyncio.to_thread(
self.container.get_archive, resolved_path
)
# Read file content from tar stream
content = await self._read_from_tar(tar_stream)
return content.decode("utf-8")
except NotFound:
raise FileNotFoundError(f"File not found: {path}")
except Exception as e:
raise RuntimeError(f"Failed to read file: {e}")
async def write_file(self, path: str, content: str) -> None:
"""Writes content to a file in the container.
Args:
path: Target path.
content: File content.
Raises:
RuntimeError: If write operation fails.
"""
if not self.container:
raise RuntimeError("Sandbox not initialized")
try:
resolved_path = self._safe_resolve_path(path)
parent_dir = os.path.dirname(resolved_path)
View on GitHub (pinned to 52a13f2a57)
Solutions
- Check {e}: UnicodeDecodeError means binary content — use copy_from instead of read_file for non-text files.
- For directories or large files, use copy_from(src, dst) which handles tar extraction properly.
- Retry once on connection-reset style errors (transient daemon/stream issues).
- Confirm the path points at a regular file: run_command(f"test -f {path}").
Example fix
# before
text = await sandbox.read_file("chart.png") # UnicodeDecodeError -> RuntimeError
# after
await sandbox.copy_from("chart.png", "/tmp/host/chart.png") # binary-safe path Defensive patterns
Strategy: try-catch
Validate before calling
is_text = (await sandbox.run_command(f"file -b {shlex.quote(path)}")).strip().startswith("ASCII") Try / catch
try:
return await sandbox.read_file(path)
except RuntimeError as e:
if "UnicodeDecodeError" in str(e.__cause__ or e):
tmp = tempfile.mkdtemp()
await sandbox.copy_from(path, tmp)
return open(os.path.join(tmp, os.path.basename(path)), "rb").read() # bytes path
raise Prevention
- Use read_file only for text; use copy_from for binary
- Verify the path is a regular file with test -f
- Retry once on transient stream errors
When it happens
Trigger: Reading a binary file (PNG, pickle, zip) — content.decode("utf-8") raises UnicodeDecodeError; tar stream truncated mid-transfer; get_archive returning a directory when a file was expected; Docker API connection reset while streaming chunks.
Common situations: Agent writes a plot/screenshot then read_file is used on it; reading large files that hit daemon stream limits; reading a path that is a directory (archive has multiple members and the reader takes raw bytes).
Related errors
- Failed to write file: {e}
- Source file is empty: {src_path}
- Failed to copy file: {e}
- Failed to create sandbox: {e}
- File not found: {path}
AI-assisted analysis of FoundationAgents/OpenManus@52a13f2a57 (2026-08-15).
Data as JSON: /api/errors/e1818096f1e71504.
Report an issue: GitHub.