Zing Forum

Reading

SecPI: Let Reasoning Models Internalize Security Thinking, Bid Farewell to Code Security Vulnerabilities

The research team proposes the SecPI method, which enables reasoning language models to internalize structured security reasoning as default behavior through fine-tuning, allowing them to generate secure code without security prompts during inference. Experiments show that the QwQ 32B model's secure code generation rate increased by 14 percentage points, and it has generalization capabilities across vulnerability types and programming languages.

SecPI推理语言模型安全代码生成CWE代码安全漏洞微调训练安全 reasoningAI编程
Published 2026-04-04 12:29Recent activity 2026-04-07 15:25Estimated read 6 min
SecPI: Let Reasoning Models Internalize Security Thinking, Bid Farewell to Code Security Vulnerabilities
1

Section 01

[Introduction] SecPI: Let Reasoning Models Internalize Security Thinking, Bid Farewell to Code Security Vulnerabilities

In the era of AI-assisted programming, reasoning language models (RLMs) serve as helpful tools for developers, but the code they generate often contains security vulnerabilities. The research team proposes the SecPI method, which allows models to internalize structured security reasoning as default behavior through fine-tuning, enabling them to generate secure code without security prompts during inference. Experiments show that the QwQ 32B model's secure code generation rate increased by 14 percentage points, and it has generalization capabilities across vulnerability types and programming languages.

2

Section 02

Security Dilemma of AI Programming Assistants

Code generated by AI programming models often contains vulnerabilities such as SQL injection and buffer overflow, which pose risks to production environments. Existing solutions have limitations: relying on manually labeled security datasets during training (high cost, limited coverage); adding security prompts during inference (reduces functional correctness, only superficial analysis).

3

Section 03

Core Innovation of SecPI: Three-Stage Fine-Tuning to Internalize Security Thinking

SecPI (Security Reasoning Internalization) makes security reasoning the default mode of models through three-stage fine-tuning:

  1. Data Filtering: Use a large model classifier to identify security-related tasks from general programming datasets;
  2. Reasoning Trajectory Generation: A teacher model, combined with structured prompts (enumerating CWE entries and mitigation measures), generates high-quality security reasoning trajectories;
  3. Fine-Tuning Training: Pair original inputs (without security prompts) with reasoning trajectories to train the target model to perform security reasoning independently.
4

Section 04

Experimental Validation: Significant Improvement in Secure Code Generation Rate

Experiments validate the effectiveness of SecPI across multiple security benchmarks:

  • For the QwQ 32B model on the CWEval benchmark, the proportion of functionally correct and secure code generated increased from 48.2% to 62.2% (+14%);
  • On the BaxBench benchmark, it increased from 18.2% to 22.0%;
  • Cross-CWE Type Generalization: After training on injection-type CWEs, the generation frequency of unseen memory safety CWEs increased by 9.9%, indicating that the model has learned deep security reasoning capabilities.
5

Section 05

Cross-Language Generalization and Practical Advantages

SecPI has cross-programming language transfer capabilities, supporting multiple languages such as Python and JavaScript without separate training; no complex security prompts or multi-round interactions are needed during inference—security becomes an out-of-the-box feature, improving user experience and lowering the threshold for secure programming.

6

Section 06

Technical Details and Implementation Challenges

Implementation challenges and solutions:

  • Data Quality: The classifier needs to accurately identify security tasks, and its discriminative ability is optimized through experiments;
  • Reasoning Trajectory Generation: The teacher model needs to systematically analyze risks and provide mitigation solutions, relying on structured prompt design;
  • Fine-Tuning Strategy: Balance the injection of security knowledge and the retention of original programming capabilities, achieved through fine hyperparameter tuning.
7

Section 07

Insights and Future Outlook

Insights: Internalized learning is superior to external intervention, and the general reasoning framework is transferable; Future directions:

  1. Validation in complex real-world scenarios;
  2. Continuous learning to address emerging vulnerabilities;
  3. Expansion to other AI security areas such as privacy protection and fairness.