langchain-capsule
Capsule integration for LangChain — run Python and JavaScript code in isolated WebAssembly sandboxes.
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
Metadata
Release files for langchain-capsule 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| langchain_capsule-0.2.0.tar.gz | 7.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_capsule-0.2.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 13.1 kB
Release files / langchain_capsule-0.2.0.tar.gz
| Download URL | langchain_capsule-0.2.0.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d691e9fd4f24aaaf34f3fd6918ea34c6a5c482af68fe5eda8d045e7ff664e137
|
|
BLAKE2b-256 checksum How to use checksums |
9c03804fd57dd372698f0c0d1493387eba3f4880ad2bf02c203ef03fd684e9e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / langchain_capsule-0.2.0-py2.py3-none-any.whl
| Download URL | langchain_capsule-0.2.0-py2.py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
56aed7d207f6ac3d0a87d62f83aac9b3ecbb7d8b8a4903a5f8b4654d05fa0170
|
|
BLAKE2b-256 checksum How to use checksums |
e76a7880fa1258ba0eff6d8c73b2364c08f7631db3a7a4553c8f95fee4ff501f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for langchain-capsule 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.