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

Institutions

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
Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
article

CodeSafetyBench evaluating and improving ethical safety in large language model code generation

Zihan Hu, Changjin Li, Rongfeng Cheng, Haobin Zhang et al.
iScience
Natural Language Processing Techniques
article

CodeSafetyBench evaluating and improving ethical safety in large language model code generation

Zihan Hu, Changjin Li, Rongfeng Cheng, Haobin Zhang, Qimeng Luo, Zhongzhi Xu, Huiru Zou, Yihan Cheng, Jie He, Qiting Wang, Weibin Cheng, Si Qin, Wenjian Liu, Kai Song
article en

Abstract

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.

iScienceVol. 29(4)
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)
Openalex Percentile: Top 49%
Natural Language Processing Techniques
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.