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 substrate-runtime binary from binary_path / SUBSTRATE_RUNTIME_BIN, the installed substrate-runtime package, or substrate-runtime 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.1.tar.gz (37.9 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.1-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: substrate_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 37.9 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.1.tar.gz
Algorithm Hash digest
SHA256 47873817eac674008b9fdcac8dec838fba83fbf63c3270d5f980b6ee372b97df
MD5 aa512c23eb3726f6f393302a6e686813
BLAKE2b-256 78346e7453959053052d3e9a9ed6f20f6b313b663db57b30b2ff0cf0bb9d48ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: substrate_sdk-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c00e3896e20685cea657feedd91c46030b82b77e3310530c392e238382a69e7d
MD5 c9825f59c52db1f64282cb3dec85e538
BLAKE2b-256 8901f372ec3e22fd7a064001fdf8d025b4384141ad614016b9a255e935a69e8a

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