Abstract
As Large Language Models (LLMs) become central to automated software development, the risk of systemic social bias embedded within generated code poses a significant challenge. This paper investigates the prevalence of structural code bias and evaluates strategies for its detection and mitigation. We find that while traditional prompt engineering is often ineffective, providing models with execution feedback from automated fairness tests can reduce code bias rates from nearly 60% to under 5%. Our findings suggest a shift from descriptive fairness instructions to functional fairness testing in AI-assisted coding.
Introduction
While research into LLM bias has primarily focused on natural language outputs, the integration of these models into coding workflows introduces a "silent" failure mode: structural code bias. Unlike textual bias, which is often immediately apparent, code bias is functional. A function designed for hiring or insurance premium calculation may contain logic that unfairly discriminates based on protected attributes like age, gender, or race, even if the code appears syntactically correct.
Existing bias-testing tools are largely optimized for NLP tasks and fail to capture the logical flow and functional outcomes of executable code. This study addresses this gap by proposing a framework to automatically detect and mitigate these "algorithmic prejudices."
Methodology
The researchers developed a novel bias-testing pipeline that treats code generation as a functional testing problem, utilizing 334 diverse scenarios grounded in real-world datasets such as Adult Income, Employee hiring, and Health Insurance premiums.
Prompt Engineering and Dataset Distillation
To evaluate the models' internal tendencies rather than their ability to follow biased instructions, the researchers distilled prompts to be neutral. This approach contrasts with previous methods that might use explicitly biased language in function names or parameters.
Automated Detection Framework
The framework employs Abstract Syntax Trees (AST) to programmatically analyze generated code. It uses counterfactual test cases—running the code with identical inputs except for a single sensitive attribute—to identify discriminatory logic. Bias is quantified using the Code Bias Score (CBS), which measures the ratio of biased functions, alongside metrics for consistency (Intersection) and prevalence (Union) across multiple generations.
Key Insights and Results
The Failure of "Asking Nicely"
A significant finding was the limited success of direct prompt engineering. Instructing models to "avoid bias" often proved ineffective, and in some cases, mentioning protected attributes increased bias by drawing the model's attention to those features. Furthermore, model size did not correlate with fairness; GPT-4 exhibited higher age-bias scores than GPT-3.5-turbo in several tests.
Mitigation via Execution Feedback
The study demonstrated that while models are poor at self-detecting bias (identifying only ~18% of age biases), they are excellent at repairing code when provided with specific execution feedback. By providing models with logs from failed fairness tests, the Code Bias Score for GPT-4 plummeted from 59.88% to 4.79%. The automated testing framework achieved 100% precision in manual validation, confirming its reliability as a diagnostic tool.
Conclusion and Future Work
This research highlights the necessity of moving from "asking for fairness" to "testing for fairness" in AI-assisted development. Future work is required to expand this framework to other programming languages and a broader range of sensitive attributes. Additionally, researchers must address the trade-off between fairness and functionality, as approximately 30% of repaired code instances saw changes in their core logic. Ensuring that bias mitigation does not compromise program intent remains a critical hurdle for production-ready systems.
Summary updated