{"id":6709,"library":"llm-sandbox","title":"LLM Sandbox","description":"LLM Sandbox is a lightweight and portable Python library designed to run large language model (LLM) generated code in a safe and isolated environment. It supports various container backends like Docker, Kubernetes, and Podman, and offers multi-language execution (Python, JavaScript, Java, C++, Go, R). The project sees frequent minor releases, addressing features, fixes, and security enhancements, and now supports the Model Context Protocol (MCP) server for direct AI assistant integration.","status":"active","version":"0.3.38","language":"en","source_language":"en","source_url":"https://github.com/vndee/llm-sandbox","tags":["llm","sandbox","code execution","security","ai","containerization","code interpreter","mcp"],"install":[{"cmd":"pip install llm-sandbox","lang":"bash","label":"Basic Installation"},{"cmd":"pip install 'llm-sandbox[docker]' # For Docker backend\npip install 'llm-sandbox[kubernetes]' # For Kubernetes backend\npip install 'llm-sandbox[podman]' # For Podman backend","lang":"bash","label":"With Specific Backend Support"}],"dependencies":[{"reason":"Required Python version.","package":"python","version":">=3.10, <4.0","optional":false},{"reason":"Core dependency for data validation and settings management.","package":"pydantic","optional":false},{"reason":"Required for Docker container backend.","package":"docker","optional":true},{"reason":"Required for Kubernetes container backend.","package":"kubernetes","optional":true},{"reason":"Required for Podman container backend.","package":"podman","optional":true}],"imports":[{"symbol":"SandboxSession","correct":"from llm_sandbox import SandboxSession"}],"quickstart":{"code":"import os\nfrom llm_sandbox import SandboxSession\n\n# Ensure Docker or another backend is running and accessible\n# For a simple test, ensure `llm-sandbox[docker]` is installed.\n\ntry:\n    with SandboxSession(lang=\"python\") as session:\n        print(\"Running Python code in sandbox...\")\n        result = session.run(\n            \"import math\\nprint(math.factorial(5))\"\n        )\n        print(f\"Sandbox Output: {result.stdout.strip()}\")\n        if result.stderr:\n            print(f\"Sandbox Error: {result.stderr.strip()}\")\n\n        print(\"\\nRunning Python code with a library (numpy)...\")\n        # Requires numpy to be installed in the sandbox environment, \n        # or configured for on-the-fly installation. The default image often includes it.\n        result_numpy = session.run(\n            \"import numpy as np\\nprint(np.mean([1, 2, 3, 4]))\", \n            libraries=[\"numpy\"]\n        )\n        print(f\"Sandbox NumPy Output: {result_numpy.stdout.strip()}\")\n        if result_numpy.stderr:\n            print(f\"Sandbox NumPy Error: {result_numpy.stderr.strip()}\")\n\nexcept Exception as e:\n    print(f\"An error occurred during sandbox execution: {e}\")\n    print(\"Ensure your container backend (e.g., Docker) is running and configured correctly.\")","lang":"python","description":"This quickstart demonstrates how to create a `SandboxSession` for Python, execute code, and optionally specify libraries. It includes error handling for common setup issues like the container backend not running."},"warnings":[{"fix":"Upgrade to `llm-sandbox>=0.3.35`.","message":"Older versions (prior to 0.3.35) contained command injection and path traversal vulnerabilities. Update immediately to version 0.3.35 or newer to secure your environment.","severity":"breaking","affected_versions":"<0.3.35"},{"fix":"Upgrade to `llm-sandbox>=0.3.32`. Carefully review environment configuration for pooled sessions and fast production modes.","message":"When using features like pooled sessions or `skip_environment_setup`, ensure consistent Python environments. Version 0.3.32 fixed an issue where pooled sessions didn't correctly use the venv Python, and 0.3.29 addressed `skip_environment_setup` using a non-existent venv.","severity":"gotcha","affected_versions":"<0.3.32"},{"fix":"Install `llm-sandbox` with the correct extra, e.g., `pip install 'llm-sandbox[docker]'`.","message":"To use Docker, Kubernetes, or Podman as a backend, you must install the corresponding optional dependencies (e.g., `pip install 'llm-sandbox[docker]'`) in addition to the base `llm-sandbox` package. Failure to do so will result in runtime errors when attempting to use specific backends.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Understand the lifecycle of `SandboxSession` and use features like `keep_template` or `SandboxPoolManager` for state management if persistence is required. Refer to documentation for advanced usage.","message":"By default, sandbox containers are destroyed after the `SandboxSession` closes, meaning any state (e.g., installed libraries, created files) is lost. To persist the container image state, ensure `keep_template=True` is set during session initialization, though the default `SandboxSession` might not directly expose this in all scenarios, requiring careful management of custom images or `SandboxPoolManager`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}