langchain - ✅(Solved) Fix Add Capsule to safely run untrusted code using WebAssembly sandboxes [1 pull requests, 7 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35518Fetched 2026-04-08 00:25:52
View on GitHub
Comments
7
Participants
2
Timeline
20
Reactions
0
Author
Participants
Timeline (top)
commented ×7labeled ×4subscribed ×4mentioned ×3

Root Cause

Only the first run takes a second (cold start), then every next run starts in ~10ms. This is because Capsule compiles and caches a native version of the Wasm module locally after the first execution.

Fix Action

Fixed

PR fix notes

PR #3416: docs: Add Capsule code interpreter integration

Description (problem / solution / changelog)

Overview

Adds documentation for langchain-capsule, a LangChain integration that lets agents safely execute Python and JavaScript code in isolated WebAssembly sandboxes.

Two execution modes are covered:

  • Stateless tools (CapsulePythonTool, CapsuleJSTool) — each call runs in a fresh sandbox
  • Session-based REPL tools (CapsulePythonREPLTool, CapsuleJSREPLTool) — variables and state persist across calls, with support for file import/export

Type of change

Type: New documentation page

Related issues/PRs

  • GitHub issue: langchain-ai/langchain#35518

Checklist

  • I have read the contributing guidelines, including the language policy
  • I have tested my changes locally using docs dev
  • All code examples have been tested and work correctly
  • I have used root relative paths for internal links
  • I have updated navigation in src/docs.json if needed

Additional notes

langchain-capsule is a community-maintained package (PyPI, GitHub) built on top of Capsule, a WebAssembly-based sandboxing runtime.

Files added/changed:

  • src/oss/python/integrations/providers/capsule.mdx — new provider page
  • src/oss/python/integrations/tools/capsule.mdx — new tool guide
  • src/oss/python/integrations/providers/all_providers.mdx — added Capsule card
  • src/oss/python/integrations/tools/index.mdx — added to code interpreter table and all-tools grid

Changed files

  • src/oss/python/integrations/providers/all_providers.mdx (modified, +8/-0)
  • src/oss/python/integrations/providers/capsule.mdx (added, +34/-0)
  • src/oss/python/integrations/tools/capsule.mdx (added, +150/-0)
  • src/oss/python/integrations/tools/index.mdx (modified, +2/-0)

Code Example

import asyncio
from langchain_capsule import CapsulePythonTool, CapsuleJSTool

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

# JavaScript / TypeScript Example
js_tool = CapsuleJSTool()
result = asyncio.run(js_tool.arun("1 + 2"))
print(result) # "3"
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

I would like LangChain to support Capsule as a lightweight, self-hosted sandbox for executing untrusted Python/JavaScript code. Capsule is a runtime that runs python/javascript code in WebAssembly sandboxes.

Use Case

When AI agents generate code, executing it directly could be risky for the host system. Except for Docker containers or cloud-based tools, there are few lightweight alternatives. While langchain-sandbox (Pyodide/Wasm) seems to be a great solution, it executes Python code only and is now archived.

Proposed Solution

Integrate langchain-capsule to documentation, a LangChain version of Capsule. Here's how the implementation works:

import asyncio
from langchain_capsule import CapsulePythonTool, CapsuleJSTool

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

# JavaScript / TypeScript Example
js_tool = CapsuleJSTool()
result = asyncio.run(js_tool.arun("1 + 2"))
print(result) # "3"

Only the first run takes a second (cold start), then every next run starts in ~10ms. This is because Capsule compiles and caches a native version of the Wasm module locally after the first execution.

Alternatives Considered

No response

Additional Context

Happy to submit a PR in the LangChain docs if there's interest. Also open to other integration possibilities around Capsule!

extent analysis

Fix Plan

Integrate langchain-capsule into LangChain

To integrate langchain-capsule into LangChain, follow these steps:

Step 1: Install langchain-capsule

pip install langchain-capsule

Step 2: Update LangChain documentation

Add a new section to the LangChain documentation to include langchain-capsule. You can create a new file in the docs directory, e.g., capsule.md.

Step 3: Add Capsule tools to LangChain

Create a new file in the langchain package, e.g., capsule_tools.py. Add the following code:

from langchain_capsule import CapsulePythonTool, CapsuleJSTool

class CapsulePythonTool:
    def __init__(self):
        self.python_tool = CapsulePythonTool()

    def run(self, code):
        return self.python_tool.run(code)

class CapsuleJSTool:
    def __init__(self):
        self.js_tool = CapsuleJSTool()

    async def run(self, code):
        return await self.js_tool.arun(code)

Step 4: Update LangChain API

Update the LangChain API to include the new Capsule tools. You can add a new method to the LLaMA class, e.g., run_capsule.

class LLaMA:
    # ...

    def run_capsule(self, code, language):
        if language == 'python':
            return self.capsule_python_tool.run(code)
        elif language == 'javascript':
            return self.capsule_js_tool.run(code)
        else:
            raise ValueError('Unsupported language')

Step 5: Test the new feature

Create a new test file to test the new Capsule feature. You can use the following code:

import unittest

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING