CodeSafetyBench evaluating and improving ethical safety in large language model code generation
As large language models transform software development, they introduce significant ethical and safety risks, with research revealing a 78.67% probability of harmful code generation under malicious prompts. To address this, we developed CodeSafetyBench, a benchmark comprising 1,050 harmful request cases across healthcare, law, and education. Our approach employs a triadic response structure-consisting of harmful outputs, refusals, and educational feedback-to train models via supervised fine-tuning (SFT) and direct preference optimization (DPO). The results demonstrate that this framework reduces harmful response rates by up to 48%, highlighting that educational feedback is more effective than simple rejection. This work exposes fundamental safety challenges in AI-generated code and provides a systematic foundation for developing safer, ethically aligned models that balance high performance with human values.
Authors
- Zihan Hu (ORCID: https://orcid.org/0009-0003-1618-9768)
- Changjin Li (ORCID: https://orcid.org/0000-0003-3171-9053)
- Rongfeng Cheng
- Haobin Zhang
- Qimeng Luo
- Zhongzhi Xu
- Huiru Zou
- Yihan Cheng
- Jie He
- Qiting Wang
- Weibin Cheng
- Si Qin
- Wenjian Liu
- Kai Song
Institutions
- University of North Carolina at Chapel Hill (US)
- Sun Yat-sen University (CN)
- Jinan University (CN)
- Guangdong Pharmaceutical University (CN)
- University of Macau (MO)
- Second Affiliated Hospital of Guangzhou Medical University (CN)
- University of Saint Joseph (CN)
- Shenzhen University Health Science Center (CN)
- Guangdong Provincial People's Hospital (CN)
- Southern Medical University (CN)
- City University of Macau (MO)
- University of Hong Kong (HK)
- South China University of Technology (CN)
- Guangzhou Medical University (CN)
Publication Details
- Journal
- iScience
- Published
- 2026-02-24
- DOI
- https://doi.org/10.1016/j.isci.2026.115073
- Primary Topic
- Natural Language Processing Techniques
- Type
- article
- Field-Weighted Citation Impact
- 0.00