Skip to main content

Capsule sandbox integration for LangChain — run code in isolated WebAssembly sandboxes

Project description

langchain-capsule

Capsule integration for LangChain — run Python and JavaScript code in isolated WebAssembly sandboxes.

CI

What is this?

langchain-capsule gives LangChain agents the ability to safely execute code in isolated WebAssembly sandboxes. The sandbox binaries are bundled inside the package — no configuration or network request required.

Installation

pip install langchain-capsule

Tools

Tool Name Description
CapsulePythonTool python_execution Stateless Python execution — each call is independent
CapsuleJSTool javascript_execution Stateless JavaScript execution — each call is independent
CapsulePythonREPLTool python_repl Persistent Python session — state is preserved across calls
CapsuleJSREPLTool javascript_repl Persistent JavaScript session — state is preserved across calls

Usage

Stateless execution

Each call runs in a fresh sandbox. No state is preserved between calls.

from langchain_capsule import CapsulePythonTool, CapsuleJSTool

python_tool = CapsulePythonTool()
print(python_tool.run("1 + 1"))  # "2"

js_tool = CapsuleJSTool()
print(js_tool.run("1 + 2"))  # "3"

Session-based execution

The session tools preserve variables, imports, and function definitions across calls — useful for multi-step computations or when the agent needs to build up state incrementally.

import asyncio
from langchain_capsule import CapsulePythonREPLTool, CapsuleJSREPLTool

async def main():
    # Python session
    py = CapsulePythonREPLTool()
    await py._arun("total = 0")
    await py._arun("total += 10")
    await py._arun("total += 32")
    print(await py._arun("total"))  # "42"

    # Inspect variables currently in scope
    print(await py.get_state())  # {"total": "int"}

    # Clear state without destroying the session
    await py.reset()

    # Destroy the session and release resources
    await py.close()

    # JavaScript session
    js = CapsuleJSREPLTool()
    await js._arun("let total = 0")
    await js._arun("total += 10")
    await js._arun("total += 32")
    print(await js._arun("total"))  # "42"
    await js.close()

asyncio.run(main())

File operations

The REPL tools can exchange files with the sandbox workspace. This is useful when the agent needs to process existing data or retrieve output files.

import asyncio
from langchain_capsule import CapsulePythonREPLTool

async def main():
    py = CapsulePythonREPLTool()

    # Copy a local file into the sandbox
    await py.import_file("./data.csv", "data.csv")

    # Process it inside the sandbox
    await py._arun("content = open('workspace/data.csv').read()")
    await py._arun("open('workspace/result.txt', 'w').write(content.upper())")

    # Retrieve the output
    await py.export_file("result.txt", "./result.txt")

    # Remove a file from the workspace
    await py.delete_file("data.csv")

    await py.close()

asyncio.run(main())

More information

Visit the Capsule repository for more information.

License

MIT License

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

langchain_capsule-0.2.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

langchain_capsule-0.2.0-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file langchain_capsule-0.2.0.tar.gz.

File metadata

  • Download URL: langchain_capsule-0.2.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for langchain_capsule-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d691e9fd4f24aaaf34f3fd6918ea34c6a5c482af68fe5eda8d045e7ff664e137
MD5 634c865d94f317a655e4f0be0ec28dcc
BLAKE2b-256 9c03804fd57dd372698f0c0d1493387eba3f4880ad2bf02c203ef03fd684e9e4

See more details on using hashes here.

File details

Details for the file langchain_capsule-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_capsule-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 56aed7d207f6ac3d0a87d62f83aac9b3ecbb7d8b8a4903a5f8b4654d05fa0170
MD5 d19fe7a2d9343b6d96ab3afbdd4c3a19
BLAKE2b-256 e76a7880fa1258ba0eff6d8c73b2364c08f7631db3a7a4553c8f95fee4ff501f

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