PythonEnv
Sandbox-backed environment exposing a persistent Python REPL for code execution.Overview
PythonEnv provides a stateful Python interpreter running in a sandboxed container:
- Persistent state: Variables and imports persist across code executions
- IPython-like behavior: Trailing expressions are automatically printed
- Package management: Pre-install packages via pip during setup
- Error handling: Full traceback capture for debugging
- Execution tracking: Numbered outputs like Jupyter notebooks
Inheritance
Constructor
Parameters
str
default:"numpy sympy scipy"
Space-separated list of packages to install with pip during sandbox startup. Use empty string to skip installation.
int
default:"30"
Maximum time to wait for the Python worker to be ready.
sandbox_name,docker_image,cpu_cores,memory_gb, etc.timeout_per_command_seconds- Timeout for each Python execution
Tools
python
str
Python code to execute. Can be multiple lines.
str - Formatted output including stdout, stderr, and expression results.
Output format:
- stdout content (if any)
- stderr content prefixed with “stderr:” (if any)
- Exception tracebacks (on error)
- Expression results as
Out[N]: <repr>(for trailing expressions) (no output)if nothing was produced
State Management
PythonEnv adds Python-specific state to the base sandbox state:
Built-in Rubric
PythonEnv includes PythonMonitorRubric which tracks:
python_ready_wait_time: Time spent waiting for Python worker initialization- All metrics from SandboxEnv
Example Usage
Basic Math Environment
Scientific Computing
Data Analysis
Code Verification with Tests
Custom Package Installation
REPL Behavior
The Python REPL behaves like IPython:Implementation Details
Worker Process
PythonEnv runs a background Python worker process in the sandbox that:
- Creates named pipes (FIFOs) for bidirectional communication
- Maintains a persistent namespace across executions
- Executes code using
exec()for statements andeval()for trailing expressions - Captures stdout/stderr using
contextlib.redirect_stdout/stderr - Returns results as JSON over the response pipe
Startup Sequence
- Sandbox container starts with Python 3.11
- Packages are installed via pip (if specified)
- Worker script is uploaded and launched as background process
- Worker creates communication pipes and sets ready flag
- First
python()call waits for ready flag before executing - Subsequent calls execute immediately
Error Types
PythonWorkerNotReadyError: Worker failed to start within timeoutPythonWorkerRequestError: Communication error with workerPythonWorkerDeadError: Worker process died unexpectedly
vf.SandboxError.
When to Use
UsePythonEnv for:
- Math and scientific computing tasks
- Code generation with execution verification
- Multi-step computations requiring persistent state
- Algorithm development and testing
- Data analysis workflows
- Bash commands and system operations
- Multi-language environments
- File system operations
- Custom execution environments
- Stateless Python function calls
- Tasks not requiring code execution
- Pre-defined operations only
See Also
- SandboxEnv - Base sandbox environment
- StatefulToolEnv - Stateful tool pattern
- ToolEnv - Stateless tools