CodeDance: A Dynamic Tool-integrated MLLM for Executable Visual Reasoning

Qi Song, Honglin Li, Yingchen Yu, Haoyi Zhou, Lin Yang, Song Bai, Qi She, Zilong Huang, Yunqing Zhao

TL;DR

CodeDance is an open-source Multimodal Large Language Model (MLLM) framework that uses executable Python code as a dynamic, unified medium for visual reasoning. By introducing a difficulty-adaptive reinforcement learning reward system, CodeDance successfully balances tool use—avoiding both tool underuse on complex tasks and tool overuse on simple ones. This approach allows a 7B model to achieve state-of-the-art results, even outperforming closed-source giants like GPT-4o on several visual reasoning benchmarks.

What Problem Does This Paper Address?

While modern MLLMs excel at basic visual tasks, complex visual reasoning (like advanced math, chart analysis, or precise counting) remains a challenge. Traditional Chain-of-Thought (CoT) approaches rely on static text templates or rigid, predefined visual schemas (such as predicting fixed bounding box coordinates). This creates an information bottleneck: models cannot dynamically interact with images, refine intermediate steps, or validate their reasoning against visual evidence.

Furthermore, existing models suffer from two extremes: tool underuse (failing to call tools on hard tasks) and tool overuse (wasting compute by calling tools on trivial tasks). While proprietary systems like OpenAI's o3 demonstrate powerful "thinking with images" capabilities, they remain closed-source black boxes. CodeDance addresses these gaps by providing an open, verifiable, and task-adaptive framework for executable visual reasoning.

Figure 1. Motivation and effectiveness of dynamic tool invocation in CodeDance. Left: qualitative examples show that both tool underuse (unable to invoke tools on challenging tasks) and tool overuse (redundant calls on easy tasks) lead to hallucinated reasoning, incorrect answers, and unnecessary complexity (more reasoning turns and longer rollout time), whereas CodeDance dynamically invokes tools according to task difficulty to obtain correct solutions. Right: quantitative results show CodeDance-7B consistently surpasses all Qwen2.5- VL 7B baselines and exceeds the 32B version on several tasks.

Methods

CodeDance implements a continuous think-execute-feedback loop. Instead of relying on rigid API schemas, the model writes standard Python code (using libraries like PIL or OpenCV) to manipulate images, perform mathematical computations, or render visual artifacts. This code is executed in a safe sandbox, and the visual or textual feedback is fed back into the model's context window for the next reasoning turn.

The training process consists of two main stages:

  1. Supervised Fine-Tuning (SFT): A cold-start phase where the model learns atomic capabilities such as cropping, drawing, and pointing.
  2. Reinforcement Learning (RL): The authors build upon Group Relative Policy Optimization (GRPO) and introduce a novel Reward for Balanced Adaptive Tool Calling (RBAT). RBAT features a sequence-level adaptive reward that scales based on group accuracy (task difficulty)—incentivizing tool use for hard problems and discouraging it for easy ones—alongside turn-level penalties to discourage broken or failing code.
Figure 2. Overview. CodeDance enables executable visual reasoning and leverages tool calling adaptively based on task difficulty.

Datasets

The authors curated a 34K high-quality SFT trajectory dataset featuring varying difficulty levels. The dataset covers fundamental image transforms (crop, resize), mathematical computations (measurement, algebra), and open-ended visual editing (drawing, annotation). Public resources (such as SA1B, GEOqa plus, and MMK12) were automatically filtered and stratified by difficulty using Qwen2.5-VL-7B and cross-validated by Qwen2.5-VL-72B.

For evaluation, CodeDance was tested across more than 10 multimodal benchmarks spanning:

  • Visual Counting: CountBench, PixmoCount
  • Visual Search: V* Bench, HR-Bench (4K & 8K)
  • General/Chart QA: ChartQA, CharXiv
  • Math Reasoning: MathVision, MathVista, MathVerse, WeMath

Key Insights and Ideas

The most exciting finding is the emergence of novel behaviors during RL training that were never explicitly taught in the SFT data:

  • Cross-Domain Tool Transfer: Visual operations defined for a specific task were spontaneously repurposed. For example, the model adapted bounding-box tools (originally trained for chart analysis) to localize and verify objects during complex counting tasks.
  • Spontaneous Tool Composition: CodeDance learned to chain independent atomic operations together to solve complex problems, such as applying a pointing tool to verify an object's position, followed immediately by a crop-and-zoom operation for fine-grained inspection.
  • Activation of Latent Pretrained Knowledge: Driven purely by adaptive rewards, the model began writing novel procedural code (like using OpenCV's cv2.rectangle to overlay a grid for counting) that was entirely absent from its fine-tuning data.

Key Results

CodeDance-7B consistently delivered massive performance leaps over its base model, Qwen2.5-VL-7B, and frequently surpassed much larger models:

  • Counting & Search: It achieved a +19.2% improvement on CountBench and a +53.0% boost on PixmoCount compared to the 7B baseline.
  • Beating Larger Models: CodeDance-7B outperformed the 32B version of Qwen2.5-VL on multiple benchmarks, including CountBench and PixmoCount.
  • Surpassing Closed-Source Models: CodeDance-7B outperformed GPT-4o on CountBench (91.2 vs 87.9) and V* Bench (84.8 vs 67.5), demonstrating the raw efficiency of delegating fine-grained visual analysis to executable code.

Limitations and Future Work

A dedicated limitations section is not extensively detailed in the paper. However, the authors hint at several promising future directions, including expanding the diversity of the integrated tool libraries and exploring even longer reasoning horizons during inference (beyond the evaluated 10-turn budget) to tackle even more open-ended, real-world agentic workflows.

Summary updated

BibTeX
@inproceedings{song2026codedance,
  author = {Qi Song and Honglin Li and Yingchen Yu and Haoyi Zhou and Lin Yang and Song Bai and Qi She and Zilong Huang and Yunqing Zhao},
  title = {{CodeDance: A Dynamic Tool-integrated MLLM for Executable Visual Reasoning}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2026},
  url = {https://openreview.net/forum?id=s0nQjQW8Um},
}
Figure