Comprehensive, actionable rules for building, deploying and monitoring ethical, transparent and compliant AI/ML systems.
Your AI models are performing well in testing but creating real-world harm. Bias in hiring algorithms, unexplainable loan denials, privacy violations—these aren't edge cases anymore. They're expensive liability risks that can shut down entire product lines.
You're building sophisticated ML systems, but without proper ethical governance, you're essentially deploying technical debt that compounds with every user interaction. Consider these real scenarios:
The EU AI Act is now law. GDPR fines reach hundreds of millions. Your next model deployment could trigger a compliance audit that reveals systematic ethical violations across your entire AI pipeline.
These Cursor Rules implement a comprehensive ethical AI framework that integrates fairness testing, explainability, and compliance monitoring directly into your Python development workflow. Instead of bolting on ethics as an afterthought, you'll build transparent, auditable, and compliant AI systems from day one.
The rules establish automated bias detection in your CI pipeline, standardized explainability endpoints, privacy-preserving techniques, and governance integration with frameworks like Credo AI and IBM Watsonx—all while maintaining the development velocity you need.
# Deploy model, hope for the best
model.fit(X_train, y_train)
model.save('production_model.pkl')
# Ethics review happens weeks later, if at all
Result: Model deployed with 12% disparate impact, discovered during audit, emergency rollback, $2M compliance fine.
# Automatic bias detection and governance
with mlflow.start_run(run_name="loan_model_v15"):
model.fit(X_train, y_train)
# Built-in fairness validation
fairness_metrics = evaluate_fairness(model, X_test, y_test, sensitive_attrs)
if fairness_metrics['disparate_impact'] < 0.8:
raise EthicalAIError("Disparate impact exceeds threshold")
# Auto-generate explainability
explainer = shap.TreeExplainer(model)
mlflow.log_artifact(explainer, "model_explainer")
# Policy validation before deployment
policy_result = credoai.scan(model, fairness_metrics)
if not policy_result.compliant:
block_deployment(policy_result.violations)
Result: Compliant model deployed with automated documentation, continuous monitoring, and audit trail.
# Federated learning for sensitive healthcare data
import tensorflow_federated as tff
@tff.federated_computation
def federated_train(federated_data):
# Train across hospitals without data sharing
return tff.learning.build_federated_averaging_process(
model_fn=create_keras_model,
client_optimizer_fn=lambda: tf.keras.optimizers.SGD(0.02)
)
# Differential privacy for user analytics
from tensorflow_privacy import DPKerasSGDOptimizer
optimizer = DPKerasSGDOptimizer(
l2_norm_clip=1.0,
noise_multiplier=1.1,
num_microbatches=250
)
Impact: GDPR-compliant model training with mathematical privacy guarantees.
# Core ethical AI dependencies
pip install aif360 fairlearn shap lime captum
pip install credoai tensorflow-federated tensorflow-privacy
pip install mlflow dvc prometheus-client
Add these rules to your .cursor-rules file and set up the directory structure:
src/
data_loaders/
preprocessing/
models/
fairness/ # Bias detection and mitigation
explainability/ # SHAP/LIME integration
pipelines/ # End-to-end ethical workflows
# credo-ai-policy.yaml
version: "1.0"
risk_level: "high"
fairness_thresholds:
disparate_impact: 0.8
equalized_odds: 0.1
explainability:
required: true
methods: ["shap", "lime"]
privacy:
differential_privacy: true
epsilon: 1.0
# tests/test_fairness.py
def test_model_fairness():
"""Ensure model meets fairness thresholds."""
from aif360.metrics import BinaryLabelDatasetMetric
metric = BinaryLabelDatasetMetric(
test_data,
privileged_groups=[{'gender': 1}],
unprivileged_groups=[{'gender': 0}]
)
assert metric.disparate_impact() >= 0.8, "Fails disparate impact test"
# Prometheus metrics for production monitoring
from prometheus_client import Counter, Histogram
fairness_violations = Counter('ai_fairness_violations_total')
prediction_latency = Histogram('ai_prediction_duration_seconds')
@prediction_latency.time()
def predict_with_monitoring(features):
prediction = model.predict(features)
# Real-time bias detection
if detect_bias_drift(features, prediction):
fairness_violations.inc()
alert_ethics_team(features, prediction)
return prediction
These rules transform ethical AI from a checkbox exercise into a competitive advantage. You'll ship compliant models faster, reduce regulatory risk, and build AI systems that stakeholders actually trust.
Your next model deployment doesn't have to be a compliance gamble. With built-in ethical governance, transparent decision-making, and automated bias detection, you can focus on solving problems while knowing your AI systems meet the highest ethical standards.
The frameworks are mature. The tooling is production-ready. The regulatory environment demands it. The only question is whether you'll implement ethical AI governance before or after your next compliance incident.
You are an expert in Ethical-AI Development using Python, TensorFlow, PyTorch, Scikit-learn, JAX, Responsible-AI toolkits (AI Fairness 360, LIME, SHAP), governance platforms (Credo AI, IBM Watsonx.governance) and Model-Ops tooling (MLflow, DVC).
Technology Stack Declaration
- Core language: Python 3.11+ with typing and pyproject-toml builds.
- Deep-learning: TensorFlow 2.x (Keras API) & PyTorch 2.x (torch-compile).
- Classical ML: Scikit-learn 1.4+.
- Explainability: SHAP, LIME, Captum.
- Fairness/Bias: AI Fairness 360, Fairlearn.
- Governance: Credo AI, IBM Watsonx.governance, NIST AI RMF tooling.
- Experiment tracking: MLflow, Weights & Biases.
- Data versioning: DVC or Git-LFS.
- Infrastructure: Docker, Kubernetes, Terraform, Azure ML or Vertex AI.
Key Principles
- Transparency-by-Design: log every model decision path; expose feature attributions through explainability endpoints.
- Fairness First: integrate bias detection in CI pipelines; block promotion on significant disparate impact (≥5 pp).
- Privacy Preservation: apply differential privacy or federated learning when handling sensitive data; comply with GDPR/CCPA.
- Human-in-the-Loop: require manual approval for models deployed to production tiers affecting human welfare (finance, healthcare, HR).
- Continuous Governance: align with EU AI Act, OECD AI Principles & NIST AI RMF; schedule quarterly ethical audits.
- Least Surprise: write deterministic, reproducible code; pin random seeds and container images.
Python
- Follow PEP 8/PEP 484 with black & isort enforced in pre-commit.
- Use dataclasses or pydantic models for structured data instead of untyped dicts.
- Prefer vectorised NumPy/Pandas operations; avoid hidden state in classes—stateless functional pipelines where feasible.
- Always log metadata via MLflow (run, version, parameters, metrics, tags). Example:
```python
with mlflow.start_run(run_name="loan_default_rf_v15"):
mlflow.log_params(model.get_params())
mlflow.log_metrics({"roc_auc": auc, "dp_ratio": dp})
```
- Keep randomness controlled:
```python
import numpy as np, torch, random
seed = 42
random.seed(seed); np.random.seed(seed); torch.manual_seed(seed)
```
- Directory schema (lower-kebab-case):
```text
src/
data_loaders/
preprocessing/
models/
fairness/
explainability/
pipelines/
```
Error Handling and Validation
- Place risk checks at function tops; raise custom EthicalAIError.
```python
if not report.fairness_passed:
raise EthicalAIError("Disparate impact exceeds threshold; deployment aborted")
```
- Catch third-party exceptions, wrap with context & log to governance tool (Credo AI incident).
- Implement redress hooks: on_failure callbacks send Slack + Jira tickets.
- Use pydantic validation on all external inputs (API/CLI) to prevent injection & type errors.
Framework-Specific Rules
TensorFlow / Keras
- Wrap model logic in tf.function with autograph disabled for easier tracing.
- Use model.add_metric for fairness metrics so they are part of training history.
- Enable `tf.keras.mixed_precision.set_global_policy('bf16')` only after privacy budget analysis.
PyTorch
- Use torch.compile for production but export fallback TorchScript for audit snapshots.
- Isolate preprocessing in `torch.utils.data.Dataset`; never modify data in __getitem__ after the first epoch (reproducibility).
- Capture feature importance with Captum and log to MLflow artifacts.
Governance Framework Integration
- Map each pipeline stage to EU AI Act risk levels; block auto-deploy for High-Risk systems.
- Maintain Credo AI policy YAML in repo root; validate in CI using `credoai scan`.
Additional Sections
Testing & Auditing
- Unit tests via pytest and hypothesis; coverage ≥90%.
- Fairness tests:
```python
from aif360.metrics import BinaryLabelDatasetMetric
di = BinaryLabelDatasetMetric(data, privileged_groups, unprivileged_groups)
assert di.disparate_impact() >= 0.8
```
- Adversarial robustness: run TextAttack with 100 attacks per sample; fail build if accuracy drops >5%.
Security & Privacy
- Encrypt PII at rest (AES-256) and in transit (TLS 1.3).
- Strip & hash user IDs before logging.
- Use TensorFlow Federated or PySyft for cross-site training involving sensitive data.
Performance & Monitoring
- Add Prometheus metrics: latency, throughput, fairness drift, data drift (Kolmogorov-Smirnov p-value).
- Canary deployments for new models with <10% traffic until post-deployment audit passes.
Documentation
- Auto-generate model cards (framework: TensorFlow Model Card Toolkit) on every MLflow run.
- Provide plain-language explanations for stakeholders; include limitations & intended use.
Common Pitfalls & Guards
- Never deploy unexplainable black-box models in high-risk domains without surrogate explainers & approval.
- Avoid proxy variables that correlate with protected attributes (e.g., ZIP code → race). Include explicit correlation tests.
- Do not omit minority class sampling; enforce stratified splits with minimum 5% representation.
Release Checklist
- [ ] Reproducibility: Dockerfile & seed fixed
- [ ] Fairness metrics within thresholds
- [ ] Explainability artifacts generated
- [ ] Privacy impact assessment complete
- [ ] Human oversight plan documented
- [ ] Signed-off by ethics committee