Skip to main content

Python SDK for the Substrate agent execution environment

Project description

substrate

Python SDK for the Substrate agent execution environment.

Install:

pip install substrate-sdk

The package is pure Python. It does not compile Rust during install. For local managed execution, the SDK discovers a prebuilt executioner runtime from a binary_path / EXECUTIONER_BIN override, an installed substrate-runtime package, or executioner on PATH. Remote-host usage does not need a local runtime.

The public API separates environment lifetime from session lifetime:

from substrate import Environment

with Environment.create(workspace={"kind": "new"}, policy={"process": {"allowExec": True, "allowedCommands": ["ls"]}}) as env:
    session = env.create_session()
    session.write("hello.txt", "hello")
    print(session.read("hello.txt"))

    session.edit({
        "path": "hello.txt",
        "oldString": "hello",
        "newString": "hello from Substrate",
    })

    print(session.bash("ls /workspace"))
    files = session.list()
    artifact = env.export_workspace()
    env.materialize_workspace_artifact(artifact, "/tmp/restored-workspace")

To join an environment created by another process or client, attach to it. An attached handle can create sessions and submit tool calls, but it does not close or destroy the environment when the handle is closed:

env = Environment.attach(
    host={"kind": "http", "baseUrl": "http://127.0.0.1:8765/"},
    environmentId="env_shared",
)
try:
    session = env.create_session()
    session.write("client-a.txt", "hello")
finally:
    env.close()

For an agent loop, pass Substrate's schemas into the model request, then execute matching tool-use blocks directly:

from anthropic import Anthropic
from substrate import Environment, tool_schemas

client = Anthropic()
messages = [{"role": "user", "content": "Create notes.txt and read it back."}]

with Environment.create(
    workspace={"kind": "new"},
    policy={"process": {"allowExec": True, "allowedCommands": ["python", "pytest"]}},
) as env:
    session = env.create_session()
    response = client.messages.create(
        model="...",
        max_tokens=1024,
        tools=tool_schemas(),
        messages=messages,
    )

    for block in response.content:
        if block.type == "tool_use":
            result = session.execute({
                "id": block.id,
                "name": block.name,
                "input": block.input,
            })
            messages.append({
                "role": "user",
                "content": [{
                    "type": "tool_result",
                    "tool_use_id": block.id,
                    "content": result.output,
                }],
            })

The package hides the file-backed queue and worker transport, but keeps environment and session lifecycles explicit. Multiple sessions can attach to the same environment. The host serializes tool execution per environment so those sessions share one mutable workspace through an ordered stream.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

substrate_sdk-0.1.0.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

substrate_sdk-0.1.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file substrate_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: substrate_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for substrate_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ca218f5058c2c1afcd03b7062de609f5ba3df37e7f6c715ef6002ec36083a40f
MD5 b5140cbd11fab93cf12ecc1d771fa9fc
BLAKE2b-256 3962eb1b24967bb37af57c3bf00a9f2a951397a24a21f55a43c44c060c6d4ff9

See more details on using hashes here.

File details

Details for the file substrate_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: substrate_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for substrate_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57c5df14e5ae37d253c3e66234f5344248cef18b736840380d7dedbe01dd8085
MD5 a9e95d7be8672d74b0dfd739db1384be
BLAKE2b-256 834ac1fc8fe8ebc53301e7ce2ab8ddaab6a9501aa8694756cc7938ebc1197e81

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page