{"id":27774319,"url":"https://github.com/langchain-ai/langchain-sandbox","last_synced_at":"2025-05-04T09:18:01.246Z","repository":{"id":288118549,"uuid":"966871416","full_name":"langchain-ai/langchain-sandbox","owner":"langchain-ai","description":null,"archived":false,"fork":false,"pushed_at":"2025-04-25T19:26:01.000Z","size":202,"stargazers_count":10,"open_issues_count":3,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-04T09:17:56.985Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/langchain-ai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-15T15:21:02.000Z","updated_at":"2025-05-01T19:27:11.000Z","dependencies_parsed_at":"2025-04-15T17:26:50.131Z","dependency_job_id":"07d4c521-c260-4f0d-8fb8-c17cc80b7a0a","html_url":"https://github.com/langchain-ai/langchain-sandbox","commit_stats":null,"previous_names":["langchain-ai/langchain-sandbox"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flangchain-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/langchain-ai","download_url":"https://codeload.github.com/langchain-ai/langchain-sandbox/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252310988,"owners_count":21727521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-30T02:52:13.676Z","updated_at":"2025-05-04T09:18:01.188Z","avatar_url":"https://github.com/langchain-ai.png","language":"Python","funding_links":[],"categories":["Python","Sandboxing \u0026 Isolation"],"sub_categories":[],"readme":"# 🛡️ LangChain Sandbox\n\n\u003e A secure environment for running Python code using Pyodide (WebAssembly) and Deno\n\n[![Python 3.10+](https://img.shields.io/badge/Python-3.10+-blue.svg)](https://www.python.org/downloads/)\n[![Deno](https://img.shields.io/badge/Deno-Required-green.svg)](https://deno.land/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n## 📋 Overview\n\nLangChain 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.\n\n## ✨ Key Features\n\n- **🔒 Security** - Isolated execution environment with configurable permissions\n- **💻 Local Execution** - No remote execution or Docker containers needed\n- **🔄 Session Support** - Maintain state across multiple code executions\n\n## Limitations\n\n- **Latency**: There is a few seconds of latency when starting the sandbox per run\n- **File access**: Currently not supported. You will not be able to access the files written by the sandbox.\n- **Network requests**: If you need to make network requests please use `httpx.AsyncClient` instead of `requests`.\n\n## 🚀 Quick Install\n\n1. Install Deno (required): https://docs.deno.com/runtime/getting_started/installation/\n\n2. Install `langchain-sandbox`:\n    \n    ```bash\n    pip install langchain-sandbox\n    ```\n\n## 💡 Example Usage\n\n```python\nfrom langchain_sandbox import PyodideSandbox\n\n# Create a sandbox instance\nsandbox = PyodideSandbox(\n   \"./sessions\", # Directory to store session files\n   # Allow Pyodide to install python packages that\n   # might be required.\n   allow_net=True,\n)\ncode = \"\"\"\\\nimport numpy as np\nx = np.array([1, 2, 3])\nprint(x)\n\"\"\"\n\n# Execute Python code\nprint(await sandbox.execute(code, session_id=\"123\"))\n\n# CodeExecutionResult(\n#   result=None, \n#   stdout='[1 2 3]', \n#   stderr=None, \n#   status='success', \n#   execution_time=2.8578367233276367\n# )\n\n# Can still access a previous result!\nprint(await sandbox.execute(\"float(x[0])\", session_id=\"123\"))\n\n#  CodeExecutionResult(\n#     result=1, \n#     stdout=None, \n#     stderr=None, \n#     status='success', \n#     execution_time=2.7027177810668945\n# )\n```\n\n### Using as a tool\n\nYou can use `PyodideSandbox` as a LangChain tool:\n\n```python\nfrom langchain_sandbox import PyodideSandboxTool\n\ntool = PyodideSandboxTool()\nresult = await tool.ainvoke(\"print('Hello, world!')\")\n```\n\nIf you want to persist state between code executions (to persist variables, imports,\nand definitions, etc.), you need to invoke the tool with `thread_id` in the config:\n\n```python\ncode = \"\"\"\\\nimport numpy as np\nx = np.array([1, 2, 3])\nprint(x)\n\"\"\"\nresult = await tool.ainvoke(\n    code,\n    config={\"configurable\": {\"thread_id\": \"123\"}},\n)\n\nsecond_result = await tool.ainvoke(\n    \"print(float(x[0]))\",  # tool is aware of the previous result\n    config={\"configurable\": {\"thread_id\": \"123\"}},\n)\n```\n\n### Using with an agent\n\nYou can use `PyodideSandboxTool` inside a LangGraph agent. If you are using this tool inside an agent, you can invoke the agent with a config, and it will automatically be passed to the tool:\n\n```python\nfrom langgraph.prebuilt import create_react_agent\nfrom langgraph.checkpoint.memory import InMemorySaver\nfrom langchain_sandbox import PyodideSandboxTool\n\ntool = PyodideSandboxTool()\nagent = create_react_agent(\n    \"anthropic:claude-3-7-sonnet-latest\",\n    tools=[tool],\n    checkpointer=InMemorySaver()\n)\nresult = await agent.ainvoke(\n    {\"messages\": [{\"role\": \"user\", \"content\": \"what's 5 + 7?\"}]},\n    config={\"configurable\": {\"thread_id\": \"123\"}},\n)\nsecond_result = await agent.ainvoke(\n    {\"messages\": [{\"role\": \"user\", \"content\": \"what's the sine of that?\"}]},\n    config={\"configurable\": {\"thread_id\": \"123\"}},\n)\n```\n\nSee full examples here:\n\n* [ReAct agent](examples/react_agent.py)\n* [CodeAct agent](examples/codeact_agent.py)\n\n## 🧩 Components\n\nThe sandbox consists of two main components:\n\n- **`pyodide-sandbox-js`**: JavaScript/TypeScript module using Deno to provide the core sandboxing functionality.\n- **`sandbox-py`**: Contains `PyodideSandbox` which just wraps the JavaScript/TypeScript module and executes it as a subprocess.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flangchain-ai%2Flangchain-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flangchain-ai%2Flangchain-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flangchain-ai%2Flangchain-sandbox/lists"}