Skip to navigation

Python SDK (preview)

meadow-python is a Python SDK for Meadow’s own API: sandboxes, images, projects, keys, provider keys, usage, budgets and the audit log. It is a preview. Version 0.1.0 is not published on PyPI yet, so install it from source as below. The import name is meadow.

Model calls do not need it. Keep the official openai and anthropic packages and set their base URL to Meadow, as Use your existing tools shows.

This SDK is separate from the Python client on Clients, which also imports as meadow. Install one of the two in an environment, not both.

Install from source

Needs Python 3.11 or newer, git, and read access to the Meadow source repository, which is private during the beta. Ask your Meadow contact for access.

git clone https://github.com/polymath-ai-labs/meadow.git
pip install ./meadow/sdks/python

Check the install:

python -c "import meadow; print(meadow.__version__, meadow.Meadow)"
0.1.0 <class 'meadow._sync_api.Meadow'>

Quickstart

Set your key. The client reads MEADOW_API_KEY, and MEADOW_BASE_URL when set (the default is https://api.usemeadow.ai). Store a sandbox vendor key first, as Provider keys shows.

export MEADOW_API_KEY="mdw_..."
from meadow import Meadow
with Meadow() as client:
with client.sandboxes.create(
image="python:3.12-slim",
name="my-first-sandbox",
timeout_seconds=600,
wait=True,
) as sandbox:
sandbox.wait_running()
print("sandbox", sandbox.id, "is running on", sandbox.provider)
result = sandbox.exec("python --version && echo hello from the sandbox")
print("exit code", result.exit_code)
print(result.stdout, end="")
sandbox.upload_file("/tmp/hello.txt", "hello from your machine\n")
print(sandbox.download_file("/tmp/hello.txt").decode(), end="")
for event in sandbox.exec_stream("for i in 1 2 3; do echo line $i; sleep 1; done"):
if event.event == "stdout":
print(event.text, end="")
elif event.event == "exit":
print("stream exit code", event.exit_code)
sandbox sbx_... is running on modal
exit code 0
Python 3.12.15
hello from the sandbox
hello from your machine
line 1
line 2
line 3
stream exit code 0

Leaving the inner with block deletes the sandbox, also when the code inside raises. timeout_seconds ends it after ten minutes if your program dies first. AsyncMeadow has the same groups and methods as coroutines, and async with works the same way.

What it does for you

  • Retries: at most 3 sends. A 429 is retried for every call. A 5xx, a timeout or a lost connection is retried only for calls that are safe to repeat: creating a project or an image, a project update that rotates the webhook secret, minting or rotating a key, running a command and writing stdin are sent once. The rules are the ones in Errors.
  • Idempotency: sandbox, image, project and key creates and background commands carry one Idempotency-Key across their retries.
  • Timeouts: 60 seconds per call by default, covering the whole answer. A command waits its timeout_seconds plus 30 seconds. A stream has no read timeout.
  • Pagination: iterating over client.sandboxes.list() yields every sandbox and fetches the next pages.
  • Errors: the OpenAI SDK’s classes (meadow.NotFoundError, meadow.ConflictError, meadow.RateLimitError, …) with status_code, type, code, message and request_id from Meadow’s error body. Those fields are cut, have keys replaced by [redacted] and have control characters escaped; err.body holds the raw body.
  • Your key: the base URL must be https (or http to a loopback host), redirects are never followed, and repr() of the client and of models hides keys, secrets, SSH and port access, environment maps and command output. A custom HTTPS base URL receives your key: set it only to a host you trust.
  • Ids are checked before they go into a URL, an answer is capped at 32 MiB (2 GiB for a file, with download_file_to to stream one), and a streamed command stops at a line over 1 MiB, an event over 4 MiB or an exit event without an integer exit_code.

The README in sdks/python lists every method.

Operations used on this page: createSandbox, execInSandbox, uploadFile, downloadFile, deleteSandbox.