NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

MathCoder: Seamless Code Integration in LLMs for Enhanced Mathematical Reasoning

Ke Wang, Houxing Ren, Aojun Zhou, Zimu Lu, Sichun Luo, Weikang Shi, Renrui Zhang, Linqi Song, Mingjie Zhan, Hongsheng Li

The gap between closed-source giants like GPT-4 and the open-source community has long been a chasm, particularly in complex mathematical reasoning. While GPT-4’s Advanced Data Analysis (formerly Code Interpreter) can seamlessly weave together natural language and Python code to solve hard problems, open-source models have generally been stuck choosing between pure text reasoning or pure code generation.

A new paper, MathCoder, introduces a framework that finally brings this seamless integration to open-source models, allowing them to reason, code, and execute in a single, fluid loop. By fine-tuning on a novel "interpolated" dataset, the resulting MathCoder models achieve state-of-the-art performance, even outperforming GPT-4 on the challenging competition-level MATH dataset.

The Two Camps of Mathematical Reasoning

Historically, mathematical reasoning in LLMs falls into two distinct camps:

  1. Chain-of-Thought (CoT): The model explains its logic in natural language. This is great for setting up a problem but prone to arithmetic errors during calculation.
  2. Program-Aided/Program-of-Thought (PAL/PoT): The model writes a script to solve the problem. This is highly accurate for calculation but struggles with problems that require nuanced linguistic reasoning to formulate the equations.

The "gold standard" is a hybrid approach—reasoning, writing a bit of code, observing the output, and continuing to reason based on that output. Until now, there was no effective recipe to teach open-source models this iterative, tool-using behavior.

The MathCoder Framework

To bridge this gap, the authors developed a framework consisting of a novel data format, a unique dataset generation strategy, and customized fine-tuning.

1. The LCE (Language, Code, Execution) Format

Instead of generating a single block of text or code, MathCoder uses a "Natural Language, Code, and Execution" (LCE) format. The model generates a reasoning step, then writes a code block wrapped in special tokens (e.g., <|code|>). It then pauses for an external Python interpreter to provide the result (wrapped in <|execution|>), and continues its reasoning.

Comparison of CoT, PoT, and the proposed LCE format

This interleaving is strictly superior to code-only or text-only approaches. The model uses natural language to "plan" and code to "calculate," mirroring how a human might use a scratchpad alongside a calculator.

2. Problem Interpolation and Dataset Creation

A major challenge in math datasets is the "difficulty gap." GSM8K (grade school math) is often too easy, while MATH (high school competitions) is extremely hard. To bridge this, the authors used GPT-4 to generate interpolated problems—new questions designed to be harder than GSM8K but simpler than MATH.

The MathCoder dataset creation and fine-tuning pipeline

This process resulted in MathCodeInstruct, a dataset of 80,000 problems. It includes 49k "seed" problems from GSM8K and MATH (with GPT-4 generated LCE solutions) and 31k "interpolated" problems. Adding these intermediate-difficulty problems improved the model's average accuracy by 3.6 to 4.0 percentage points, proving the power of the "middle ground" in training data.

3. Customized Fine-Tuning

The models were fine-tuned using Supervised Fine-Tuning (SFT) on base models like Llama-2 and CodeLlama. Crucially, during training, the loss is only calculated on the language and code generated by the model. The execution results are masked out because the model shouldn't learn to predict the output of a script; it should learn to utilize the external tool's output.

Key Results: Small Models, Big Impact

MathCoder achieves state-of-the-art (SOTA) results among open-source models across multiple benchmarks:

  • MATH Dataset: 45.2% accuracy, surpassing previous open-source SOTA and even the base GPT-4 model.
  • GSM8K Dataset: 83.9% accuracy.
  • Generalization: MathCoder-L-70B outperformed ChatGPT-3.5 and PaLM-2 on both major benchmarks, as well as on out-of-domain datasets like SVAMP, Mathematics, and SimulEq.

One of the most surprising findings was the efficiency of the architecture. The smallest MathCoder (7B parameters) outperformed much larger models like WizardMath-70B. This strongly suggests that how a model is taught to reason (via the LCE format) is far more important than raw parameter count.

Limitations and Future Work

Despite these impressive gains, the authors note a few remaining hurdles:

  • Uni-modal Constraints: As a text-based model, MathCoder still struggles with complex geometry problems that require spatial reasoning or visual understanding.
  • Dependency on GPT-4: The initial dataset construction relies heavily on high-quality outputs from GPT-4, limiting the total independence of the training pipeline.
  • Theorem Proving: The model is optimized for calculation-heavy problems and is not yet designed for formal mathematical theorem proving.

Future work will likely explore multi-modal versions of MathCoder to tackle geometry and integrate more formal logic verification, pushing open-source mathematical reasoning even further.

Summary updated

BibTeX
@inproceedings{wang2024mathcoder,
  author = {Ke Wang and Houxing Ren and Aojun Zhou and Zimu Lu and Sichun Luo and Weikang Shi and Renrui Zhang and Linqi Song and Mingjie Zhan and Hongsheng Li},
  title = {{MathCoder: Seamless Code Integration in LLMs for Enhanced Mathematical Reasoning}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=z8TW0ttBPp},
}
Figure