SuperAPI AI Directory

langfair

Python library for LLM bias and fairness assessments.

Open website

About this tool

LangFair is a Python library for use-case-level bias and fairness evaluation of large language model (LLM) applications. Its core premise is that static benchmark evaluations often struggle to cover the risks of LLMs in various real-world applications, and that ignoring use-case-specific prompts may lead to misleading conclusions about bias and fairness. Therefore, LangFair adopts a "Bring Your Own Prompts (BYOP)" approach, allowing users to customize evaluations according to their own use cases.

In terms of main capabilities, LangFair focuses on output-based metrics and does not require access to model internal states, making it suitable for governance audits and real-world testing. The evaluation metrics it supports cover multiple categories, including toxicity metrics (Expected Maximum Toxicity, Toxicity Probability, Toxic Fraction, etc.), counterfactual fairness metrics (Strict/W Weak Counterfactual Sentiment Parity, Counterfactual Cosine Similarity Score, Counterfactual BLEU, Counterfactual ROUGE-L, etc.), stereotype metrics (Stereotypical Associations, Co-occurrence Bias Score, Stereotype classifier metrics, etc.), recommendation fairness metrics (Jaccard Similarity, Search Result Page Misinformation Score, Pairwise Ranking Accuracy Gap, etc.), and classification fairness metrics (Predicted Prevalence Rate Disparity, False Negative Rate Disparity, False Omission Rate Disparity, False Positive Rate Disparity, False Discovery Rate Disparity, etc.). In addition, it also provides adversarial red teaming evaluation for assessing worst-case toxicity and counterfactual generation.

In terms of usage, users can install the latest version from PyPI, and it is recommended to use venv to create a virtual environment. ExampleThe code shows how to use the ResponseGenerator class together with a LangChain LLM object to generate responses (for example, generating 25 responses per prompt, consistent with toxicity evaluation conventions), and then use ToxicityMetrics, StereotypeMetrics, CounterfactualGenerator, and CounterfactualMetrics to calculate metrics respectively. To simplify the process, the AutoEval class can complete multi-step evaluation for text generation and summarization use cases in two lines of code; the SelfEval class provides the same evaluation but without requiring a LangChain LLM, with users generating responses themselves. During construction, it checks protected attribute words in prompts, creates counterfactual prompt variants, and exposes the complete prompt list through get_prompts(). After users generate one response per prompt in order, they pass them to evaluate to calculate metrics.

Applicable scenarios include bias and fairness evaluation for LLM use cases such as text generation, summarization, classification, and recommendation systems, as well as scenarios requiring governance audits and red team testing. In terms of limitations, the official materials do not explicitly list specific limitations, but emphasize that its metrics are output-based and do not depend on internal states; at the same time, evaluation effectiveness depends on the quality and representativeness of the prompts provided by users. The project was developed and open-sourced by CVS Health, and provides resources such as a documentation site, example notebooks, and research papers.

Classification: Based on its explicit purpose, LangFair belongs to AI development platforms (ai-frameworks).