Table of Contents
Fetching ...

Bias Unveiled: Investigating Social Bias in LLM-Generated Code

Lin Ling, Fazle Rabbi, Song Wang, Jinqiu Yang

TL;DR

This work addresses the problem of social bias in LLM-generated code by introducing Solar, a black-box fairness framework, and SocialBias-Bench, a dataset of 343 real-world, human-centered coding tasks. Solar automatically generates domain-specific prompts and executable test cases to quantify bias using Code Bias Score $CBS$, Bias Leaning Score $BLS$, and Pass@attribute, then leverages iterative prompting with feedback to mitigate bias while preserving functional correctness. Evaluation across four state-of-the-art code-generation LLMs reveals pervasive biases, with substantial model- and demographic-dimension variance; iterative prompting with Solar achieves substantial bias reductions (e.g., from 60.58% to 8.77% CBS in GPT-3.5-turbo-0125) and improves functional alignment. The work contributes an extensible dataset, a metamorphic-testing-inspired fairness framework, and practical bias-mitigation strategies, advancing responsible deployment of LLMs in software development.

Abstract

Large language models (LLMs) have significantly advanced the field of automated code generation. However, a notable research gap exists in evaluating social biases that may be present in the code produced by LLMs. To solve this issue, we propose a novel fairness framework, i.e., Solar, to assess and mitigate the social biases of LLM-generated code. Specifically, Solar can automatically generate test cases for quantitatively uncovering social biases of the auto-generated code by LLMs. To quantify the severity of social biases in generated code, we develop a dataset that covers a diverse set of social problems. We applied Solar and the crafted dataset to four state-of-the-art LLMs for code generation. Our evaluation reveals severe bias in the LLM-generated code from all the subject LLMs. Furthermore, we explore several prompting strategies for mitigating bias, including Chain-of-Thought (CoT) prompting, combining positive role-playing with CoT prompting and dialogue with Solar. Our experiments show that dialogue with Solar can effectively reduce social bias in LLM-generated code by up to 90%. Last, we make the code and data publicly available is highly extensible to evaluate new social problems.

Bias Unveiled: Investigating Social Bias in LLM-Generated Code

TL;DR

This work addresses the problem of social bias in LLM-generated code by introducing Solar, a black-box fairness framework, and SocialBias-Bench, a dataset of 343 real-world, human-centered coding tasks. Solar automatically generates domain-specific prompts and executable test cases to quantify bias using Code Bias Score , Bias Leaning Score , and Pass@attribute, then leverages iterative prompting with feedback to mitigate bias while preserving functional correctness. Evaluation across four state-of-the-art code-generation LLMs reveals pervasive biases, with substantial model- and demographic-dimension variance; iterative prompting with Solar achieves substantial bias reductions (e.g., from 60.58% to 8.77% CBS in GPT-3.5-turbo-0125) and improves functional alignment. The work contributes an extensible dataset, a metamorphic-testing-inspired fairness framework, and practical bias-mitigation strategies, advancing responsible deployment of LLMs in software development.

Abstract

Large language models (LLMs) have significantly advanced the field of automated code generation. However, a notable research gap exists in evaluating social biases that may be present in the code produced by LLMs. To solve this issue, we propose a novel fairness framework, i.e., Solar, to assess and mitigate the social biases of LLM-generated code. Specifically, Solar can automatically generate test cases for quantitatively uncovering social biases of the auto-generated code by LLMs. To quantify the severity of social biases in generated code, we develop a dataset that covers a diverse set of social problems. We applied Solar and the crafted dataset to four state-of-the-art LLMs for code generation. Our evaluation reveals severe bias in the LLM-generated code from all the subject LLMs. Furthermore, we explore several prompting strategies for mitigating bias, including Chain-of-Thought (CoT) prompting, combining positive role-playing with CoT prompting and dialogue with Solar. Our experiments show that dialogue with Solar can effectively reduce social bias in LLM-generated code by up to 90%. Last, we make the code and data publicly available is highly extensible to evaluate new social problems.

Paper Structure

This paper contains 13 sections, 5 equations, 4 figures, 5 tables.

Figures (4)

  • Figure 1: An overview of social bias evaluation framework Solar with examples.
  • Figure 2: An example of test case generated by Solar.
  • Figure 3: Radar chart: shape the pattern of prejudicial preferences of age on different models, the blue line: the GPT-3.5-turbo-0125, the orange line: codechat-bison@002, the green line: CodeLlama-70b-instruct-hf, the red line: claude-3-haiku-20240307. (For more information about all demographics, you can find the appendix via the shared code link.)
  • Figure 4: Illustration on the effect of hyper-parameters temperature t on CBS for the four subject LLMs. The x-axis represents the hyper-parameter values of t, while the y-axis signifies CBS.