Thinking with Spatial Code for Physical-World Video Reasoning
Organizations: Johns Hopkins University · Stanford University
Abstract
We introduce Thinking with Spatial Code, a framework that transforms RGB video into explicit, temporally coherent 3D representations for physical-world visual question answering. We highlight the empirical finding that our proposed spatial encoder can parse videos into structured spatial code with explicit 3D oriented bounding boxes and semantic labels, enabling large language models (LLMs) to reason directly over explicit spatial variables. Specifically, we propose the spatial encoder that encodes image and geometric features by unifying 6D object parsing and tracking backbones with geometric prediction, and we further finetuning LLMs with reinforcement learning using a spatial rubric reward that encourages perspective-aware, geometrically grounded inference. As a result, our model outperforms proprietary vision-language models on VSI-Bench, setting a new state-of-the-art. Code is available at https://github.com/Beckschen/spatialcode.
Figures & tables
| Methods | Size | VSI-Bench ( Yang et al., 2025b ) | Video-RoboSpatial | ||||||||
| Avg. | Obj. Count | Abs. Dist. | Obj. Size | Room Size | Rel. Dist. | Rel. Dir. | Route Plan | Appear. Order | Config. | ||
| Human Level | – | 79.2 | 84.3 | 47.0 | 60.4 | 45.9 | 94.7 | 95.8 | 95.8 | 100 | 92.1 |
| Proprietary MLLMs | |||||||||||
| Seed-1.6 | 230B | 49.9 | 43.5 | 34.3 | 66.1 | 52.8 | 55.0 | 35.7 | 44.3 | 67.9 | 54.3 |
| Gemini-1.5 Pro | – | 48.8 | 49.6 | 30.9 | 64.1 | 49.4 | 51.3 | 48.1 | 42.0 | 68.0 | – |
| Gemini-2.5-Pro | – | 53.5 | 46.0 | 37.3 | 68.7 | 54.3 | 61.9 | 43.9 | 47.4 | 68.7 | 53.3 |
| Input | Methods | ARKitScenes | ScanNet | ||
| [email protected] | [email protected] | [email protected] | [email protected] | ||
| Image detector | 3D-MOOD ( Yang et al., 2025c ) | 0.066 | 0.009 | 0.083 | 0.025 |
| DetAny3D ( Zhang et al., 2025 ) | 0.094 | 0.012 | 0.119 | 0.024 | |
| Point-cloud detector | SpatialLM ( Mao et al., 2025 ) | 0.122 | 0.055 | 0.134 | 0.025 |
| SceneScript ( Avetisyan et al., 2024 ) | 0.020 | 0.014 | 0.101 | 0.043 | |
| MLLMs | Qwen3-VL-4B ( Yang et al., 2025a ) | 0.041 | 0.011 | 0.077 | 0.023 |
| SAM2 Enc. | DA3 Enc. | Depth Head | [email protected] | 3D IoU | Dep. Err. | Scale Err. |
| ✓ | - | - | 0.27 | 0.10 | 0.50 | 0.63 |
| ✓ | ✓ | - | 0.31 | 0.12 | 0.42 | 0.57 |
| ✓ | ✓ | ✓ | 0.32 | 0.14 | 0.31 | 0.55 |
| Training | Avg. | Rel. Dir. | Abs. Dist. | Config. |
| w/o spatial code | 51.8 | 48.8 | 42.6 | 47.0 |
| w/o RL | 56.5 | 55.5 | 50.0 | 65.3 |
| w/ Accuracy Reward | 57.6 | 59.3 | 52.3 | 63.0 |
| w/ Spatial Rubric | 60.0 | 87.1 | 60.7 | 67.0 |
| Spatial Code Source | RL | Perception | Reasoning |
| Ground-truth codes | 1.00 | 68.9 | |
| Spatial Encoder (ours) | 0.52 | 56.5 | |
| Ground-truth codes | 1.00 | 73.2 | |
| Spatial Encoder (ours) | 0.52 | 60.0 | |
| Human performance | – | 79.2 |
Appendix figures & tables13 assets
Supplementary material from the paper’s appendix.
Appendix
| Hyperparameter | Symbol | Value |
| GRPO Algorithm | ||
| Group size (samples per prompt) | 16 | |
| KL penalty coefficient | 0.01 | |
| Sampling temperature | 0.7 | |
| Reward clipping range | – | |
| Optimization | ||
| Task Type | Key Instruction | Answer |
| pairwise_configuration | Focus on relative positions, orientations, left/right, front/behind | Yes/No |
| object_rel_direction | Analyze relative positions from object-centric perspective | A/B/C/D |
| object_rel_distance | Compare closest-point distances to multiple candidates | A/B/C/D |
| object_abs_distance | Calculate min distance between closest bbox points | Numeric |
| object_counting | Count objects matching specified category | Numeric |
| object_size_estimation | Report max(x_size, y_size, z_size) in requested unit | Numeric |
| Component | Range | Description |
| Exact match with ground truth | ||
| Structure compliance and penalties | ||
| Task-specific reasoning quality | ||
| Clipped sum of all components |
| Indicator | Condition | Weight |
| Valid format | Single “Final Answer:” with valid response | |
| Missing format | No “Final Answer:” detected | |
| Degenerate repetition | Pathological output patterns (e.g., repeated phrases) | |
| Excessive length | Response characters | |
| Too short | Response characters |
| Indicator | Condition | |
| Structured reasoning | step indicators (“Step 1”, “First”, “Then”) | |
| Conclusion statement | Contains conclusion phrase (“therefore”, “thus”) | |
| Reasoning consistency | Analysis logically supports final answer | |
| Reasoning inconsistency | Analysis contradicts final answer |
| Indicator | Condition | |
| Positive Indicators | ||
| Coordinate extraction | Extracts coordinate values | |
| Facing vector computation | Computes observer-to-target direction | |
| Local coordinate system | Constructs forward/right basis vectors | |
| Vector projection | Uses dot product for local components | |
| Quadrant determination | Maps component signs to quadrant | |
| Indicator | Condition | |
| Positive Indicators | ||
| Perspective-based reasoning | Uses object’s perspective, not viewer’s | |
| Orientation awareness | Considers yaw angle in analysis | |
| Coordinate analysis | Compares object coordinates correctly | |
| Directional consistency | Handles opposite pairs (left/right) | |
| Negative Indicators | ||
| Component | Parameters |
| SAM2 Encoder (Hiera-ViT-L) | 214M |
| DA3 Encoder (ViT-G) | 1.43B |
| Qwen3-VL-4B (LM backbone) | 4B |
| Total | 5.6B |
| Model | Total Param. | Spatial Enc. | LM | VSI-Bench (Avg 8) |
| Qwen3-VL-8B | 8B | 0 | 8B | 55.1 |
| Ours | 5.6B | 1.6B | 4B | 60.8 (+2.9%) |
Visual Spatial Reasoning
Visual spatial reasoning studies how AI systems infer geometry, object relations, viewpoints, and physical change from images and video.
See all 251 papers