Python SDK
This Python client provides a simple, high-level interface for creating and interacting with sandboxes managed by the Agent Sandbox controller. It’s designed to be used as a context manager, ensuring that sandbox resources are properly created and cleaned up.
Usage
Prerequisites
- A running Kubernetes cluster (e.g.,
kind). - The Agent Sandbox controller must be deployed with the extensions feature enabled.
- A
SandboxTemplateresource must be created in the cluster. - The
kubectlcommand-line tool must be installed and configured to connect to your cluster.
Installation
- Create a virtual environment:
python3 -m venv .venv - Activate the virtual environment:
source .venv/bin/activate - Option 1: Install from source via git:
# Replace "main" with a specific version tag (e.g., "v0.1.0") from # https://github.com/kubernetes-sigs/agent-sandbox/releases to pin a version tag. export VERSION="main" pip install "git+https://github.com/kubernetes-sigs/agent-sandbox.git@${VERSION}#subdirectory=clients/python/agentic-sandbox-client" - Option 2: Install from source in editable mode:
git clone https://github.com/kubernetes-sigs/agent-sandbox.git cd agent-sandbox/clients/agentic-sandbox-client-python cd ~/path_to_venv pip install -e .
Example:
from agentic_sandbox import SandboxClient
with SandboxClient(template_name="sandbox-python-template", namespace="default") as sandbox:
result = sandbox.run("echo 'Hello, World!'")
print(result.stdout)
How It Works
The SandboxClient client automates the entire lifecycle of a temporary sandbox environment:
-
Initialization (
with SandboxClient(...)): The client is initialized with the name of theSandboxTemplateyou want to use and the namespace where the resources should be created:template_name(str): The name of theSandboxTemplateresource to use for creating the sandbox.namespace(str, optional): The Kubernetes namespace to create theSandboxClaimin. Defaults to “default”.- When you create a
SandboxClientinstance within awithblock, it initiates the process of creating a sandbox.
-
Claim Creation: It creates a
SandboxClaimKubernetes resource. This claim tells the agent-sandbox controller to provision a new sandbox using a predefinedSandboxTemplate. -
Waiting for Readiness: The client watches the Kubernetes API for the corresponding
Sandboxresource to be created and become “Ready”. This indicates that the pod is running and the server inside is active. -
Port Forwarding: Once the sandbox pod is ready, the client automatically starts a
kubectl port-forwardprocess in the background. This creates a secure tunnel from your local machine to the sandbox pod, allowing you to communicate with the server running inside. -
Interaction: The
SandboxClientobject provides three main methods to interact with the running sandbox:run(command): Executes a shell command inside the sandbox.write(path, content): Uploads a file to the sandbox.read(path): Downloads a file from the sandbox.
-
Cleanup (
__exit__): When thewithblock is exited (either normally or due to an error), the client automatically cleans up all resources. It terminates thekubectl port-forwardprocess and deletes theSandboxClaim, which in turn causes the controller to delete theSandboxpod.
How to Test the Client
A test script, test_client.py, is included to verify the client’s functionality.
You should see output indicating that the tests for command execution and file operations have passed.
Packaging and Installation
This client is configured as a standard Python package using pyproject.toml.
Prerequisites
- Python 3.7+
pipbuild(install withpip install build)
Building the Package
To build the package from the source, navigate to the agentic-sandbox-client directory and run the following command:
python -m build
This will create a dist directory containing the packaged distributables: a .whl (wheel) file and a .tar.gz (source archive).
Feedback
Was this page helpful?