Python SDK

Sync and async clients for the upstream platform. Requires Python 3.10+.

$ pip install upstream-sdk

Upstream (sync client)

The primary entry point. Wraps an httpx.Client for synchronous HTTP.

Upstream(api_key: str, *, base_url: str = "https://api.upstream.build", timeout: float = 30.0)
api_keystrYour API key (starts with sk-)
base_urlstrAPI base URL. Override for self-hosted deployments.
timeoutfloatHTTP request timeout in seconds.

sandbox()

Create a sandbox session. Returns a Sandbox context manager.

client.sandbox(image: str = "python:3.12", *, vcpu: int = 2, memory_mib: int = 512, ready_timeout: float = 30.0) → Sandbox
imagestrTemplate or image name. See Templates.
vcpuintVirtual CPU count (1--4).
memory_mibintMemory in MiB (128--2048).
ready_timeoutfloatMax seconds to wait for VM to become ready.
with client.sandbox(image="python:3.12", vcpu=2, memory_mib=512) as sb:
    result = sb.exec(["python", "-c", "print('hello')"])

run()

Submit a markdown pipeline to the turn executor and poll until complete.

client.run(markdown: str, *, poll_interval: float = 1.0, timeout: float = 300.0) → RunResult
markdownstrMarkdown pipeline content with fenced bash blocks.
poll_intervalfloatSeconds between status polls.
timeoutfloatMax seconds to wait for completion.

usage()

Get account usage summary.

client.usage() → Usage

close()

Close the underlying HTTP client. Called automatically if using context manager patterns.

AsyncUpstream

Async variant. Same interface, backed by httpx.AsyncClient. Use as an async context manager.

from upstream import AsyncUpstream

async with AsyncUpstream(api_key="sk-...") as client:
    usage = await client.usage()

Sandbox

Represents a live Firecracker microVM session. Use as a context manager for automatic cleanup.

exec()

Execute a command inside the sandbox.

sb.exec(command: list[str], *, stream: bool = False) → ExecResult | StreamingExecResult
commandlist[str]Command and arguments as a list.
streamboolIf True, returns StreamingExecResult with iterable stdout.

write_file()

sb.write_file(path: str, content: str) → None

Write a file inside the sandbox. Uses exec with a heredoc internally.

read_file()

sb.read_file(path: str) → str

Read a file from the sandbox. Raises SandboxError if the file does not exist.

destroy()

sb.destroy() → None

Destroy the sandbox VM. Called automatically when the with block exits.

Data types

ExecResult

Returned by sb.exec() when stream=False (the default).

FieldTypeDescription
exit_codeintProcess exit code (0 = success)
stdoutstrStandard output
stderrstrStandard error

StreamingExecResult

Returned by sb.exec(stream=True). Iterates stdout lines as they arrive.

FieldTypeDescription
stdoutIterator[str]Yields lines as they arrive from the VM
exit_codeintProcess exit code (available after iteration)

RunResult

Returned by client.run().

FieldTypeDescription
run_idintUnique run identifier
statusstr"completed" or "failed"
step_countintTotal pipeline steps
pass_countintSteps that passed
rawdictFull JSON response

Usage

FieldTypeDescription
total_sessionsintTotal sandbox sessions created
total_vcpu_secondsfloatTotal compute consumed
rawdictFull JSON response

Exceptions

All exceptions inherit from UpstreamError, which carries an optional status_code.

ExceptionHTTP codesWhen
AuthError401, 403Invalid or missing API key
QuotaError429Rate limit or quota exceeded
SandboxError4xxSandbox-specific failures (create, exec, destroy)
TimeoutError--Sandbox not ready or exec not complete in time
UpstreamError5xxBase class; server errors