ShajinJoseph/code-interpreter
Code Interpreter
LibreChat-compatible code execution backend. Self-hosted Python + bash sandbox using smolagents LocalPythonExecutor (AST-restricted, in-process — no Docker-in-Docker), wrapped in a FastAPI app that speaks the same HTTP contract as code.librechat.ai.
The image bundles the tooling Anthropic's Agent Skills need at runtime so /exec never pays a package-install cost:
- LibreOffice (headless), poppler-utils, qpdf, ghostscript, tesseract-ocr, pandoc
- Node.js 20 LTS with
docx,pptxgenjs,pdf-lib,xlsxinstalled globally (NODE_PATHset, sorequire('docx')works from any cwd) - Python:
python-docx,python-pptx,openpyxl,pandas,pypdf,pdfplumber,pdf2image,pypdfium2,reportlab,mammoth,markitdown[pptx],pytesseract,pillow,matplotlib, plus smolagents
⚠️ No authentication. Anyone who can reach this service can run arbitrary code in the container. Run behind a reverse proxy with auth, on a private HF Space, or otherwise restrict network access.
Endpoints
Schema matches LibreChat's packages/agents CodeExecutor and api/server/services/Files/Code/crud.js:
// POST /exec request
{ "lang": "py", "code": "print(1+1)", "session_id": "...", "files": [{"id": "...", "session_id": "...", "name": "..."}] }
// POST /exec response
{ "stdout": "...", "stderr": "...", "session_id": "...", "files": [{"id": "...", "name": "..."}] }
// POST /upload response
{ "message": "success", "storage_session_id": "...", "files": [{"fileId": "...", "filename": "..."}] }Configuration
Deploy to a Hugging Face Space
- Create a new Space, SDK: Docker.
- Push these files to the Space repo.
- Point LibreChat's
LIBRECHAT_CODE_BASEURLathttps://<your-space>.hf.space. Restrict network access at the Space level (private Space, etc.) — the service ships with no auth of its own.
Local dev
docker build -t code-interpreter .
docker run -p 7860:7860 code-interpreter
# in another shell:
python test_local.pySecurity notes
LocalPythonExecutor is AST-restricted but not a hardened sandbox — the container itself is the trust boundary. Imports are unrestricted inside the container so the document skills work; if you need stricter isolation, swap the executor for DockerExecutor or E2BExecutor. There is no application- level authentication; do not expose the service to the open internet.
