This document explains how to integrate the Code Executor API with Open-WebUI to enable code execution capabilities within your AI chat interface.
The integration allows Open-WebUI to execute code in five languages (Python, Node.js, Ruby, PHP, and Go) through sandboxed Docker containers, providing a safe environment for running untrusted code directly from chat conversations.
┌─────────────┐ ┌──────────────┐ ┌─────────────────┐
│ Open-WebUI │──────▶│ Code │──────▶│ Docker │
│ (Browser) │ HTTP │ Interpreter │ Docker│ Containers │
│ │ │ API │ API │ (Sandboxed) │
└─────────────┘ └──────────────┘ └─────────────────┘
- Docker and Docker Compose
- Open-WebUI running (locally or in Docker)
- Node.js 20+ (for local development)
# Using Make
make up
# Or using Docker Compose
docker compose --profile runners up -dThe API will be available at http://localhost:8080
- Download the tool export from the releases or use the one in this repository
- In Open-WebUI, go to Settings → Workspace → Tools
- Click Import and upload the JSON file
- Update the API URL if needed (see Docker Networking section)
- In Open-WebUI, go to Settings → Workspace → Tools
- Click New Tool
- Copy the contents of:
openwebui_tool.pyfor standard setupopenwebui_tool_docker.pyif Open-WebUI is running in Docker (recommended)
- Save the tool with a descriptive name
The API URL depends on where Open-WebUI is running:
| Open-WebUI Location | API URL |
|---|---|
| Local browser (not Docker) | http://localhost:8080/v1/runs |
| Docker container | http://host.docker.internal:8080/v1/runs |
| Same Docker network | http://code-executor-api:8080/v1/runs |
Important: If Open-WebUI is in Docker, you have two options:
- Use
openwebui_tool_docker.pywhich hashost.docker.internalpre-configured (recommended) - Use
openwebui_tool.pyand change theapi_urlvalve tohttp://host.docker.internal:8080/v1/runs
code-executor/
├── api/ # Main API implementation
│ └── src/
│ └── index.ts # Enhanced with CORS, OpenAPI spec
├── openwebui_tool.py # Open-WebUI tool implementation
├── openwebui_tool_docker.py # Docker-specific version with host.docker.internal
├── test_tool.py # Standalone test script
└── docs/
└── OPENWEBUI_INTEGRATION.md # This file
Added endpoints and features for Open-WebUI compatibility:
- CORS support: Allows browser-based requests
/openapi.json: Serves OpenAPI specification/modelsendpoints: Compatibility with OpenAI clients- Better path resolution: Works in different environments
The main Open-WebUI tool implementation with:
- Class-based structure: Uses
Toolsclass as required by Open-WebUI - 6 functions:
execute_code,run_python,run_javascript,run_ruby,run_php,run_go - Error handling: Graceful failures with emoji indicators
- Configurable endpoints: Via
Valvesconfiguration
Standalone Python script for testing the API without Open-WebUI:
python3 test_tool.py- Start a new chat
- Look for the tools/functions selector (usually a 🧩 icon)
- Select "Code Executor" from available tools
- The tool is now active for that chat session
"Run this Python code: print('Hello, World!')"
"Execute this Ruby script and tell me the output:
def factorial(n)
return 1 if n <= 1
n * factorial(n - 1)
end
puts factorial(10)"
"Can you run JavaScript code to generate 10 random numbers?"
"Execute PHP code to show the current date and time"
"Run Go code to print hello world"
| Function | Language | Description |
|---|---|---|
run_python(code) |
Python 3.11 | Execute Python code |
run_javascript(code) |
Node.js 20 | Execute JavaScript code |
run_ruby(code) |
Ruby 3.x | Execute Ruby code |
run_php(code) |
PHP 8.x | Execute PHP code |
run_go(code) |
Go 1.21 | Execute Go code (compilation + execution) |
execute_code(code, language) |
Any | Generic execution with language parameter |
- Sandboxed execution: Each code run is in an isolated Docker container
- Resource limits: CPU, memory, and time limits enforced
- Network isolation: Containers have no network access
- Read-only filesystem: Prevents system modifications
- Non-root execution: Runs as unprivileged user
| Resource | Limit |
|---|---|
| Timeout | 5 seconds (API), 60 seconds (Tool) |
| Memory | 256 MB |
| CPU | 5000ms |
| Output size | 1 MB |
| Artifacts | 5 MB total |
Go programs require compilation before execution, which adds approximately 3-4 seconds to the total execution time. The Docker container uses golang:1.21-alpine and handles:
- Compilation with optimization flags (
-ldflags -s -w) - Increased process limits (256 pids) for concurrent compilation
- Proper memory management for ARM64 architectures
Cause: Docker networking issue
Solution: Change localhost to host.docker.internal in tool configuration
Cause: Code took longer than 60 seconds (tool timeout) or 5 seconds (API timeout) Note: Go programs require compilation which takes 3-4 seconds Solution: Optimize code or increase timeout in API configuration
Cause: Tool not properly saved or selected Solution:
- Verify tool is saved in Workspace → Tools
- Check if tool is enabled for current chat
- Try refreshing Open-WebUI
Cause: Incorrect API key Solution: Ensure API key matches between tool config and API environment
# API logs
docker logs code-executor-api-1 --tail 50
# Open-WebUI logs (if in Docker)
docker logs open-webui --tail 50| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
/v1/runs |
POST | Execute code | Yes (Bearer token) |
/v1/health |
GET | Health check | No |
/openapi.json |
GET | OpenAPI spec | No |
/models |
GET | Model list (compatibility) | No |
| Variable | Default | Description |
|---|---|---|
PORT |
8080 | API port |
API_KEYS |
dev_123:default:5:10 | API keys with rate limits |
SANDBOX_WORKDIR |
/sandbox | Container working directory |
PUBLIC_BASE_URL |
http://localhost:8080 | Public URL for API |
# Test API directly
curl -X POST http://localhost:8080/v1/runs \
-H "Authorization: Bearer dev_123" \
-H "Content-Type: application/json" \
-d '{"language": "python", "code": "print(1+1)"}'
# Test with Python script
python3 test_tool.py# Stop services
docker compose --profile runners down
# Rebuild API
docker compose --profile runners build api
# Start services
docker compose --profile runners up -d- Go to Tools in Open-WebUI
- Click the export button on your tool
- Save the JSON file
- Go to Tools in Open-WebUI
- Click Import
- Select the JSON file
- Update configuration as needed
The export includes:
- Complete tool code
- Function specifications
- Metadata (name, description, author)
- Configuration (endpoints, auth)
To contribute improvements:
- Test your changes locally
- Update this documentation if needed
- Include the tool export for easy sharing
- Submit a pull request
This integration is part of the Code Executor API project.