NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

SecPI: Secure Code Generation with Reasoning Models via Security Reasoning Internalization

Hao Wang, Niels Mündler, Mark Vero, Jingxuan He, Dawn Song, Martin VechevarXiv 2026

TL;DR

SecPI is a lightweight, cost-effective post-training pipeline that teaches Reasoning Language Models (RLMs) to generate secure code by default. By training models to internalize structured security reasoning, SecPI eliminates the need for explicit security prompts at inference time, significantly boosting code security without sacrificing functional correctness.

What Problem Does This Paper Address?

Reasoning Language Models (RLMs)—such as DeepSeek-R1 or QwQ—have revolutionized automated programming by generating explicit "thinking traces" before outputting code. However, when it comes to security, these models suffer from a frustrating paradox: by default, they introduce critical vulnerabilities at rates similar to standard LLMs.

If you try to fix this at inference time by adding a generic reminder like "Make sure the code is secure," two things happen: the model's functional correctness drops significantly, and it tends to perform shallow, ad-hoc security analysis that misses complex flaws. On the other hand, traditional fine-tuning methods require expensive, manually curated security datasets that lack the step-by-step reasoning traces RLMs need to learn effectively.

Figure 1: Reasoning LLMs reason insufficiently about security by default when coding. When instructed for security, they do not systematically explore all potential vulnerabilities and do not apply the correct mitigations. SecPI trains the model to proactively reason about security and the CWEs systematically, leading to the generated code being secure.

SecPI addresses this gap by enabling models to autonomously reason about security risks and mitigations by default, without any explicit security instructions from the user.

Methods

The authors introduce SecPI (Security Reasoning Internalization), a fully automated three-step data curation and fine-tuning pipeline:

  1. Automated Security Data Curation: Instead of building a security dataset from scratch, SecPI uses an LLM-based classifier (GPT-4o-mini / o3-mini) to scan existing general-purpose coding datasets and filter out tasks that have potential security implications.
  2. CWE-Based Reasoning Synthesis: For each security-relevant task, a powerful teacher model (DeepSeek-R1) generates a high-quality reasoning trace. The teacher is guided by a structured prompt that forces it to systematically enumerate relevant Common Weakness Enumerations (CWEs), analyze how they might manifest, and draft targeted mitigations before writing the final code.
  3. Prompt Internalization: Finally, the target model is fine-tuned via Supervised Fine-Tuning (SFT) on pairs consisting of the standard task description (with no security prompt) and the teacher's secure reasoning trace + code.

Through this internalization process, the model learns to bake security analysis directly into its default thinking process.

Figure 2: Main techniques of SecPI: I. SecPI curates security-relevant data with low cost by scraping existing general-purpose coding datasets for security-relevant problems. II. It then uses a CWE-based prompt to synthesize structured and systematic security reasoning and responses from a teacher model. III. Finally, it adopts supervised fine-tuning to internalize the prompt and train the student model to produce similar reasoning by default.

Datasets

  • Training: The pipeline was trained on a lightweight subset of KODCODE-V1, downsampled and filtered to 1,327 Python problems covering 78 distinct CWEs (such as path traversal, uncontrolled recursion, and weak cryptography).
  • Evaluation:
  • CWEval: Evaluates functional correctness and security across multiple languages using end-to-end exploits.
  • BaxBench: A challenging benchmark for generating complete, runnable web-service backends.
  • LiveCodeBench: Used to evaluate general, non-security-critical programming capabilities to ensure functional correctness wasn't degraded.

Key Insights and Ideas

  • The "Oracle Prompt" Trap: Interestingly, providing the model with exact ground-truth CWEs during training (e.g., "Avoid CWE-78") actually hurts fine-tuning performance. When the vulnerability is handed to the model, it skips the critical step of threat analysis. SecPI uses a CWE-agnostic structured prompt, forcing the model to learn how to discover vulnerabilities autonomously.
  • Remarkable Generalization: Even though SecPI was trained exclusively on Python data, the internalized security habits generalized beautifully to non-Python languages like C, C++, JavaScript, and Go.
  • Cross-CWE Transfer: In a disjoint setup where the model was trained only on injection-related vulnerabilities, it still showed a 9.9% improvement on held-out, completely unseen memory-safety vulnerabilities. The model didn't just memorize fixes; it learned a generalized security mindset.

Key Results

The authors evaluated SecPI across four open-source RLMs (including QwQ-32B, Llama-70B-Decoupled, and Qwen-32B-Distill).

  • Security Boosts: For QwQ-32B, SecPI improved the percentage of functionally correct and secure generations (FuncSec) from 48.2% to 62.2% (+14.0 points) on CWEval, and from 18.4% to 22.0% on BaxBench (outperforming GPT-4o).
  • Preserving Functionality: Unlike inference-time prompts which tank general coding performance, SecPI-tuned models maintained stable functional correctness on LiveCodeBench.
  • High Efficiency: The entire data curation and training process for a 32B model costs under $100 USD end-to-end, requiring less than 32 GH200 GPU hours.

Limitations and Future Work

While highly effective, the authors note a few limitations:

  • RLM Specificity: The current method relies on models that natively support thinking traces, making it less directly applicable to standard instruction-tuned LLMs without explicit Chain-of-Thought prompting.
  • Minor Functionality Trade-off: There is still a slight drop in general coding performance (up to 6.8% in some variants), which the authors propose mitigating by mixing more purely functional coding data into the fine-tuning mix.
  • Future Directions: The team plans to explore using rejection sampling to curate even higher-quality traces and leveraging the correlation between reasoning quality and code security to design reward signals for Reinforcement Learning (RL).

Summary updated

BibTeX
@misc{wang2026secpi,
  author = {Hao Wang and Niels Mündler and Mark Vero and Jingxuan He and Dawn Song and Martin Vechev},
  title = {{SecPI: Secure Code Generation with Reasoning Models via Security Reasoning Internalization}},
  year = {2026},
  eprint = {2604.03587},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2604.03587},
}
Figure