A Framework for Identifying, Categorizing, and Explaining Bias in AI-Generated Code
Organizations: University of British Columbia, Kelowna, Canada · Federal University of Pará, Belém, Brazil
Abstract
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. | Indirect discrimination despite attribute removal. | ( Liu et al., 2023b ; Huang et al., 2025 ) | |
| Technical / Statistical | Measurement Bias | Metrics poorly represent the intended construct. | Misleading evaluation and incorrect performance claims. | ( Lum et al., 2022 ; Jacobs and Wallach, 2021 ) |
| 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." |
| 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. |
| 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. |
| ICL3 | Inconsistent outputs and missing fields | Introduced mandatory JSON fields, stricter output formatting, self-verification instructions, and refined issue-generation guidance. |
| ICL4 | Incorrect classifications on edge cases | 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. |
| 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% |
| 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% |
| Model | Method | Precision | Average BT Accuracy % 2 2 2 Out of total biases identified. | Average BCR Accuracy | Average JSS | Recall |
| Gemini | ICL5 | 85.80% | 90.90% | 90.00% | 81.28% | 97.67% |
| deepseek-r1:14b | ICL5 | 48.08% | 55.73% | 65.83% | 59.32% | 66.64% |
| qwen3-coder | ICL5 | 68.53% | 82.13% | 87.87% | 80.12% | 89.81% |
| phi4:14b | ICL5 | 60.55% | 64.44% | 73.39% | 63.68% | 74.95% |
| BT = Bias Type, BCR = Bias Code Recognition, JSS = Justification Similarity Score | ||||||
| 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 |
Language Model Bias Evaluation
Language model bias evaluation tests how models’ outputs, decisions, and representations vary across social groups, cultures, languages, and contexts.
See all 497 papers