pySpatial: Generating 3D Visual Programs for Zero-Shot Spatial Reasoning

Zhanpeng Luo, Ce Zhang, Silong Yong, Cunxi Dai, Qianwei Wang, Haoxi Ran, Guanya Shi, Katia P. Sycara, Yaqi Xie

TL;DR

pySpatial is a zero-shot framework that enables Multi-modal Large Language Models (MLLMs) to reason in 3D by generating and executing Python programs. Instead of relying on flawed "spatial imagination," the model uses an API to reconstruct 3D scenes and synthesize novel viewpoints, outperforming state-of-the-art models like GPT-4.1-mini by nearly 13% on complex spatial benchmarks.

Introduction: The "Flat-Earth" Problem in MLLMs

While MLLMs like GPT-4o are impressively good at describing 2D images, they often struggle with 3D spatial reasoning—tasks like determining what is behind an object in a different room or how a camera moved between two frames.

The core challenge is that MLLMs are trained primarily on 2D internet data. When asked a spatial question, they often rely on "spatial mental models"—an implicit, often inaccurate "imagination" of the scene. Previous attempts to fix this used 2D cognitive maps, but these still fail to capture the geometric reality of a 3D environment. This paper asks: How can we give MLLMs an explicit geometric foundation to reason upon?

Comparison of 2D spatial mental models vs. pySpatial's 3D visual programming approach.

Methodology: pySpatial Framework

The authors introduce pySpatial, a visual programming framework that treats the MLLM as a "Code Agent." Instead of answering a query directly, the MLLM writes a Python script using a specialized 3D API. The process follows three main steps:

  1. 3D Reconstruction: The framework uses off-the-shelf models (like CUT3R or VGGT) to convert a sequence of 2D images into a 3D point cloud.
Example of a 3D reconstructed scene used by pySpatial.
  1. Program Generation & Execution: Given a query (e.g., "What is to the left of the chair?"), the MLLM generates code to manipulate a virtual camera. It might call rotate_left() and then synthesize_novel_view() to render exactly what would be visible from that new perspective.
  2. Final Reasoning: The MLLM receives the original images plus the newly rendered "novel views." Because the spatial problem has been transformed into a simple visual recognition task, the model can now answer accurately.

This approach is zero-shot, meaning it requires no expensive fine-tuning of the underlying MLLM.

Experimental Setup

The researchers evaluated pySpatial on three major benchmarks:

  • MINDCUBE: A challenging dataset of over 21,000 spatial question-answer pairs across 3,268 multi-view indoor scenes. It tests rotation, relative positions ("around"), and complex layouts ("among").
  • OMNI3D-BENCH: A single-view spatial reasoning benchmark used to test if the framework generalizes beyond multi-view sequences.
  • MMSI-BENCH: Used for additional validation of positional relationships and camera motion.

Key Findings

  • Explicit vs. Implicit Reasoning: By forcing the model to write code that physically moves a camera in a reconstructed space, the researchers ground the model's logic in actual geometry rather than a black-box "thought" process.
  • Benchmark Dominance: On the full MINDCUBE dataset, pySpatial achieved 58.56% accuracy, significantly outperforming the best open-weight model (DeepSeek-VL2-Small at 47.62%) and proprietary models like GPT-4.1-mini (45.62%).
  • Massive Gains in "Among" Tasks: In scenarios requiring reasoning about objects relative to their entire surroundings, pySpatial outperformed all baselines by over 10%, as it could "see" the whole scene via reconstruction.
  • Real-World Robotics: The authors deployed pySpatial on a Unitree Go1 quadruped robot. While standard MLLMs failed to navigate complex indoor routes due to poor distance estimation, pySpatial generated geometrically consistent motion plans that allowed the robot to successfully reach target objects.

Limitations and Future Work

Despite the success, the paper identifies three main sources of failure:

  1. Reconstruction Quality (13% of errors): If the 3D point cloud is "noisy" or incomplete, the synthesized views are blurry, leading the model astray.
  2. Final Reasoning Errors (20% of errors): Sometimes the code is perfect and the rendered view is clear, but the MLLM still misinterprets the final image.
  3. Efficiency: Generating code and reconstructing 3D scenes takes more time (averaging ~7.45 seconds per query) than a direct text response.

Future work will likely focus on integrating more robust 3D reconstruction backbones and exploring how to make the code-generation-execution loop even faster for real-time robotics.

Summary updated

BibTeX
@inproceedings{luo2026pyspatial,
  author = {Zhanpeng Luo and Ce Zhang and Silong Yong and Cunxi Dai and Qianwei Wang and Haoxi Ran and Guanya Shi and Katia P. Sycara and Yaqi Xie},
  title = {{pySpatial: Generating 3D Visual Programs for Zero-Shot Spatial Reasoning}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=yv15C8ql24},
}
Figure