Skip to main content
This example demonstrates how to create an environment where models can solve math problems by writing and executing Python code. The environment provides a sandboxed Python REPL with scientific computing libraries.

Overview

The Math Python environment combines:
  • Dataset: MATH competition problems (or custom math datasets)
  • Tools: Python REPL with numpy, sympy, scipy
  • Evaluation: Symbolic math verification using \boxed{} answer format
  • Sandbox: Isolated execution environment with configurable resources

Complete Implementation

Here’s the full working implementation from environments/math_python/math_python.py:

How It Works

1. Dataset Loading

The environment uses the load_example_dataset utility to load math problems:
Supported datasets:
  • "math" - MATH competition problems (training: 7,500 problems)
  • "math500" - MATH-500 benchmark (500 test problems)
  • "aime2024", "aime2025" - AIME competition problems
  • "gsm8k" - Grade school math (see GSM8K example)
Dataset format:

2. System Prompt

The system prompt instructs the model to:
  • Use Python for calculations
  • Format final answers using \boxed{} notation
  • Lists available packages (numpy, sympy, scipy by default)

3. Answer Parsing

The extract_boxed_answer function extracts content from LaTeX \boxed{} notation:

4. Math Verification

MathRubric provides symbolic math verification:
Features:
  • Symbolic equivalence checking (e.g., “1/2” equals “0.5”)
  • LaTeX expression normalization
  • Floating-point tolerance for numerical answers
  • Returns 1.0 for correct answers, 0.0 otherwise

5. Python Sandbox Environment

PythonEnv provides:
  • Isolated execution environment (Docker container)
  • Persistent Python REPL session
  • Pre-installed packages (numpy, sympy, scipy)
  • Configurable resources (CPU, memory, disk)
  • Automatic cleanup after rollouts

Example Interaction

User: What is the value of 32+42\sqrt{3^2 + 4^2}?Assistant: I’ll use Python to calculate this.
Tool Output: 5.0Assistant: The value is 5\boxed{5}Result: ✓ Correct (reward = 1.0)

Running the Environment

Installation

Quick Evaluation

Custom Configuration

Configuration Options

Key Features

Sandboxed Execution

  • Isolation: Each rollout gets a fresh sandbox container
  • Security: No access to host filesystem or network (by default)
  • Resource limits: Configurable CPU, memory, and disk quotas
  • Automatic cleanup: Containers are destroyed after rollouts

Package Management

Customize available packages:
Or restrict to standard library only:

Multi-Turn Interaction

The environment supports iterative problem-solving:
  1. Model writes Python code
  2. Code executes in sandbox
  3. Model sees output and continues reasoning
  4. Repeats until model provides final answer or hits max_turns

Metrics Tracked

  • correct_answer: 1.0 if answer matches ground truth, 0.0 otherwise
  • num_turns: Number of model-environment interactions
  • sandbox_ready_wait_time: Time to initialize sandbox (seconds)
  • sandbox_command_execution_time: Total time executing Python code
  • python_ready_wait_time: Time to start Python REPL

Advanced Usage

Custom Answer Extraction

Provide your own answer extraction logic:

Custom Reward Functions

Add additional reward signals:
  • GSM8K - Single-turn math reasoning without code execution
  • Wiki Search - Tool environment with custom tools
  • Browser Examples - More complex stateful environments

Next Steps

  • Learn about Environments to understand the architecture
  • See Sandboxes for more on containerized execution
  • Explore Rubrics for custom evaluation logic