TL;DR
Existing Visual Programming (VP) frameworks for Visual Question Answering (VQA) generate monolithic, single-function code blocks that struggle with complex logic and lack modularity. This paper introduces Recursive Visual Programming (RVP), a top-down approach where Large Language Models (LLMs) decompose complex visual queries into sub-problems via recursive API calls. By incorporating dynamic type assignment, RVP significantly improves both accuracy and code readability across multiple VQA benchmarks.
What Problem Does This Paper Address?
Visual Question Answering (VQA) requires AI systems to reason over visual data to answer natural language questions. Recently, Visual Programming (VP) has emerged as a state-of-the-art paradigm: instead of answering a question directly, an LLM acts as a planner, generating a Python program that orchestrates various specialized computer vision models via APIs.
However, current VP methods suffer from a major design flaw: they require the LLM to generate the entire program as a single, monolithic function block all at once. This forces the model to juggle high-level logic, low-level details, and API syntax simultaneously. As a result, standard VP methods frequently suffer from logic errors, struggle with long-context coherence, and produce code that is difficult for humans to interpret or debug.
Methods
Inspired by how human programmers break down complex software engineering tasks, the authors propose Recursive Visual Programming (RVP).
The core innovation is the introduction of a new API function: recursive_query(image, sub-question). Instead of writing a massive sequential script, the LLM generates a high-level function that delegates complex sub-tasks to recursive_query. During execution, each call to recursive_query triggers a fresh, context-specific query to the LLM, generating a new, modular sub-function. This process repeats recursively until the problem is broken down into atomic steps that can be solved by standard vision APIs (such as object detection or property verification).
To make this architecture viable, RVP introduces two critical mechanisms:
- Dynamic Type Assignment: Standard VP tools use static return types (e.g., always returning a string). RVP allows the LLM to explicitly request specific data types within the sub-query text (e.g., "Return a list of ImagePatch objects..."). The system dynamically parses this request to declare and return the correct data structure, even handling complex types unseen in the few-shot examples.
- Termination Conditions: To prevent infinite loops, the system enforces a maximum recursion depth of 10 and automatically switches to a direct, non-recursive query if a sub-function attempts to call itself with the exact same input question.
Datasets
The authors evaluated RVP across four distinct benchmarks testing different visual reasoning modalities:
- GQA: A dataset focused on compositional reasoning and multi-step questions over single real-world images.
- Visual Spatial Reasoning (VSR): A benchmark specifically evaluating spatial relations (e.g., "under", "left of") in text-image pairs.
- COVR: A dataset requiring compositional generalization and reasoning across multiple images simultaneously.
- NextQA (Hard Temporal subset): A video question-answering benchmark designed to test complex temporal and causal reasoning.
Key Insights and Ideas
- Recursion Unlocks LLM Reasoning: Preliminary tests on symbolic tasks (like the Games of 24) revealed that simply prompting an LLM to "solve the problem recursively" yields massive accuracy gains compared to non-recursive approaches. RVP successfully ports this software engineering paradigm into the multimodal domain.
- Modularity Prevents Context Drift: Monolithic code generation often suffers from "lost in the middle" phenomena, where the LLM forgets the original question's constraints by the end of the function. By isolating sub-problems into distinct function scopes, RVP forces local logical consistency.
Key Results
Quantitative Performance
RVP consistently outperformed the standard non-recursive baseline (ViperGPT) across all benchmarks using gpt-3.5-turbo:
- GQA (test-dev): Accuracy improved from 44.63% to 45.62%.
- COVR: Accuracy improved from 51.69% to 52.67%.
- NextQA: Accuracy improved from 47.21% to 48.82%.
The impact of Dynamic-Explicit Type Assignment was particularly striking. In ablation studies on the GQA dataset, explicitly stating the desired return type achieved an accuracy of 70.23%, compared to just 55.34% when the return type was left implicit.
Qualitative and Readability Gains
A human study involving 15 computer science students demonstrated that RVP drastically improves code clarity. Participants were able to comprehend the high-level intent of RVP-generated code significantly faster than monolithic baselines. Quantitatively, RVP reduced the average length of generated functions from 15.4 lines down to 8.47 lines, successfully shifting complexity away from single giant blocks into clean, modular sub-routines.
Limitations and Future Work
Despite its advantages, RVP introduces two main trade-offs:
- Cost and Latency: Because RVP relies on multiple sequential calls to the LLM rather than a single generation, it increases runtime by approximately 2.1x and API costs by roughly 3x compared to standard VP.
- Open-Source Model Gap: While commercial models like GPT-3.5 handled dynamic typing seamlessly, smaller open-source models like CodeLlama-13B struggled, exhibiting a 4.91% type error rate and failing to generalize to complex, unseen data types like
List[ImagePatch].
The authors note that current VQA benchmarks primarily feature short, simple questions. They suggest that future work should focus on developing more intricate, multi-layered visual reasoning datasets to fully exploit the deep recursion capabilities of RVP.
Summary updated