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

Recursive Visual Programming

Jiaxin Ge, Sanjay Subramanian, Baifeng Shi, Roei Herzig, Trevor Darrell

Abstract

Visual Question Answering (VQA) often requires complex multi-step reasoning that challenges standard Large Language Models (LLMs). Traditional Visual Programming (VP) approaches rely on monolithic code generation, which is prone to logic errors and brittle execution. We present Recursive Visual Programming (RVP), a framework that enables LLMs to decompose complex visual tasks into modular, recursive sub-problems. RVP improves accuracy, interpretability, and flexibility by mimicking human-like top-down problem-solving and employing dynamic type assignment.

Introduction

Visual Question Answering (VQA) involves interpreting visual data to answer natural language queries. A prominent strategy, Visual Programming (VP), utilizes an LLM as a planner to generate Python scripts that interface with vision APIs. However, existing VP methods typically generate solutions as single, monolithic code blocks. This "one-shot" approach mirrors writing a complex software system within a single function, often resulting in logic errors and poor handling of intricate visual details. RVP addresses these limitations by shifting toward a modular, recursive architecture.

Methodology: Recursive Decomposition

Recursive Visual Programming (RVP) replaces monolithic generation with a top-down, recursive strategy. The framework introduces a recursive_query(image, sub-question) API, allowing the LLM to delegate complex sub-tasks to new execution contexts. This mimics standard software engineering practices where a large problem is broken into manageable helper functions.

Recursive decomposition of a complex visual query into nested sub-problems.

When recursive_query is invoked during execution, it triggers a fresh LLM prompt focused solely on the sub-question. This process can recurse up to a depth of ten, enabling the model to isolate logical steps and reduce cognitive load. Furthermore, RVP employs Dynamic Type Assignment. Unlike previous systems with rigid return types, RVP allows sub-queries to return diverse data types such as bool, int, ImagePatch, or List[ImagePatch]. The model autonomously determines the appropriate type based on the sub-query's intent, allowing it to generalize to complex data structures even when they are absent from few-shot training examples.

Key Insights and Logic Correctness

The modularity of RVP serves as a reasoning scaffold. By isolating sub-problems, the LLM is less susceptible to the "noise" and context-window saturation of the broader task. This leads to significantly higher logic and type correctness compared to non-recursive baselines, which often return incorrect data types (e.g., returning a color string when a boolean is expected).

Comparison of recursive vs. non-recursive code generation, illustrating how recursion ensures logic and type correctness.

Beyond machine performance, RVP offers substantial human-centric benefits. In user studies, human programmers understood the high-level intent of RVP-generated code nearly twice as fast as monolithic code. The average line length of RVP functions was found to be 8.47, nearly half the length of non-recursive functions (15.4). This modularity also facilitates easier debugging; the LLM can more accurately identify the specific sub-function where a logic error occurred, rather than sifting through a single, dense script.

Evaluation and Results

RVP was evaluated across four benchmarks: GQA (real-world reasoning), VSR (spatial reasoning), COVR (multi-image reasoning), and NextQA (temporal video reasoning). RVP consistently outperformed the state-of-the-art non-recursive baseline, ViperGPT.

Notable results include:

  • Accuracy Gains: RVP showed a ~4.5% improvement in zero-shot performance on spatial reasoning tasks (VSR).
  • Interpretability: Participants identified code intent in 15-20 seconds for RVP, compared to over 30 seconds for standard VP.
  • Complex Reasoning: RVP was particularly effective at "Query" and "Choose" type questions in GQA, which require intricate logical branching.

Limitations and Future Work

While RVP enhances reasoning, it introduces trade-offs in cost and latency. The multiple LLM calls required for recursion result in approximately 3x higher costs and 2.1x longer execution times than monolithic methods. Additionally, over half of the remaining errors (52%) stem from underlying vision API limitations rather than the recursive logic itself. Future work should focus on more complex, multi-step reasoning tasks where recursion becomes a necessity and optimizing the cost of recursive calls.

Conclusion

Recursive Visual Programming demonstrates that top-down decomposition is a powerful tool for LLM-based visual reasoning. By shifting from monolithic scripts to modular, typed functions, RVP achieves superior performance and interpretability, providing a more robust framework for complex VQA tasks.

Summary updated

BibTeX
@inproceedings{ge2024recursive,
  author = {Jiaxin Ge and Sanjay Subramanian and Baifeng Shi and Roei Herzig and Trevor Darrell},
  title = {{Recursive Visual Programming}},
  booktitle = {European Conference on Computer Vision},
  year = {2024},
  doi = {10.1007/978-3-031-72775-7\_1},
}
Figure