LocalFiles

write()

files.write(file, content) → Promise<Item>

Writes a file and returns what was written. Text or binary, parents created, and an existing file is replaced.

freereplacestext and binary
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Parameters

filestringrequired

The path to write, relative to the workspace.

contentstring | Buffer | Uint8Array | ArrayBufferrequired

What to write. Anything else is refused.

Text

const item = await pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.");
// { name: "brief.md", path: "Launch plan/brief.md", type: "file", size: 29,
//   mimeType: "text/markdown", modifiedAt: "…" }
item = pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.")
# {"path": "Launch plan/brief.md", "size": 29}

A string is written as UTF-8. The mimeType that comes back is read from the extension, not from the content — so the name you choose is what decides how the file is later treated.

Binary

// A generated image, straight from a model session
const [picture] = await session.generate({
  inputs: [{ type: "text", content: "A fox in the snow" }],
  outputs: [{ type: "image" }],
}).then((r) => r.outputs);

await pc.files.write("Assets/fox.png", Buffer.from(picture.content, "base64"));

// A download
const response = await fetch("https://example.com/report.pdf");
await pc.files.write("Reports/report.pdf", await response.arrayBuffer());
import base64, urllib.request

# A generated image, straight from a model session
picture = session.generate(
    inputs=[{"type": "text", "content": "A fox in the snow"}],
    outputs=[{"type": "image"}],
)["outputs"][0]

pc.files.write("Assets/fox.png", base64.b64decode(picture["content"]))

# A download
with urllib.request.urlopen("https://example.com/report.pdf") as response:
    pc.files.write("Reports/report.pdf", response.read())

It replaces, and keeps nothing

There is no previous version here

write() is the plain operation: the old content is gone. Keeping a copy is what the agent's own write tools add on top, under .versions/ — if you want that behaviour in your code, read the file first and write the copy yourself.

// Keep a copy before replacing
const before = await pc.files.read("notes.md");
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
await pc.files.write(`.versions/notes.md/${stamp}.md`, before.content);
await pc.files.write("notes.md", next);
from datetime import datetime, timezone

# Keep a copy before replacing
before = pc.files.read("notes.md")
stamp = datetime.now(timezone.utc).isoformat().replace(":", "-").replace(".", "-")
pc.files.write(f".versions/notes.md/{stamp}.md", before["content"])
pc.files.write("notes.md", next_content)

Folders are created as needed

Writing Clients/Acme/2026/invoice.md creates the three folders on the way. One call, whatever the depth.

Errors

CodeStatusWhen
missing_content400content is undefined or null. An empty string is valid.
invalid_content400Not a string, a Buffer, a Uint8Array or an ArrayBuffer.
invalid_path400Absolute, outside the workspace, or the workspace root.
local_files_require_node400The runtime has no filesystem.
storage_not_on_this_machine409Another computer's storage.

An object is not serialised for you: pass JSON.stringify(value), so the formatting stays yours.