Overview
Code Interpreter provides:- Sandboxed Execution: Code runs in an isolated Docker container
- Data Visualization: Automatic handling of matplotlib plots and images
- Timeout Protection: Configurable execution timeouts
- Working Directory: Persistent workspace for file operations
- Pre-installed Libraries: Common data science packages included
Registration
code_interpreter
Parameters
The Python code to execute. Can be provided as a plain string or within a JSON object.
Parameter Schema
Configuration
When initializing the Code Interpreter, you can configure:Working directory for code execution and file storage. Defaults to
$DEFAULT_WORKSPACE/tools/code_interpreter.Maximum execution time in seconds. Code execution will be terminated if it exceeds this limit.
Environment Variables
Override the default working directory via environment variable.
Base URL for serving generated images (useful for web deployments).
Usage
Basic Usage
With Data Analysis
Using with Agents
Return Format
The tool returns results in markdown format:Standard Output
Images
Generated plots are automatically captured and returned:Errors
Timeout
Docker Image
The tool uses a Docker image namedcode-interpreter:latest which is automatically built from the included Dockerfile.
Required Dependencies (Host)
To check Docker availability:jupyter-client- For kernel communicationPillow- For image handling
Container Features
- Jupyter Kernel: IPython kernel for code execution
- Pre-installed Packages: numpy, pandas, matplotlib, seaborn, scikit-learn, scipy
- Chinese Font Support: Alibaba PuHuiTi font for matplotlib
- Isolated Environment: Separate container per agent instance
Advanced Features
Persistent Kernels
Kernels are reused across multiplecall() invocations within the same agent instance:
File Handling
Files passed tocall() are automatically copied to the working directory:
Timeout Mechanism
A countdown timer is injected to enforce timeouts:Error Handling
Best Practices
Security Considerations
Security Considerations
- Code executes in an isolated Docker container
- No network access by default (can be configured)
- Limited to the working directory
- Timeout protection prevents runaway processes
Performance Tips
Performance Tips
- Reuse the same CodeInterpreter instance for multiple calls
- Set appropriate timeouts based on expected execution time
- Docker image is built once and cached
- Kernel remains active between calls for faster execution
Resource Management
Resource Management
- Each CodeInterpreter instance creates one Docker container
- Containers are automatically cleaned up on instance deletion
- Use
work_dirto control disk usage - Monitor Docker resource consumption for production deployments
Example: Data Analysis Agent
Troubleshooting
Docker not found
Docker not found
Ensure Docker is installed:
Permission denied
Permission denied
Add your user to the docker group:
Image build fails
Image build fails
Check the Dockerfile at
qwen_agent/tools/resource/code_interpreter_image.dockerfile and ensure base images are accessible.Kernel won't start
Kernel won't start
Check Docker logs:
Related
Python Executor
Execute Python without Docker (less secure, faster)
ReAct Agent
Agent that can use Code Interpreter for reasoning