As Large Language Models (LLMs) become integrated into software development workflows, concerns regarding unintentional biases in AI-generated code. Although evidence suggests these biases exist, limited research has systematically identified, categorized, and explained them. This study investigates bias in AI-generated code and evaluates whether LLMs can reliably identify and explain it through a taxonomy-driven framework. We extended an existing dataset of biased AI-generated Python code and manually annotated snippets with bias categories and human-authored justifications to establish a ground-truth dataset. Using this dataset, we evaluated proprietary and open-source LLMs as automated bias detection and justification systems through ICL. Finally, we analyzed similarity between LLM-generated explanations and human-authored justifications using structured justification and code identification metrics. Our findings demonstrate that LLMs can effectively support code bias identification and explanation. Gemini achieved 80.14% classification accuracy, with 84.0% precision and 95.7% recall, while the best open-source alternative, Qwen3-coder, achieved 82.45% accuracy, 68.64% precision, and 80.22% recall. Additionally, the models achieved justification similarity scores of 80.4% and 80.14%, respectively, relative to human-authored reasoning, and code identification similarity scores of 86.0% and 87.82%. These results suggest that LLMs can detect biased logic in generated Python code and produce explanations that substantially align with expert interpretations.
Figures & tables
Category
Bias Type
Definition
Implications
Literature
Social / Normative
Demographic Bias
Differential behavior or outcomes based on protected attributes (e.g., gender, race, age).
Unequal outcomes across demographic groups.
( Liu et al., 2023b ; Ling et al., 2025 ; Huang et al., 2025 )
Stereotype Bias
Code encodes generalized beliefs or social stereotypes that influence outputs or defaults.
Reinforcement of harmful social narratives.
( Liu et al., 2023b ; Bai et al., 2025 )
Representational Bias
Omission or misrepresentation of certain identities or groups in enumerations or data structures.
Structural exclusion or invisibility of groups.
( Liu et al., 2023b ; Shahbazi et al., 2023 )
Explicit Bias
Direct encoding of discriminatory logic using protected attributes.
Immediate discriminatory decisions.
( Huang et al., 2025 )
Proxy Bias
Use of non-sensitive variables that function as stand-ins for protected attributes.
Misleading evaluation and incorrect performance claims.
( Lum et al., 2022 ; Jacobs and Wallach, 2021 )
Table 1. Taxonomy of Bias in Code and Code Generation Tools
Figure 2. High level overview of the methodology. A flow diagram illustrating the methodology of the study, showing the main stages from data collection and preparation through bias evaluation and analysis to the final results.
Protected Attribute / Association
Bias Type
Standardized Reasoning Template
Yes / Positive
Threshold Cutoff
"Uses a fixed protected attribute (‘X’) with a threshold to determine eligibility or outcomes, where the rigid cutoff may produce advantageous treatment for individuals near the boundary."
Stereotype
"Associates positive trait (‘X’) with a protected group/attribute, reinforcing a positive stereotype in the program logic."
Representational
"Does not represent those outside of the protected attribute in a context where all should have equal opportunity for inclusion."
Naming/ Documentation
"Makes a positive semantic link (‘X’) between the protected attribute and the descriptor."
Demographic
"Uses a protected attribute (‘X’) to grant advantages or preferential outcomes resulting in differential treatment of individuals based on group membership."
Yes / Negative
Threshold/ Cutoff
"Uses a fixed protected attribute (‘X’) with a threshold to determine eligibility or outcomes, where the rigid cutoff may produce unadvantageous treatment for individuals near the boundary."
Table 2. Code Bias Taxonomy Labelling Template
Step
Description
1. Trigger
Identifies the specific code element that introduces or enables bias. Typical triggers include conditional logic (e.g., if / elif ), thresholds or numeric cutoffs, variable or function names, comments, default parameter values, hard-coded constants, scoring rules, and ranking criteria.
2. Mechanism
Explains how the trigger affects system behavior. This includes changes in allocation (who receives benefits), restriction (who is excluded), scoring differences, ranking or prioritization, default role assignment, representational framing, or interpretive influence.
3. Impact
Articulates the broader implication of the behavior. This requires specifying (i) the affected protected or socially salient group, (ii) the form of differential treatment, and (iii) how representation or downstream decisions may be influenced.
Table 3. Three-step standardized justification framework for bias identification.
ICL
Observed Limitation
Prompt Refinement
ICL1
Baseline prompt
Established the bias taxonomy, standardized justification templates, JSON output schema, and representative few-shot example.
ICL2
False positives and unsupported bias assignments
Added evidence-grounding using the biased_code field, dependency rules between protected attributes, association types, and bias categories, and semantic validity constraints.
Added valid grouping rules, distinguished naming bias from behavioral bias, and expanded edge-case handling to reduce spurious detections.
ICL5
Remaining ambiguity in representational bias and association handling
Refined representational bias criteria, differentiated positive and negative association rules for non-protected attributes, and clarified the final decision rules used in evaluation.
Table 4. Evolution of the ICL prompt through iterative error-driven refinement.
Manually Labelled (N=784)
Entire Dataset (N=7,840)
Bias Type
Count
% of Subset
Count
% of Dataset
Naming/Documentation
784
100.00%
7,753
98.89%
Stereotype
439
55.99%
5,484
69.95%
Demographic
407
51.91%
5,486
69.97%
Representational
113
14.41%
1,236
15.77%
Threshold/Cutoff
113
14.41%
1,495
19.07%
Table 5. Distribution of Bias Types Across the Entire Dataset and Manually Labelled Subset
Model
Method
Precision
Average BT Accuracy % 1 1 1 Out of total biases identified.
Average BCR Accuracy
Average JSS
Recall
Gemini
ICL1
66.32%
91.10%
85.24%
80.78%
98.81%
ICL2
76.67%
90.7%
86.02%
80.80%
97.91%
ICL3
75.31%
91.52%
86.49%
81.11%
98.41%
ICL4
83.97%
89.90%
85.97%
80.36%
95.69%
ICL5
85.69%
90.80%
90.97%
81.18%
97.66%
deepseek-r1:14b
ICL5
47.96%
55.64%
65.85
59.28%
66.65%
Table 6. Bias Code Justification Accuracy, Precision, and Recall when N=1
Model
Method
Precision
Average BT Accuracy % 2 2 2 Out of total biases identified.
Table 7. Bias Code Justification Accuracy, Precision, and Recall when N=3
Bias Type
Precision
Recall
F1-score
Support
Naming/Documentation
1.000
1.000
1.000
784
Stereotype
0.778
0.966
0.862
440
Demographic
0.720
0.963
0.824
409
Representational
0.664
0.791
0.722
115
Threshold/Cutoff
0.745
0.982
0.847
113
Proxy
0.026
0.200
0.047
5
Table 8. Bias-type classification performance of Gemini using the ICL5 configuration. Performance is reported using precision, recall, and F1-score for each bias category. Support indicates the number of manually labelled instances containing each bias type.