Test-Time Fairness and Robustness in Large Language Models

Leonardo Cotta, Chris J. Maddison

Abstract

Frontier Large Language Models (LLMs) can be socially discriminatory or sensitive to spurious features of their inputs. Because only well-resourced corporations can train frontier LLMs, we need robust test-time strategies to control such biases. Existing solutions, which instruct the LLM to be fair or robust, rely on the model’s implicit understanding of bias. Causality provides a rich formalism through which we can be explicit about our debiasing requirements. Yet, as we show, a naive application of the standard causal debiasing strategy, counterfactual data augmentation, fails to fulfill individual-level debiasing requirements at test time. To address this, we develop stratified invariance, a flexible debiasing notion that can capture a range of debiasing requirements, from population level to individual level, through an additional measurement that stratifies the predictions. We developed a complete test for this new approach and introduced a data augmentation strategy that guarantees stratified invariance at test time under suitable assumptions, together with a prompting strategy that encourages stratified invariance in LLMs. We show that our prompting strategy, unlike implicit instructions, consistently reduces the bias of frontier LLMs across a suite of synthetic and real-world benchmarks without requiring additional data, finetuning or pre-training.

Introduction

As Large Language Models (LLMs) are increasingly deployed in high-stakes scenarios—such as hiring, medical screening, or credit scoring—their tendency to rely on "spurious features" (like gender or race) becomes a major liability. For example, a model might predict someone is a "nurse" simply because the input text uses female pronouns, reflecting societal biases present in training data.

Retraining or fine-tuning these massive models is prohibitively expensive for most users, and for closed-source models, it is impossible. Current "test-time" fixes often involve "safety prompts" (e.g., "Please be unbiased"). However, these fail because they rely on the model’s own implicit, often flawed, understanding of bias. This research argues for an explicit approach to fairness requirements using the language of causality.

Methodology: Out-Of-Context (OOC) Prompting

The core contribution of this work is Out-Of-Context (OOC) prompting, a zero-shot, test-time strategy that uses causal principles to "scrub" biased attributes from inputs and replace them with randomized alternatives.

Comparison of Standard, Safety, and OOC prompting strategies.

OOC prompting operates in three distinct stages:

  1. Obfuscation: The LLM is first asked to rewrite the input text to remove the protected attribute (e.g., changing "John" to "the person").
  2. Addition: The model then re-inserts a randomized version of that attribute (e.g., changing "the person" to "Mary" or "Bob").
  3. Aggregation: This process is repeated multiple times ($m$ samples), and the final answer is determined by a majority vote.

By separating the task from the biased context, OOC forces the model to rely on the actual substance of the query rather than demographic stereotypes.

Stratified Invariance

The authors propose Stratified Invariance as a flexible theoretical framework for fairness. While counterfactual invariance—ensuring a model gives the exact same answer if you change only one protected attribute—is often mathematically impossible to guarantee at test time, stratified invariance ensures the model's distribution of answers remains stable across different groups, conditioned on specific context.

Experimental Setup

The researchers evaluated OOC prompting across six diverse benchmarks involving nine different protected or spurious attributes:

  • CivilComments: Detecting toxicity while ignoring race, religion, or gender.
  • Bios: Predicting occupations based on biographies, where gender is the protected attribute.
  • Amazon: Evaluating review helpfulness while ignoring the sentiment of the reviewer.
  • Discrimination: Synthetic decision-making tasks involving age, race, and gender.
  • MIMIC-III: Predicting alcohol abuse history from clinical notes without biasing based on employment status.
  • Semantic Leakage: A synthetic task measuring how much unrelated context leaks into predictions.

Key Insights and Results

The paper demonstrates that causal "adjustment sets" (variables that block non-causal paths between input and output) can be used to design prompts that are much more effective than simple instructions.

Causal graphs illustrating anti-causal and causal relationships in model predictions.

Performance and Bias Reduction

  • Consistent Bias Reduction: OOC prompting reduced "SI-bias" (the metric for stratified invariance) in 23 out of 27 tested scenarios across GPT-3.5, GPT-4, and Llama-3-70B.
  • Outperforming Baselines: OOC significantly outperformed "Chain-of-Thought" (CoT) reasoning and standard safety prompts, which often increased bias or yielded inconsistent results.
  • Accuracy Retention: Crucially, the method did not degrade model performance. Macro F1-scores generally stayed within 0.05 of the standard prompting baseline.
  • Emergent Capability: OOC prompting works well even on smaller models (like the Qwen-1.5 4B/7B family), suggesting that the ability to obfuscate text emerges early in model scaling.

Conclusion and Limitations

OOC prompting represents a significant step forward in test-time debiasing by moving from implicit instructions to explicit causal interventions. However, several limitations remain:

  • Inference Cost: The multi-step process and aggregation make it roughly 5x more expensive in terms of token usage and latency.
  • Causal Knowledge: Users must identify which attributes are protected and provide descriptions for obfuscation.
  • Model Dependency: The method relies on the LLM's inherent ability to rewrite text accurately.

While OOC is a powerful tool, it is not a total guarantee of fairness and should be used with caution in sensitive policy-making domains. Future work will focus on automating the discovery of causal adjustment sets.

Summary updated

BibTeX
@article{cotta2025test,
  author = {Leonardo Cotta and Chris J. Maddison},
  title = {{Test-Time Fairness and Robustness in Large Language Models}},
  journal = {Accepted by TMLR},
  year = {2025},
  url = {https://openreview.net/forum?id=1fML4VF5FG},
}
Figure