CAD-Coder: Text-to-CAD Generation with Chain-of-Thought and Geometric Reward
In this work, we introduce CAD-Coder, a novel framework that reformulates text-to-CAD as the generation of CadQuery scripts—a Python-based, parametric CAD language. This representation enables direct geometric validation, a richer modeling vocabulary, and seamless integration with existing LLMs. To further enhance code validity and geometric fidelity, we propose a two-stage learning pipeline: (1) supervised fine-tuning on paired text–CadQuery data, and (2) reinforcement learning with Group Reward Policy Optimization (GRPO), guided by a CAD-specific reward comprising both a geometric reward (Chamfer Distance) and a format reward. We also introduce a chain-of-thought (CoT) planning process to improve model reasoning, and construct a large-scale, high-quality dataset of 110K text–CadQuery–3D model triplets and 1.5K CoT samples via an automated pipeline. Extensive experiments demonstrate that CAD-Coder enables LLMs to generate diverse, valid, and complex CAD models directly from natural language, advancing the state of the art of text-to-CAD generation and geometric reasoning.
Introduction. Computer-Aided Design (CAD) systems are fundamental tools in engineering and manufacturing, enabling the creation of precise 3D models. However, traditional CAD workflows often demand significant expertise and are time-consuming [21, 6], which limits broader accessibility and hampers rapid iteration. Recent advancements in Large Language Models (LLMs)[28], particularly their proficiency in natural language understanding and code generation[3, 1], present a promising opportunity to streamline CAD processes [5, 26] based on natural language descriptions. The ability to generate or modify CAD models via textual instructions [2, 13, 16] could lower the entry barrier for novices and enhance the efficiency of experienced users.
However, generating CAD from textual descriptions remains a nontrivial challenge. To leverage progress in natural language processing, researchers have proposed representing CAD models using pre-defined command sequences and formulating text-to-CAD as a machine translation problem—i.e., autoregressively predicting CAD command tokens conditioned on input text [32, 13].
Despite their utility, these approaches face several limitations. First, verifying the validity of a CAD model represented by command sequences is challenging. Second, most existing methods support only a limited set of operations, such as sketch and extrusion, restricting the diversity of generated CAD models. Third, CAD command sequences are often difficult to interpret and edit, complicating both understanding and debugging.
To address these issues, we advocate for a new proxy representation of CAD models. In this paper, we utilize CadQuery [7], a Python-based parametric CAD scripting language, as the target representation. CadQuery is particularly suitable for the following reasons: (1) It provides inherent geometric validation, as CadQuery scripts can be directly executed to verify the validity of the resulting CAD model. (2) It offers a rich vocabulary for CAD modeling, enabling the representation of diverse and complex geometries. (3) CadQuery scripts are composed of semantic, function-based constructs, making them more interpretable than low-level command sequences. (4) Importantly, as CadQuery is implemented in Python, it allows us to leverage the code generation capabilities of modern LLMs that are already proficient in programming tasks.
Consequently, we reformulate the text-to-CAD task as generating CadQuery code from natural language input. While this representation enables the use of LLMs for CAD generation, adapting LLMs to reliably produce high-quality CAD models remains challenging. The core difficulty arises from the dual requirements of CadQuery code: syntactic correctness (from a programming perspective) and geometric plausibility (from a 3D modeling perspective). While supervised finetuning (SFT) on paired text and CadQuery code can teach the model syntactic patterns, it is insufficient to guarantee both code validity and geometric correctness, as it lacks explicit 3D knowledge and reasoning capabilities [37, 9, 36].
To overcome these challenges, we draw inspiration from recent advances where reinforcement learning (RL) has improved LLM reasoning and planning across various domains. In particular, we integrate Group Reward Policy Optimization (GRPO), an efficient RL algorithm, into the text-to-CAD code generation pipeline. Our approach consists of two stages: (1) We begin by supervised fine-tuning an LLM with paired natural language descriptions and CadQuery code to establish basic syntax and mapping. (2) We then enhance the model’s planning and reasoning ability via RL, introducing a novel CAD-Specific reward function.
Specifically, since multiple distinct CadQuery scripts can produce geometrically equivalent CAD models—a challenge for SFT to capture—we introduce a chain-of-thought (CoT) process that encourages the model to plan before code generation. Our CAD-Specific reward comprises two components: a geometric reward, which uses the Chamfer Distance (CD) between generated and target 3D geometries to ensure geometric accuracy, and a format reward, which enforces the reasoning process and syntactic correctness of the generated code.
To facilitate research in this area, we construct a large-scale, geometrically verified dataset comprising 110K text–CadQuery-3D model triplets and 1.5K high-quality CoT samples. We also propose an automatic data construction pipeline to accelerate dataset creation and ensure high quality. Extensive experiments demonstrate that our method unlocks new capabilities for LLMs, enabling the generation of complex, functional CAD models directly from high-level textual intent. In summary, our contributions include the following:
Related work. Large language models (LLMs) have revolutionized code generation, with models like GPT-4 [20] and specialized code-focused LLMs such as CodeLlama [27] showcasing impressive capabilities in translating natural language into various programming languages [3, 1]. Standard training typically involves supervised fine-tuning (SFT) on extensive code corpora and instruction datasets. To better align LLM behavior with specific goals or complex tasks, reinforcement learning (RL) techniques have been increasingly employed. Reinforcement learning with human feedback (RLHF)[11] is widely used for general alignment. For more task-specific optimization, policy gradient algorithms like Proximal Policy Optimization (PPO)[23] are commonly utilized, though these often require training a separate critic network, which adds computational overhead.
Our approach leverages Group Reward Policy Optimization (GRPO) [24], a more recent and efficient RL algorithm that estimates baselines through relative rewards within a sample batch, removing the need for a critic and making RL fine-tuning more feasible for complex tasks like ours. Generating structured and logically coherent code, especially for multi-step procedures common in CAD modeling, requires advanced reasoning. Chain-of-Thought (CoT) prompting [30] has proven effective in improving the reasoning and planning capabilities of LLMs by prompting them to generate intermediate steps. We leverage CoT to enhance the model’s ability to decompose complex natural language instructions into coherent CadQuery code sequences.
Generative modeling for CAD systems commonly employs two main representations: boundary representation (B-rep) [14] and command sequence representations [32, 31]. B-rep models combine geometry and topology to offer high precision and accuracy; however, their complexity presents significant challenges for generative modeling. To address this, various models use separate latent spaces and decoders for geometry and topology [10, 34, 8]. Notably, HoLa [17] introduces a unified latent space for B-rep generation. Despite their advantages, direct generation of B-rep models from text remains computationally intensive, as it involves capturing intricate geometric features and interrelationships. Alternatively, command sequence representations, such as those proposed by DeepCAD [32], model the procedural nature of CAD design by encoding the design process as a series of commands, e.g., sketch creation or extrusion. Several approaches [12, 4] have demonstrated the ability to generate command sequences from point clouds or images. CAD-MLLM [33] leverages multimodal large language model (MLLM) to enhance the performance of this generation process.
Method. We adopt CadQuery, a Python-based parametric CAD scripting language, as the core representation for 3D modeling in our framework. CadQuery can be executed directly without any external software dependencies. CadQuery allows models to be constructed using chainable geometric operations (e.g., box(), circle(), extrude()), encoded as modular and readable Python code. Each script corresponds to a complete, executable modeling procedure that can be rendered directly via the OpenCascade kernel into high-fidelity 3D geometry.
Traditional methods such as DeepCAD [32] represent model structures using sketch-extrude command sequences. As illustrated in Fig. 1, these representations are typically linearized and low-level, lack modularity, and cannot be directly executed. They often require additional post-processing to produce 3D shapes. This not only increases modeling complexity but also hinders the model’s ability to learn structured modeling semantics. In contrast, CadQuery is interpretable and executable. CadQuery provides expressive and flexible geometric operations, ranging from basic primitives to complex modeling procedures. Meanwhile, CadQuery scripts can be directly executed for validation. The provided high-level API can also better align with the input textual description.
Considering the Python nature, CadQuery is well-suited for generative modeling with language models. Nevertheless, a crucial challenge lies in generating code that is both syntactically correct and produces geometrically accurate and valid 3D designs. To address this, our approach employs a two-stage training strategy. First, an initial Supervised fine-tuning (SFT) phase teaches the model the specific CadQuery syntax. Then, a reinforcement learning (RL) phase is introduced to further enhance the geometric accuracy and validity of the generated 3D output.
Overview. We adopt Qwen2.5-7B-Instruct [35], a strong open-source language model pre-trained on a mixture of web text and code, as the base model for CadQuery code generation. The input to the model is a natural language description L of the 3D design intent. The output is an executable CadQuery script C which is executed to produce a 3D geometry M = Execute(C). The model is fine-tuned and optimized in an autoregressive decoding setup, where CadQuery tokens are generated sequentially, conditioned on the input and previous tokens.
To better generate CAD models, our method leverages a two-stage training strategy. In the first stage, we SFT the LLM with paired data, which enables the model to learn CadQuery’s fundamental syntax and common programming patterns. To further enhance the geometric reasoning ability of the model, which improves the accuracy of the final 3D model, we introduce the second RL stage with a reward specifically designed for CAD.
Stage 1: Supervised Fine-Tuning for CAD Code Generation. We begin by performing SFT to equip the model with the basic capability to translate natural language descriptions into executable CadQuery code. Unlike generic code generation, CAD code must follow strict syntactic and geometric constraints. This phase serves as a foundation that enables the model to understand the CadQuery’s syntax and learn the basic mapping between high-level descriptions to low-level modeling operations in a structured format.
This allows the model to follow the syntax of CadQuery and establish preliminary mappings between common shape-related language patterns and CAD primitives (e.g. “create a hole” −→.hole(), “draw a circle” −→.circle()).
After this stage, the model shows promising capabilities in generating valid CadQuery code for standard and relatively simple modeling cases. However, we observe two major limitations: The generated code sometimes lacks geometric accuracy compared to the target shape, and the model struggles with complex structures that require multi-step or spatial reasoning.
Discussion. Table 1 summarizes the quantitative performance on the test set. Our full method achieves the best results across all metrics, significantly outperforming prior works in terms of geometric accuracy. Specifically, it reduces the Mean CD to 6.54 and the Median CD to 0.17, both by large margins compared to the strong baseline Text2CAD [13], surpassing all existing LLMs. Fig. 3 exhibits the qualitative results. We can observe that LLMs frequently fail to generate valid code. Our method can better align with the target shape. These results highlight the effectiveness of our geometry-aware optimization and CoT-enhanced reasoning in generating precise and structurally valid 3D CAD models. Our method also maintains a lower code invalidity ratio, demonstrating that reinforcement-driven learning does not compromise executability.
Table 2 isolates the effect of each component in the training pipeline. Starting from a model trained solely with SFT, we observe limited geometric fidelity (Mean CD 74.55, Median CD 0.33). This baseline demonstrates that SFT alone cannot adequately capture spatial reasoning for complex CAD structures. However, the model also performs poorly (Mean CD 76.20) without SFT, showing the necessity of using prior knowledge. Even without CoT, the GRPO alone dramatically boosts performance (Mean CD 17.34, Median CD 0.20), confirming that CAD-Specific reward supervision is essential for improving 3D accuracy. Our full method, adding CoT prompting for cold-starting, further improves results (Mean CD 6.54, Median CD 0.17), indicating that structured multi-step reasoning enhances the model’s ability to handle complex geometric prompts. Fig. 5 illustrates the CD distributions of the generated model under different training strategies. We can observe that more effective training strategies result in distributions that are skewed towards smaller CD values and fewer invalid results. Fig. 3 reveals the visualization results with different components. Experimental results demonstrate the effectiveness of each component.
Conclusion. In this paper, we have presented a novel approach to text-to-CAD generation by leveraging CadQuery as an intermediate representation. By combining the strengths of Python-based code generation and the inherent interpretability of CadQuery, our method overcomes key challenges associated with traditional command sequence-based approaches, including model validity and limited operation sets. We propose a two-stage training strategy combining supervised fine-tuning (SFT) with reinforcement learning (RL). The integration of Group Reward Policy Optimization (GRPO) and a CAD-Specific reward function ensures that the generated CAD models are both syntactically correct and geometrically plausible, while the Chain-of-Thought (CoT) process allows for improved reasoning and planning. Our large-scale, geometrically verified dataset facilitates further research in this domain, and the experimental results show that our method significantly advances the capabilities of LLMs in generating complex CAD models from natural language descriptions. This work opens the door to more accessible, efficient, and flexible CAD generation, making it easier for both novice and experienced users to create high-quality 3D models based on textual input.
Lines of inquiry this paper opens 24
Research framings built by reading the notes related to this paper — the questions it feeds into.
How should design principles adapt for AI-generated interfaces? Can visual rendering-based rewards effectively improve code and layout quality?- Does the rendering reward generalize beyond chart editing to other code generation tasks?
- How does the rendering reward weight code executability against visual fidelity failures?
- What filtering and verification steps ensure automatically generated CAD training data remain valid?
- Why does general VLM pretraining fail at numeric constraint understanding in CAD?
- How do executability and geometric accuracy rewards interact during training?
- How do format rewards and geometric rewards split the work of training CAD models?
- What is Chamfer Distance and why measure it for CAD models?
- Does format reward contribute independently to CAD-Coder's final performance?
- Why do multiple CadQuery scripts produce geometrically equivalent CAD models?
- Can structural features replace learned reward models in design generation tasks?
- What breaks when floor plan rewards ignore architectural style or program type?
- Can visual rendering rewards work for tasks beyond code reconstruction?
- How does render-and-compare training differ from expert-judged rewards in visual tasks?
- Can preference-based training replace hand-coded metrics for design evaluation?