[Paper Review] Bias Testing and Mitigation in LLM-based Code Generation
This paper proposes a novel bias testing framework to detect social biases—such as gender and age—in code generated by large language models (LLMs). It evaluates five SOTA LLMs and finds 20.29% to 44.93% of generated functions exhibit bias; prompt-based mitigation strategies, especially one- and few-shot learning, reduce bias by 80–90% in GPT-4.
As the adoption of LLMs becomes more widespread in software coding ecosystems, a pressing issue has emerged: does the generated code contain social bias and unfairness, such as those related to age, gender, and race? This issue concerns the integrity, fairness, and ethical foundation of software applications that depend on the code generated by these models but are underexplored in the literature. This paper presents a novel bias testing framework that is specifically designed for code generation tasks. Based on this framework, we conduct an extensive empirical study on the biases in code generated by five widely studied LLMs (i.e., PALM-2-CodeChat-bison, Claude-instant-1, GPT-3.5-turbo, GPT-4-turbo, and GPT-4). Our findings reveal that biases are prevalent. For example, 13.47% to 49.10% of the codes generated by these LLMs have biased behaviors towards gender. Moreover, we study five bias mitigation prompt strategies that are commonly used in current code generation scenarios, i.e., zero-shot, one-shot, few-shot, and two Chain-of-Thought (CoT) prompts, with and without provided feedback-driven refinement. Our evaluation results illustrate that using direct prompt engineering strategies has limited effectiveness in mitigating bias, but our test execution feedback can help to reduce the ratio of code biases to a large extent (e.g., from 59.88% to 4.79% for GPT-4).
Motivation & Objective
- To investigate whether social biases—such as gender, age, and race—exist in code generated by state-of-the-art LLMs.
- To develop a reliable, automated framework for detecting bias in code generation outputs, tailored to the structural and logical nature of code.
- To evaluate the effectiveness of prompt engineering strategies in mitigating bias in LLM-generated code functions.
- To provide a reproducible benchmark and open-source tools for future research and development in fair code generation.
Proposed method
- Construct a prompt set targeting bias-sensitive tasks (e.g., aid eligibility based on gender or age) to elicit code from LLMs.
- Parse generated code using Abstract Syntax Trees (AST) to extract function names, parameters, and values for systematic analysis.
- Generate test cases by varying only one input parameter (e.g., gender) while keeping others constant, to detect inconsistent or biased behavior.
- Execute test cases in a local environment to check for assertion failures, indicating bias; use manual inspection for runtime errors.
- Define three metrics: Code Bias Score (CBS), CBS_U@K (union set of biases across runs), and CBS_I@K (intersection set) to quantify bias prevalence and consistency.
- Apply five prompt-based mitigation strategies—zero-shot, one-shot, few-shot, and two Chain-of-Thought (CoT) prompts—using bias testing results to refine code generation.

Experimental results
Research questions
- RQ1RQ1: Does bias exist in LLM-generated code, particularly in functions involving sensitive attributes like gender and age?
- RQ2RQ2: Is the proposed bias testing framework reliable in identifying and measuring bias in code generation outputs?
- RQ3RQ3: How effective are prompt engineering strategies in mitigating bias in code generated by LLMs?
Key findings
- Between 20.29% and 44.93% of code functions generated by five SOTA LLMs were found to be biased on bias-sensitive tasks.
- The proposed code bias testing framework successfully identifies bias through automated test case execution and AST-based analysis, with manual validation for edge cases.
- One-shot and few-shot prompt engineering strategies were the most effective, reducing code bias in GPT-4 by 80% to 90%.
- Larger models do not necessarily produce fewer biased outputs, indicating that model scale alone does not mitigate bias.
- The framework’s CBS metrics effectively capture both the prevalence and consistency of bias across multiple model runs.
- The study reveals that bias in code generation is not limited to specific models, but is a systemic issue across current SOTA LLMs.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.