Upload files with the Python SDK

One call for a file up to 1 GB, a way to finish after a crash, and a whole folder at once.

One file

client.documents.upload does every step of an upload: it hashes the file, starts the upload, sends the parts to storage four at a time, gets new addresses for parts whose address expired, sends a failed part again, and completes the upload.

from byterover import ByteRover

client = ByteRover()
drive_id = "drv_4h7k2m9q8r1t5v6w3x0y"

result = client.documents.upload(
    drive_id,
    "/policies/customer-refund-policy.pdf",
    "customer-refund-policy.pdf",
    tags=["policy"],
    wait="searchable",
)
print(result.document.status, result.operation.status)

The path decides the document: a document already at the path gets a new revision. By default the call returns once the upload is complete. With wait="done" it also waits for the file's operation to finish, and with wait="searchable" it fails unless the document is then searchable. wait_timeout is 10 minutes by default.

The source can be a file path, bytes, a binary file you opened (open(path, "rb")), or an object with a size and a read(start, end) method. The SDK reads it twice, once to hash it and once to send it, so a stream that can be read only once is refused. Every option is checked before the file is read.

The API chooses the part size: every part but the last is part_size bytes, 8 MiB today. on_upload_created hands you part_count and part_size.

After the upload completed

With wait, anything that fails after the upload completed is an UploadWaitError. The document exists, so don't upload the file again: the error has its document_id and operation_id, and the first error as its __cause__.

from byterover import UploadWaitError

try:
    client.documents.upload(drive_id, "/reports/q3.pdf", "q3.pdf", wait="done")
except UploadWaitError as error:
    print("Saved as", error.document_id, "but", error.__cause__)
    client.operations.wait(error.operation_id)

Survive a crash

Pass your own idempotency_key when an upload must survive your process stopping. Run the same upload again, with the same key and the same file, and it either finishes the earlier upload or answers the document the earlier run made. The file is never stored twice. Keys last 24 hours. Here a key can have at most 240 characters, not 255: the upload makes two keys from yours, <key>:create and <key>:complete, and each must fit the API's limit.

client.documents.upload(drive_id, "/reports/q4.pdf", "q4.pdf", idempotency_key="report-q4-2026")

To finish a large upload after a crash without starting a new one, keep what on_upload_created hands you, and resume with the same file. It holds no part address or key, so it can go in a file. A resume works while the upload is still open, for up to 24 hours, and sends every part again. A part that couldn't be sent ends the upload with an UploadIncompleteError, which names its upload_id and path.

import json
from pathlib import Path

from byterover import UploadIncompleteError

saved = Path("upload.json")
try:
    client.documents.upload(
        drive_id,
        "/archive/2025.pdf",
        "2025.pdf",
        on_upload_created=lambda plan: saved.write_text(json.dumps(plan)),
    )
except UploadIncompleteError:
    # Now, or after a restart:
    client.uploads.resume(json.loads(saved.read_text()), "2025.pdf")

If completing an upload loses its response, the SDK completes it again, which is safe: a completed upload answers with its document. While another completion is still running, it keeps asking for complete_wait, 2 minutes by default, then raises an UploadIncompleteError. Raise complete_timeout for very large files.

A whole folder

client.documents.upload_directory uploads every file whose type ByteRover reads. Hidden files and folders, such as .git and .env, empty files, symbolic links and files of a type ByteRover doesn't read are skipped and listed in skipped. A file or folder that can't be read, or that changed during the upload (swapped for a link, a folder or another file), is listed in failed. include_hidden and ignore change that. Links inside the folder are never followed, and a subfolder or file swapped for a link while the folder is read is refused (in failed). The folder you pass is read even when it is itself a link. The checks go by path, so a program that keeps swapping a folder for a link while the upload runs could still, with exact timing, get a file from outside read: don't upload a folder that other programs are changing. It isn't all or nothing: the result lists what succeeded, what uploaded but isn't ready (uploaded_not_ready: its document exists, its wait failed), what failed and what was skipped. upload passes wait, wait_timeout and part_concurrency to each file's upload, so upload={"wait": "searchable"} waits until every file can be searched; they aren't keyword arguments of upload_directory itself.

result = client.documents.upload_directory(
    drive_id,
    "handbook",
    prefix="/handbook",
    tags=["handbook"],
    upload={"wait": "searchable"},
    ignore=lambda path: path.startswith("drafts/"),
    on_progress=lambda progress: print(f"{progress.done}/{progress.total} {progress.path}"),
)

for failure in result.failed:
    print(failure.path, failure.error)
print(len(result.succeeded), "uploaded,", len(result.skipped), "skipped")

If you stop it (Ctrl-C), it still raises, once the files in flight have settled, and the exception carries what finished as upload_directory_result, so you don't upload those again. A file whose completion was cut off may still be stored: look for it with documents.list before uploading it again.

Step by step

The steps are there too: client.uploads.create, list_parts, put_part and complete. put_part sends no API key, since a part goes straight to storage, and never logs the part's address, which is itself a credential.

import hashlib
from pathlib import Path

data = Path("notes.pdf").read_bytes()
plan = client.uploads.create(
    drive_id,
    path="/notes/notes.pdf",
    size_bytes=len(data),
    sha256=hashlib.sha256(data).hexdigest(),
)

parts = []
for part in client.uploads.list_parts(plan.upload_id):
    start = (part.part_number - 1) * plan.part_size
    etag = client.uploads.put_part(url=part.url, body=data[start : start + plan.part_size])
    parts.append({"part_number": part.part_number, "etag": etag})

written = client.uploads.complete(plan.upload_id, parts=parts)
print(written.document.id)

On this page