TL;DR
PyVision is an agentic, multi-turn visual reasoning framework that enables Multimodal Large Language Models (MLLMs) to write, execute, and iteratively refine Python code to act as custom visual tools on the fly. Rather than relying on static, predefined toolkits, PyVision uses Python as a universal tool-creation interface, yielding massive improvements on challenging visual tasks—including a +31.1% boost for Claude-4.0-Sonnet on symbolic vision puzzles and a +7.8% boost for GPT-4.1 on fine-grained visual search.
What Problem Does This Paper Address?
Traditional tool-augmented vision systems (e.g., ViperGPT, VisProg) typically orchestrate predefined, static libraries containing specialized models like Grounding DINO or SAM. These fixed toolsets introduce severe bottlenecks: they lack generality across unseen tasks, restrict workflows to single-turn interactions, and fail when tasks demand bespoke visual manipulations.
Furthermore, even state-of-the-art MLLMs often rely on superficial visual heuristics—such as misapplying memorized templates to modified visual illusions or failing basic geometry counting because they cannot inspect pixel distributions. The authors investigate whether modern MLLMs can move beyond simply using tools to dynamically inventing them via multi-turn code generation.
Methods
PyVision introduces an interactive multi-turn loop between the MLLM and an isolated Python runtime sandbox:
- Python as the Sole Primitive Tool: Instead of invoking external vision models, PyVision supplies the standard scientific Python ecosystem (NumPy, OpenCV, SciPy, Pillow, Scikit-image, Matplotlib).
- Multi-Turn Execution & State Persistence: The MLLM writes code inside
<code>tags. Variables and state persist across turns, enabling iterative problem-solving (e.g., executing a coarse crop, inspecting visual feedback, adjusting coordinates, and applying threshold filters). Text outputs (print()) and plotted figures (plt.show()) are injected directly back into the conversational context as multimodal clues (mm_clue_i). - Process Isolation & Structured I/O: Each code snippet executes inside dynamically spawned subprocesses to prevent state pollution or crashes from terminating the session, while passing variables through structured communication rather than raw filesystem writes.
- Tooling Taxonomy: By clustering embeddings of generated snippets, the authors categorize dynamic tools into:
- Basic Image Processing (cropping, rotation, contrast adjustment)
- Advanced Image Processing (Otsu segmentation, edge detection, OCR)
- Visual Prompting & Sketching (drawing markers or auxiliary trajectory lines)
- Numerical & Statistical Analysis (pixel histograms, area/perimeter calculations)
- Long-Tail Operations (e.g., direct pixel-subtraction difference maps)
Datasets
The authors evaluated PyVision across six core benchmarks covering varied cognitive tasks:
- MathVista & MathVision-mini: Multimodal mathematical and quantitative reasoning.
- MMMU: College-level multi-discipline multimodal tasks.
- VisualPuzzles: Abstract logic, inductive, analogical, and spatial puzzles.
- VLMsAreBlind-mini: Geometric primitives and symbolic visual puzzles designed to expose VLM perception failures.
- V*: High-resolution, fine-grained visual search.
Domain-specific case studies were also conducted on OmniMedVQA (medical imaging), Remote Sensing VQA (GeoChat), and VSI-Bench (egocentric video table counting).
Key Insights and Ideas
- Dynamic Tooling Amplifies Inherent Strengths: PyVision does not replace backend capabilities; it unlocks them. When paired with Claude-4.0-Sonnet (stronger in abstract reasoning), PyVision produced the largest gains on reasoning-heavy and symbolic tasks (+31.1% on VLMsAreBlind-mini). Conversely, paired with GPT-4.1 (superior in raw perception), it yielded the largest boost on fine-grained visual search (+7.8% on V*).
- Task-Driven Tool Emergence: Models naturally adopted distinct algorithmic strategies depending on the domain. Fine-grained visual search tasks triggered coordinate-based cropping in over 83% of tool calls; medical tasks elicited contrast enhancement and histogram equalization; and remote sensing drove morphological segmentation.
- Disparate Agentic Styles: Claude-4.0-Sonnet generated significantly more code blocks per query than GPT-4.1, achieving nearly 100% code usage across sessions with longer iterative toolchains, whereas GPT-4.1 utilized fewer, more concise code executions.
Key Results
PyVision achieved consistent performance improvements over standard Chain-of-Thought (CoT) baselines across models:
- Claude-4.0-Sonnet:
- VLMsAreBlind-mini: 48.1% $\rightarrow$ 79.2% (+31.1%)
- VisualPuzzles: 42.7% $\rightarrow$ 51.0% (+8.3%)
- MathVista: 71.4% $\rightarrow$ 76.2% (+4.8%)
- MathVision-mini: 48.0% $\rightarrow$ 51.3% (+3.3%)
- GPT-4.1:
- V* (Visual Search): 68.1% $\rightarrow$ 75.9% (+7.8%)
- VisualPuzzles: 44.9% $\rightarrow$ 47.4% (+2.5%)
- MMMU: 71.9% $\rightarrow$ 74.3% (+2.4%)
- Qwen2.5-VL-72B: Showed an identical amplification trend, surging +10.0% on perceptual search (V*) but only +1.7% on abstract math (MathVision-mini).
Limitations and Future Work
- Hallucination in Code-Assisted Reasoning: While models successfully synthesize complex visual inspection pipelines (e.g., generating difference maps for spot-the-difference puzzles), they can still misinterpret the visual output or hallucinate non-existent features.
- Latency and Overhead: Multi-turn code generation, sandbox execution, and repeated context expansion introduce notable inference latency compared to single-pass feedforward models.
- Foundational Bottlenecks: PyVision relies strictly on the underlying MLLM's ability to plan accurately and diagnose intermediate errors. If the base model lacks the perception to evaluate an intermediate cropped image or the logic to fix buggy code, the iterative loop breaks.
Summary updated