Papers
Topics
Authors
Recent
Search
2000 character limit reached

Social Bias in LLM-Generated Code: Benchmark and Mitigation

Published 1 May 2026 in cs.SE, cs.AI, and cs.SI | (2605.00382v3)

Abstract: LLMs are increasingly deployed to generate code for human-centered applications where demographic fairness is critical. However, existing evaluations focus almost exclusively on functional correctness, leaving social bias in LLM-generated code largely unexamined. Extending our prior work on Solar, we conduct a comprehensive empirical study using SocialBias-Bench, a benchmark of 343 real-world coding tasks spanning seven demographic dimensions. We evaluate four prominent LLMs and find severe bias across all models, with Code Bias Scores reaching up to 60.58%. We further show that standard prompt-level interventions, such as Chain-of-Thought reasoning and fairness persona assignment, inadvertently amplify bias rather than reduce it. We then investigate whether structured multi-agent software process frameworks can improve fairness, finding that structured pipelines reduce bias when early roles correctly scope what the code should and should not consider. However, adding explicit fairness instructions to all agent roles produces worse outcomes than providing none, suggesting that diffused responsibility goes unaddressed. To address these limitations, we propose the Fairness Monitor Agent (FMA), a modular component that plugs into any existing code generation pipeline without modifying it. FMA analyzes the task description to determine which attributes should be considered or restricted, then detects and corrects violations through an iterative review process, without requiring an executable test suite. Evaluated on all 343 tasks, FMA reduces bias by 65.1% compared to a developer agent alone and improves functional correctness from 75.80% to 83.97%, outperforming all other studied approaches.

Summary

  • The paper demonstrates that social bias is intrinsically embedded in LLM-generated code, with Code Bias Scores reaching up to 60.6% in certain models.
  • It introduces the Solar fairness framework and SocialBias-Bench, using 343 human-centered tasks to evaluate bias across seven demographic dimensions.
  • The proposed Fairness Monitor Agent reduces bias by 65.1% over three repair cycles while improving functional correctness from 75.8% to 84% without oracle access.

Systematic Analysis and Mitigation of Social Bias in LLM-Generated Code

Introduction

This paper investigates the presence, characterization, and mitigation of social bias in code generated by LLMs, with a rigorous evaluation framework and an extensive empirical study over 343 human-centered coding tasks spanning seven demographic dimensions. The authors extend the Solar fairness framework, introduce the SocialBias-Bench task set, evaluate multiple LLMs and multi-agent workflows, scrutinize prompt-level interventions, and propose a modular Fairness Monitor Agent (FMA) system for mitigating code bias in an oracle-free manner.

Frameworks and Benchmark

Solar Fairness Evaluation Framework

Solar provides an automated pipeline for bias quantification in LLM-generated code, leveraging metamorphic testing. For each coding task, the framework constructs matched test inputs that differ only in a sensitive attribute, systematically identifying unjustified output disparities. It introduces rigorous metrics:

  • Code Bias Score (CBS): Proportion of generated code snippets that exhibit bias under sensitive attribute perturbation.
  • Bias Leaning Score (BLS): Directional preference among demographic values within a dimension.
  • Pass@attribute: Accuracy metric reflecting correct use of relevant (required) and exclusion of sensitive (prohibited) features in generated code logic. Figure 1

    Figure 1: Overview of the Solar fairness evaluation framework.

SocialBias-Bench

SocialBias-Bench encompasses 343 tasks covering real-world scenarios such as admissions, licensing, benefits, and healthcare. Each task is specified with a neutral docstring and structured attributes, including both related and sensitive demographic fields. Task and input design discourage shortcut learning and require the LLM to perform nontrivial reasoning, increasing ecological validity with respect to professional software contexts.

LLM Bias: Characterization and Root Causes

Pervasiveness and Model Dependency

Empirical assessment reveals severe and systematic social bias across all tested models. GPT-3.5-turbo-0125 demonstrates the highest CBS (up to 60.6%), while even the lowest-bias model (CodeLlama-70b-instruct-hf) exhibits nontrivial bias (CBS 28.3%). Age and employment status are consistently the most affected dimensions, and distinct models exhibit unique directionality in their bias (e.g., favoring or penalizing specific demographic values), tracing these disparities to variations in training data composition and alignment regimes.

Temperature and Sampling

Temperature scaling does not uniformly mitigate bias. In some models, such as CodeLlama-70b-instruct-hf, lowering the temperature (thus favoring high-probability outputs) amplifies bias. Bias direction and magnitude are therefore fundamentally encoded in model weights, not in prompt stochasticity or surface-level decoding parameters. Figure 2

Figure 2

Figure 2

Figure 2

Figure 2

Figure 2

Figure 2

Figure 2

Figure 2: Radar charts: Bias Leaning Ratio of seven demographic dimensions on different models.

Prompt-Level and Role-Based Fairness Interventions

Chain-of-Thought and Persona Prompting

Contrary to prevailing assumptions in the prompt engineering literature, neither Chain-of-Thought (CoT) reasoning nor fairness persona assignments reduce code bias. Both interventions, when applied to instruction or system prompts, either maintain or amplify CBS, with marked increases in some cases (e.g., GPT-3.5-turbo-0125: 60.58% → 72.65% with CoT). Pass@attribute concurrently decreases, indicating degradation in both fairness and correctness. The intuition is that explicit “fairness” prompts operate within the model’s internal stereotypical associations, often serving to surface—rather than suppress—embedded bias.

Structured Multi-Agent Workflows

Multi-agent software process models (FlowGen) show a consistent reduction in bias relative to single-agent LLM pipelines. The Waterfall configuration achieves the lowest CBS (24.5% vs. baseline 60.6%). However, naive diffusion of fairness instructions (i.e., adding minimal fairness reminders to every agent’s prompt) is counterproductive: when responsibility is distributed across all roles, CBS increases (31.1%) relative to the unbiased multi-agent baseline, evidencing a dilution-of-responsibility effect.

Ablation studies confirm that upstream roles, particularly the requirement engineer, are critical for fairness. Their removal induces large increases in CBS, demonstrating the importance of explicit attribute scoping early in the code generation process.

Modular, Oracle-Free Bias Mitigation: Fairness Monitor Agent (FMA)

FMA is introduced as a plug-in, pipeline-agnostic fairness intervention system that does not require modification to the code generation backbone or access to a metamorphic test oracle. FMA consists of dedicated LLM-based agents for requirements analysis, fairness attribute classification, static code review, and iterative repair. It enforces a closed-world assumption: attributes not explicitly required are classified as restricted. Figure 3

Figure 3: Overview of the FMA pipeline architecture.

Over iterative rounds, FMA identifies and repairs fairness violations and functional errors. Across three repair cycles, CBS is reduced by 65.1% (from 48.4% to 16.9%), with functional correctness (Pass@attribute) increasing from 75.8% to 84%. Bias is strongly suppressed in most dimensions, though employment status bias proves more resilient, likely due to its implicit encoding via task-relevant logic rather than explicit attribute checks. Notably, these results are achieved without test oracle access, confirming the agent’s robustness and generalizability beyond Solar infrastructure.

Implications and Future Directions

Theoretical

The study demonstrates that social bias in LLM-generated code is a structural property of model parameters and not amenable to mitigation via prompt-level techniques. Bias mitigation must thus occur through architectural and external means, with explicit attribute scoping and reviewer/repairer loops as central mechanisms. The multi-agent paradigm is validated for both correctness and fairness, but only when attribute responsibilities are clearly delineated and enforced.

Practical

In deployment pipelines lacking an executable test oracle, FMA provides a practical, model-agnostic method for real-time bias detection and correction, applicable to any code generation application with task structure. Its modularity and independence from dataset-specific structure make it well-suited for professional software engineering contexts.

Open Problems

Persistent bias in domains like employment status (and, by extension, attributes with indirect or world-knowledge entanglements) highlights the need for enhanced reviewer agents with implicit conditioning detection and possibly hypothetical test case reasoning. The benchmark’s scope could be extended to multi-file, language-agnostic, and higher-order programming contexts, and FMA evaluated on newer LLMs or adversarially robust objectives.

Conclusion

This work establishes that social bias in LLM-based code generation is structurally encoded and broadly immune to prompt-level fairness interventions. Structured multi-agent workflows and, more decisively, oracle-free pipeline auditing via FMA deliver strong, quantifiable reductions in bias without sacrificing functional correctness. The paper’s rigorous empirical methodology, multi-layered workflow analysis, and demonstration of an extensible mitigation pipeline lay critical groundwork for practical fairness auditing and repair in LLM-driven code generation systems.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Collections

Sign up for free to add this paper to one or more collections.