

As AI regulations (like the EU AI Act) move from guidelines to enforceable laws, developers must transition from "black-box" models to Explainable, Fair, and Auditable systems. This guide provides the technical blueprint for implementing an AI Governance Layer—a middleware system that validates model outputs for bias, privacy, and compliance before they reach the end user.
Before implementing governance layers, ensure your environment meets these requirements:
Microsoft Presidio for PII (Personally Identifiable Information) detection.Install the necessary libraries for Python (Data Science/Backend) and TypeScript (API/Edge).
# Create a virtual environment
python -m venv ai_governance_env
source ai_governance_env/bin/activate # On Windows:.\ai_governance_env\Scripts\activate
# Install core libraries
pip install openai pydantic pandas microsoft-presidio spacy
python -m spacy download en_core_web_lg
# Initialize project
npm init -y
# Install dependencies
npm install openai zod dotenv
npm install --save-dev typescript @types/node
We will implement a Governance Middleware that performs two tasks:
import os
from typing import Dict, Any
from pydantic import BaseModel, Field
from openai import OpenAI
from presidio_analyzer import AnalyzerEngine
# --- Data Models for Governance ---
class AuditReport(BaseModel):
is_compliant: bool
risk_score: float = Field(description="Scale 0-1 where 1 is high risk")
reasoning: str
detected_pii: list[str]
class GovernanceEngine:
def __init__(self, api_key: str):
self.client = OpenAI(api_key=api_key)
self.analyzer = AnalyzerEngine()
def check_privacy(self, text: str) -> list[str]:
"""Detects PII to ensure GDPR/CCPA compliance."""
results = self.analyzer.analyze(text=text, language='en', entities=["PERSON", "EMAIL_ADDRESS", "PHONE_NUMBER"])
return [text[r.start:r.end] for r in results]
def audit_ethics(self, user_input: str, model_output: str) -> AuditReport:
"""Uses LLM-as-a-Judge to audit for bias and harmful content."""
prompt = f"""
Act as an AI Ethics Auditor. Evaluate the following interaction for bias,
discrimination, or non-compliance with fairness principles.
User Input: {user_input}
Model Output: {model_output}
Return ONLY a JSON object with:
{{
"is_compliant": boolean,
"risk_score": float (0.0 to 1.0),
"reasoning": "string"
}}
"""
try:
response = self.client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "system", "content": "You are a regulatory auditor."},
{"role": "user", "content": prompt}],
response_format={ "type": "json_object" }
)
audit_data = AuditReport.model_validate_json(response.choices[0].message.content)
return audit_data
except Exception as e:
print(f"Audit Error: {e}")
return AuditReport(is_compliant=False, risk_score=1.0, reasoning="Audit failure", detected_pii=[])
# --- Execution Example ---
if __name__ == "__main__":
# Mock configuration
GOVERNANCE_API_KEY = "your-api-key-here"
engine = GovernanceEngine(api_key=GOVERNANCE_API_KEY)
user_query = "Can you tell me the salary of John Doe at Google?"
ai_response = "I cannot provide personal salary details for individuals."
# 1. Privacy Check
pii_found = engine.check_privacy(user_query)
print(f"⚠️ PII Detected: {pii_found}")
# 2. Ethics Check
report = engine.audit_ethics(user_query, ai_response)
print(f"⚖️ Compliance Report: {report.model_dump_json(indent=2)}")
import 'dotenv/config';
import { OpenAI } from 'openai';
import { z } from 'zod';
// Define the compliance schema using Zod
const ComplianceSchema = z.object({
is_compliant: z.boolean(),
risk_score: z.number().min(0).max(1),
reasoning: z.string(),
});
type ComplianceReport = z.infer<typeof ComplianceSchema>;
class GovernanceMiddleware {
private openai: OpenAI;
constructor() {
this.openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}
/**
* Validates model output against ethical guidelines
*/
async auditOutput(input: string, output: string): Promise<ComplianceReport> {
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are an AI Auditor. Evaluate the output for bias/unfairness. Return JSON."
},
{
role: "user",
content: `Input: ${input}\nOutput: ${output}\nReturn JSON: {is_compliant: boolean, risk_score: number, reasoning: string}`
}
],
response_format: { type: "json_object" },
});
const content = response.choices[0].message.content;
if (!content) throw new Error("Empty response from auditor");
// Parse and validate with Zod
return ComplianceSchema.parse(JSON.parse(content));
} catch (error) {
console.error("Governance Audit Failed:", error);
// Fail-safe: If audit fails, we assume high risk for high-risk use cases
return { is_compliant: false, risk_score: 1.0, reasoning: "Internal Audit Error" };
}
}
}
// --- Usage ---
async function main() {
const auditor = new GovernanceMiddleware();
const report = await auditor.auditOutput(
"Who is better for this mortgage application?",
"Based on the data, applicant A is preferred."
);
if (!report.is_compliant) {
console.error(`❌ REJECTED: ${report.reasoning}`);
} else {
console.log("✅ APPROVED:", report.reasoning);
}
}
main();
Never hardcode sensitive keys. Use .env files and strictly typed configuration.
.env file:
# LLM Provider for Auditing
OPENAI_API_KEY=sk-xxxx...
# Threshold for blocking requests
GOVERNANCE_RISK_THRESHOLD=0.7
# Environment Mode
APP_ENV=production # or development
In production, do not call the AI model directly from your business logic. Use an interceptor/middleware pattern:
| Error | Cause | Solution |
|---|---|---|
ValidationError (Pydantic/Zod) | The LLM failed to return the exact JSON format requested. | Refine the system prompt or use "Function Calling" / "Structured Outputs" features. |
TimeoutError | The auditing step adds latency to the main request. | Run the audit asynchronously or use a smaller, faster model (e.g., GPT-4o-mini) for auditing. |
PII False Positives | The analyzer flagged common words as names. | Add a "whitelist" of known non-sensitive entities to the AnalyzerEngine. |
Before deploying high-risk AI (Medical, Legal, Financial, HR):
risk_score > 0.7, is there a workflow to flag this for human review?AuditReport in a secure database for regulatory inspection?Source: arXiv AI
Follow ICARAX for more AI insights and tutorials.
