SecPI: Secure Code Generation with Reasoning Models via Security Reasoning Internalization
On the map: LLM reasoning → Reasoning for agents, planning and code
Abstract (arXiv)
Reasoning language models (RLMs) are increasingly used in programming. Yet, even state-of-the-art RLMs frequently introduce critical security vulnerabilities in generated code. Prior training-based approaches for secure code generation face a critical limitation that prevents their direct application to RLMs: they rely on costly, manually curated security datasets covering only a limited set of vulnerabilities. At the inference level, generic security reminders consistently degrade functional correctness while triggering only shallow ad-hoc vulnerability analysis. To address these problems, we present SecPI, a fine-tuning pipeline that teaches RLMs to internalize structured security reasoning, producing secure code by default without any security instructions at inference time. SecPI filters existing general-purpose coding datasets for security-relevant tasks using an LLM-based classifier, generates high-quality security reasoning traces with a teacher model guided by a structured prompt that systematically enumerates relevant CWEs and mitigations, and fine-tunes the target model on pairs of inputs with no security prompt and teacher reasoning traces -- as a result, the model learns to reason about security autonomously rather than in response to explicit instructions. An extensive evaluation on security benchmarks with state-of-the-art open-weight reasoning models validates the effectiveness of our approach. For instance, SecPI improves the percentage of functionally correct and secure generations for QwQ 32B from 48.2% to 62.2% (+14.0 points) on CWEval and from 18.2% to 22.0% on BaxBench. Further investigation also reveals strong cross-CWE and cross-language generalization beyond training vulnerabilities. Even when trained only on injection-related CWEs, QwQ 32B generates correct and secure code 9.9% more frequently on held-out memory-safety CWEs.
Key facts. Cited by 1 paper in the map (data through 7 Oct 2026). Placed in Reasoning for agents, planning and code, one of 17 directions in LLM reasoning. Builds on 20 of its 33 references that are in the map. First posted on arXiv 4 Apr 2026.
Builds on
20 of 33 references.
Closest ancestors · 3
Same direction, newest first
- 2025PurpCode: Reasoning for Safer Code Generation25 Jul
- 2025KodCode: A Diverse, Challenging, and Verifiable Synthetic Dataset for Coding4 Mar
- 2024Deliberative Alignment: Reasoning Enables Safer Language Models20 Dec
Other references · 17
- 2021Evaluating Large Language Models Trained on Codecited by 8,053
- 2025DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learningcited by 6,146
- 2024GPT-4o System Cardcited by 4,467
- 2024LlamaFactory: Unified Efficient Fine-Tuning of 100+ Language Modelscited by 1,442
- 2023Purple Llama CyberSecEval: A Secure Coding Benchmark for Language Modelscited by 130
- 2023LLMSecEval: A Dataset of Natural Language Prompts for Security Evaluationscited by 58
- 2025CWEval: Outcome-driven Evaluation on Functionality and Security of LLM Code Generationcited by 55
- 2023CodeLMSec Benchmark: Systematically Evaluating and Finding Security Vulnerabilities in Black-Box Code Language Modelscited by 52
- 2024Constrained Decoding for Secure Code Generationcited by 35
- 2024ProSec: Fortifying Code LLMs with Proactive Security Alignmentcited by 24
- 2024HexaCoder: Secure Code Generation via Oracle-Guided Synthetic Training Datacited by 14
- 2024PromptIntern: Saving Inference Costs by Internalizing Recurrent Prompt during Large Language Model Fine-tuningcited by 13
5 more
- 2024SecCoder: Towards Generalizable and Robust Secure Code Generationcited by 13
- 2025Teaching an Old LLM Secure Coding: Localized Preference Optimization on Distilled Preferencescited by 10
- 2024Generative Prompt Internalizationcited by 6
- 2024SeCodePLT: A Unified Platform for Evaluating the Security of Code GenAIcited by 4
- 2025SCGAgent: Recreating the Benefits of Reasoning Models for Secure Code Generation with Agentic Workflowscited by 4
Built on by
Citing papers per year: 2026: 1.
1 paper.
Similar, not cited
Close in wording, no citation either way
- 2025Fortifying LLM-Based Code Generation with Graph-Based Reasoning on Secure Coding Practices
- 2025VulInstruct: Teaching LLMs Root-Cause Reasoning for Vulnerability Detection via Security Specifications
- 2026Inference-Time Safety For Code LLMs Via Retrieval-Augmented Revision
- 2025R2Vul: Learning to Reason about Software Vulnerabilities with Reinforcement Learning and Structured Reasoning Distillation
- 2026Secure Code Generation via Online Reinforcement Learning with Vulnerability Reward Model
- 2026SEC-bench Pro: Can Language Models Solve Long-Horizon Software Security Tasks?
- 2026Antaeus: Hunting Repository-Level Logic Vulnerabilities via Context-Grounded LLM Reasoning ↗
- 2025Secure-Instruct: An Automated Pipeline for Synthesizing Instruction-Tuning Datasets Using LLMs for Secure Code Generation