open-swe/scripts/create_sandbox_snapshot.py
Ramon Nogueira 5925a90a95
feat: migrate LangSmith sandbox creation to snapshot API (#1201)
* feat: migrate LangSmith sandbox creation to snapshot API

Replaces the template-based sandbox flow (DEFAULT_SANDBOX_TEMPLATE_NAME /
DEFAULT_SANDBOX_TEMPLATE_IMAGE) with the new snapshot-based flow.

- New required env var DEFAULT_SANDBOX_SNAPSHOT_ID (UUID of a pre-built
  LangSmith snapshot; build out-of-band via UI or SandboxClient.create_snapshot)
- Optional DEFAULT_SANDBOX_SNAPSHOT_FS_CAPACITY_BYTES overrides the root FS
  size at boot (default 32 GiB)
- Startup-time validation via a FastAPI lifespan hook: the server refuses
  to boot with a clear ValueError if SANDBOX_TYPE=langsmith and
  DEFAULT_SANDBOX_SNAPSHOT_ID is unset, so failures surface in boot logs
  rather than on the first thread
- Reconnect-to-existing-sandbox path unchanged
- Docs (INSTALLATION.md, CUSTOMIZATION.md) updated to describe the new
  snapshot workflow

* fix: format create_sandbox_snapshot.py to pass ruff

---------

Co-authored-by: aran-yogesh <yogesh.mahendran@langchain.dev>
2026-04-21 12:17:19 -07:00

47 lines
1.4 KiB
Python

"""Create a LangSmith sandbox snapshot for open-swe."""
import argparse
import os
from langsmith.sandbox import SandboxClient
DEFAULT_IMAGE = "bracelangchain/deepagents-sandbox:v1"
DEFAULT_FS_CAPACITY = 32 * 1024**3 # 32 GiB
def main() -> None:
parser = argparse.ArgumentParser(description="Create a LangSmith sandbox snapshot")
parser.add_argument(
"--name", default="open-swe-new", help="Snapshot name (default: open-swe-new)"
)
parser.add_argument(
"--image", default=DEFAULT_IMAGE, help=f"Docker image (default: {DEFAULT_IMAGE})"
)
parser.add_argument(
"--fs-capacity",
type=int,
default=DEFAULT_FS_CAPACITY,
help="FS capacity in bytes (default: 32 GiB)",
)
parser.add_argument(
"--api-key",
default=os.environ.get("LANGSMITH_API_KEY") or os.environ.get("LANGSMITH_API_KEY_PROD"),
help="LangSmith API key",
)
args = parser.parse_args()
if not args.api_key:
raise SystemExit("Set LANGSMITH_API_KEY or pass --api-key")
client = SandboxClient(api_key=args.api_key)
snapshot = client.create_snapshot(
name=args.name,
docker_image=args.image,
fs_capacity_bytes=args.fs_capacity,
)
print(f"Snapshot created: {snapshot.id}")
print(f"\nAdd to your .env:\n DEFAULT_SANDBOX_SNAPSHOT_ID={snapshot.id}")
if __name__ == "__main__":
main()