freshcrate
Skin:/
Home > Security > langchain-sandbox

langchain-sandbox

Safely run untrusted Python code using Pyodide and Deno

Why this rank:Strong adoptionHealthy release cadence

Description

Safely run untrusted Python code using Pyodide and Deno

README

Warning

This package is no longer maintained. These days we recommend accessing code execution either through sandbox APIs or LLM provider APIs.

We do not recommend using langchain-sandbox for any production use cases. You are welcome to fork the code for your own use cases!

🛡️ LangChain Sandbox

A secure environment for running Python code using Pyodide (WebAssembly) and Deno

Python 3.10+ Deno License: MIT

📋 Overview

LangChain Sandbox provides a secure environment for executing untrusted Python code. It leverages Pyodide (Python compiled to WebAssembly) to run Python code in a sandboxed environment.

✨ Key Features

  • 🔒 Security - Isolated execution environment with configurable permissions
  • 💻 Local Execution - No remote execution or Docker containers needed
  • 🔄 Session Support - Maintain state across multiple code executions

🛡️ Security Considerations

Warning

While LangChain Sandbox uses Pyodide and Deno to isolate code execution, the actual security guarantees depend on how you configure the sandbox and Deno permissions. If you grant broad permissions (such as host file system or unrestricted network access) via arguments passed to PyodideSandbox, sandboxed or untrusted code may access your host resources.

See the Deno Security Model documentation for details on configuring permissions securely. Carefully review and restrict permissions when running untrusted code.

Limitations

  • Latency: There is a few seconds of latency when starting the sandbox per run
  • File access: Currently not supported. You will not be able to access the files written by the sandbox.
  • Network requests: If you need to make network requests please use httpx.AsyncClient instead of requests.

🚀 Quick Install

  1. Install Deno (required): https://docs.deno.com/runtime/getting_started/installation/

  2. Install langchain-sandbox:

    pip install langchain-sandbox

💡 Example Usage

Warning

Use allow_net to limit the network requests that can be made by the sandboxed code to avoid SSRF attacks https://docs.deno.com/runtime/fundamentals/security/#network-access

from langchain_sandbox import PyodideSandbox

# Create a sandbox instance
sandbox = PyodideSandbox(
    # Allow Pyodide to install python packages that
    # might be required.
    allow_net=True,
)
code = """\
import numpy as np
x = np.array([1, 2, 3])
print(x)
"""

# Execute Python code
print(await sandbox.execute(code))

# CodeExecutionResult(
#   result=None, 
#   stdout='[1 2 3]', 
#   stderr=None, 
#   status='success', 
#   execution_time=2.8578367233276367,
#   session_metadata={'created': '2025-05-15T21:26:37.204Z', 'lastModified': '2025-05-15T21:26:37.831Z', 'packages': ['numpy']},
#   session_bytes=None
# )

Stateful Sandbox

If you want to persist state between code executions (to persist variables, imports, and definitions, etc.), you can set stateful=True in the sandbox. This will return session_bytes and session_metadata that you can pass to .execute().

Warning

session_bytes contains pickled session state. It should not be unpickled and is only meant to be used by the sandbox itself

sandbox = PyodideSandbox(
    # Create stateful sandbox
    stateful=True,
    # Allow Pyodide to install python packages that
    # might be required.
    allow_net=True,
)
code = """\
import numpy as np
x = np.array([1, 2, 3])
print(x)
"""

result = await sandbox.execute(code)

# Pass previous result
print(await sandbox.execute("float(x[0])", session_bytes=result.session_bytes, session_metadata=result.session_metadata))

#  CodeExecutionResult(
#     result=1, 
#     stdout=None, 
#     stderr=None, 
#     status='success', 
#     execution_time=2.7027177810668945
#     session_metadata={'created': '2025-05-15T21:27:57.120Z', 'lastModified': '2025-05-15T21:28:00.061Z', 'packages': ['numpy', 'dill']},
#     session_bytes=b'\x80\x04\x95d\x01\x00..."
# )

Using as a tool

You can use PyodideSandbox as a LangChain tool:

from langchain_sandbox import PyodideSandboxTool

tool = PyodideSandboxTool()
result = await tool.ainvoke("print('Hello, world!')")

Using with an agent

You can use sandbox tools inside a LangGraph agent:

from langgraph.prebuilt import create_react_agent
from langchain_sandbox import PyodideSandboxTool

tool = PyodideSandboxTool(
    # Allow Pyodide to install python packages that
    # might be required.
    allow_net=True
)
agent = create_react_agent(
    "anthropic:claude-3-7-sonnet-latest",
    tools=[tool],
)
result = await agent.ainvoke(
    {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
)

Stateful Tool

Important

Stateful PyodideSandboxTool works only in LangGraph agents that use the prebuilt create_react_agent or ToolNode.

If you want to persist state between code executions (to persist variables, imports, and definitions, etc.), you need to set stateful=True:

from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState
from langgraph.checkpoint.memory import InMemorySaver
from langchain_sandbox import PyodideSandboxTool, PyodideSandbox

class State(AgentState):
    # important: add session_bytes & session_metadata keys to your graph state schema - 
    # these keys are required to store the session data between tool invocations.
    # `session_bytes` contains pickled session state. It should not be unpickled
    # and is only meant to be used by the sandbox itself
    session_bytes: bytes
    session_metadata: dict

tool = PyodideSandboxTool(
    # Create stateful sandbox
    stateful=True,
    # Allow Pyodide to install python packages that
    # might be required.
    allow_net=True
)
agent = create_react_agent(
    "anthropic:claude-3-7-sonnet-latest",
    tools=[tool],
    checkpointer=InMemorySaver(),
    state_schema=State
)
result = await agent.ainvoke(
    {
        "messages": [
            {"role": "user", "content": "what's 5 + 7? save result as 'a'"}
        ],
        "session_bytes": None,
        "session_metadata": None
    },
    config={"configurable": {"thread_id": "123"}},
)
second_result = await agent.ainvoke(
    {"messages": [{"role": "user", "content": "what's the sine of 'a'?"}]},
    config={"configurable": {"thread_id": "123"}},
)

See full examples here:

🧩 Components

The sandbox consists of two main components:

  • pyodide-sandbox-js: JavaScript/TypeScript module using Deno to provide the core sandboxing functionality.
  • sandbox-py: Contains PyodideSandbox which just wraps the JavaScript/TypeScript module and executes it as a subprocess.

Release History

VersionChangesUrgencyDate
langchain_sandbox==0.0.6## What's Changed * Add sync sandbox by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/25 * sandbox: add sync methods, add default timeout by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/27 * change to langchain scope by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/28 * release 0.0.6 by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/29 * Update js readme to reflect org scope by @eyurtsev in https://github.comLow5/21/2025
langchain_sandbox==0.0.5## What's Changed * allow returning session bytes & metadata directly by @vbarda in https://github.com/langchain-ai/langchain-sandbox/pull/12 * update json package info by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/19 * update readme by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/20 * patch: surface information about failing installs as part of stderr by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/21 * 0.0.5: release by @eyLow5/16/2025
langchain_sandbox==0.0.4## What's Changed * update README.md by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/3 * update readme clarification by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/4 * minor fix by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/6 * sandbox-py: add a code execution tool by @vbarda in https://github.com/langchain-ai/langchain-sandbox/pull/7 * add codeact example by @vbarda in https://github.com/langchain-ai/langchain-sandbox/pull/Low4/25/2025
langchain_sandbox==0.0.3## What's Changed * streamline perms by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/2 **Full Changelog**: https://github.com/langchain-ai/langchain-sandbox/compare/langchain_sandbox==0.0.2...langchain_sandbox==0.0.3Low4/15/2025
langchain_sandbox==0.0.2## What's Changed * Add default permissions to access node_modules, capture output from deno --version check by @eyurtsev in https://github.com/langchain-ai/langchain-sandbox/pull/1 ## New Contributors * @eyurtsev made their first contribution in https://github.com/langchain-ai/langchain-sandbox/pull/1 **Full Changelog**: https://github.com/langchain-ai/langchain-sandbox/compare/langchain_sandbox==0.0.1...langchain_sandbox==0.0.2Low4/15/2025
langchain_sandbox==0.0.1**Full Changelog**: https://github.com/langchain-ai/langchain-sandbox/commits/langchain_sandbox==0.0.1Low4/15/2025

Dependencies & License Audit

Loading dependencies...

Similar Packages

ai-project-templateStarter app for building AI SaaS (RAG, Agentic workflow) applicationsmaster@2026-05-01
capsuleA secure, durable runtime to sandbox AI agent tasks. Run untrusted code in isolated WebAssembly environments.v0.8.10
cohere-terrariumA simple Python sandbox for helpful LLM data agentsv1.0.1
YouTubeGPTYouTubeGPT is an LLM-based web-app that can be run locally and allows you to summarize and chat (Q&A) with YouTube videos.v3.3.1
multi-agent-orchestration-frameworkModular multi-agent orchestration framework powered by LangGraph and FastAPI.v0.1.0

More from langchain-ai

langchainThe agent engineering platform
langgraphBuild resilient language agents as graphs.
langgraphjsFramework to build resilient language agents as graphs.
openshell-deepagentA general-purpose coding agent that runs inside an NVIDIA OpenShell sandbox, orchestrated by Deep Agents and powered by NVIDIA Nemotron. The agent writes and executes code in an isolated, policy-gover

More in Security

clineAutonomous coding agent right in your IDE, capable of creating/editing files, executing commands, using the browser, and more with your permission every step of the way.
E2BOpen-source, secure environment with real-world tools for enterprise-grade agents.
OpenSandboxSecure, Fast, and Extensible Sandbox runtime for AI agents.
vm0the easiest way to run natural language-described workflows automatically