SANDBOX | A Flexible Code Executor for Dify
Project Manifesto
Introduction
DIFY-SANDBOX-PY is a custom code executor tailored for Dify—the open-source LLM application development platform—designed to maintain full compatibility with Dify’s official sandbox API while unlocking greater flexibility for personal workflows. Built to support Python 3.12 and Node.js 20, this tool addresses a key pain point of the official Dify sandbox: restrictive permissions and limited dependency support that often cause frustrating errors for data analysis tasks.
While the official Dify sandbox excels at security (with strict permission controls to prevent code injection), it falls short for personal use cases where you fully own and edit Dify’s code nodes. DIFY-SANDBOX-PY fills this gap by offering broader permissions and out-of-the-box support for data science libraries (like numpy>2.0
, matplotlib
, and scikit-learn
), eliminating confusing dependency-related errors and streamlining data-focused workflows.
Developed by referencing Dify’s official sandbox API specifications, this project prioritizes compatibility while adding features that make Dify more powerful for tasks like data visualization, numerical analysis, and script execution.
Source Code: https://github.com/Wunrry/dify-sandbox
🌟 Key Features
DIFY-SANDBOX-PY builds on the official sandbox’s core functionality with targeted improvements for usability and data work:
🐍 Python & Node.js Support
- Natively supports Python 3.12 and Node.js 20—aligning with modern runtime standards for stability and performance.
- Seamlessly integrates with Dify’s code nodes, requiring no changes to your existing Dify workflows (beyond updating the sandbox image).
📊 Data Analysis Ready
- Pre-installs essential data science libraries by default, saving you time on manual setup:
numpy
(for numerical computing)pandas
(for data manipulation)matplotlib
(for data visualization)
- Eliminates “missing dependency” errors that often disrupt data-focused Dify projects.
🎨 New Plotting Endpoint
- Introduces a dedicated
/v1/sandbox/plot
endpoint to generate visualizations directly from JSON input. - Send a POST request with structured JSON (e.g., x/y data, titles, labels), and the endpoint returns a PNG image of your plot.
Test Example JSON:{ "x": [1, 2, 3, 4, 5], "y": [10, 20, 25, 30, 40], "title": "Sample Plot", "xlabel": "X Axis", "ylabel": "Y Axis" }
⚡ Blazing-Fast Dependency Installation
- Uses UV (a modern Python dependency manager) instead of
pip
, enabling millisecond-level dependency installs when restarting the sandbox. - This is a massive upgrade over traditional
pip
installs, which can take minutes for large libraries.
🌐 Unrestricted Network Access
- Removes the official sandbox’s network restrictions by default.
- Perfect for tasks like fetching data from APIs, downloading datasets, or integrating with external services directly from Dify code nodes.
🧩 Easy Dependency Management
- Follow Dify’s familiar workflow for adding custom dependencies:
- Simply list required packages in
/docker/volumes/sandbox/dependencies/python-requirements.txt
and restart the sandbox—UV handles the rest.
🛠 Tech Stack
DIFY-SANDBOX-PY uses a lightweight, high-performance stack to ensure compatibility and speed:
Component | Purpose |
---|---|
FastAPI | Powers the sandbox API (including the new /v1/sandbox/plot endpoint) |
Python 3.12 | Runtime for Python code execution and data science tasks |
Node.js 20 | Runtime for Node.js code execution |
UV | Python dependency manager (fast installs, minimal overhead) |
Matplotlib | Renders plots for the /v1/sandbox/plot endpoint |
Numpy/Pandas | Pre-installed data science core libraries |
Docker | Containerization for easy deployment and consistency across environments |
🚀 Usage Guide
Getting started with DIFY-SANDBOX-PY is straightforward—just update your Dify docker-compose.yaml
to use the pre-built image or build it yourself.
Option 1: Use the Pre-Built Docker Image
- Open your Dify project’s
docker-compose.yaml
file. - Locate the
sandbox
service section and replace the official image with the DIFY-SANDBOX-PY image:sandbox: # Replace the official image with this line image: svcvit/dify-sandbox-py:0.1.2 # Keep other existing configs (ports, volumes, etc.) unchanged
- Restart your Dify services with:
docker-compose down && docker-compose up -d
Option 2: Build the Image Locally
If you want to customize the sandbox further, build the image from source:
- Clone the DIFY-SANDBOX-PY repository (replace with your repo URL if public):
git clone <your-repository-url> dify-sandbox-py cd dify-sandbox-py
- Build the Docker image:
docker build -t dify-sandbox-py:local .
- Update
docker-compose.yaml
to use your local image:sandbox: image: dify-sandbox-py:local
- Restart Dify services as before.
⚠️ Important Notes
- Personal Use Focus: This sandbox is intended for scenarios where you fully control and edit Dify’s code nodes. It disables some security restrictions (e.g., network access), so avoid using it with untrusted code.
- Core Dependencies Only: The base image includes only FastAPI and runtime essentials. Any additional libraries (e.g.,
scikit-learn
,tensorflow
) must be added topython-requirements.txt
. - UV vs. Pip: UV is faster, but if you need
pip
-specific flags, you can modify the Dockerfile to override the dependency manager.
🎯 Vision & Goals
The goal of DIFY-SANDBOX-PY is to make Dify a more powerful tool for data practitioners and hobbyists. By removing unnecessary restrictions and pre-configuring data science tools, we want to:
- Eliminate the friction of dependency management in Dify code nodes.
- Enable seamless data visualization and analysis directly within Dify workflows.
- Provide a drop-in alternative to the official sandbox that prioritizes flexibility for personal projects.
Ultimately, this project aims to help users focus on building great Dify applications—without getting stuck on permission errors or missing libraries.
🤝 Contributing & Support
DIFY-SANDBOX-PY is an open-source project, and contributions are welcome! Whether you want to:
- Add support for more libraries (e.g.,
seaborn
,plotly
), - Improve the
/v1/sandbox/plot
endpoint (e.g., add chart types), - Fix bugs or optimize performance,
feel free to fork the repository, submit a pull request, or open an issue.
If you find this tool useful, consider giving it a ⭐ on GitHub (link to your repo here) to help others discover it. For questions or troubleshooting, reach out via the repository’s issue tracker.
Built with ❤️ for Dify users who want more flexibility in their code workflows.