# bias_guardrail.py

> Source: <https://dev.to/maria_josegonzalezantel_80/biasguardrailpy-f9j>
> Published: 2026-08-04 08:20:52+00:00

Designing Real‑Time Safety and Bias Guardrails for Generative AI Career Advisors to Meet UK Online Safety Act and DSA Requirements

Meta: Learn how to embed real‑time safety and bias guardrails in generative AI career advisors to comply with UK OSA and DSA, with actionable code patterns.

`unitary/toxic-bert`

) can be wrapped in a lightweight microservice that returns a safety score within 150 ms.
The UK Online Safety Act (OSA) places a duty of care on platforms that host user‑generated content, requiring proactive detection and removal of harmful material, including harassment, hate speech, and biased advice that could impede equal opportunity. The EU Digital Services Act (DSA) mirrors this obligation for very large online platforms, mandating transparent risk assessments, independent audits, and swift takedown procedures for illegal content. For a generative AI career advisor, the risk surface includes:

Both frameworks require *real‑time* intervention: the platform must assess and act on content before it reaches the user, not merely rely on post‑publication moderation. This shifts the guardrail from a retrospective filter to an inline validation step in the generation pipeline.

To satisfy OSA/DSA while preserving low latency, I advocate a three‑layered approach:

Each layer emits structured audit events (user‑ID, timestamp, safety score, action taken) to an immutable log (AWS CloudWatch Logs + S3 Glacier for long‑term retention), satisfying the DSA’s transparency and traceability requirements.

Bias in career advice often manifests as stereotypical associations (e.g., “nursing” → female, “engineering” → male). I use a lightweight bias classifier fine‑tuned on the **Bias Benchmark for QA (BBQ)** dataset, exported as a TensorFlow SavedModel and served via AWS Lambda. The classifier returns a bias probability per protected attribute (gender, ethnicity, age, disability).

``` python
# bias_guardrail.py
import json
import boto3
import numpy as np
import tensorflow as tf

# Load model once per container
model = tf.keras.models.load_model("/opt/bias_model")

def detect_bias(text: str) -> dict:
    """Return bias scores for protected attributes."""
    # Simple tokenization – replace with your NLP pipeline
    tokens = text.lower().split()
    # Pad/truncate to model input size (e.g., 128)
    seq = tf.keras.preprocessing.sequence.pad_sequences(
        [tokens], maxlen=128, padding='post'
    )
    preds = model.predict(seq)[0]  # shape: (num_attributes,)
    attributes = ["gender", "ethnicity", "age", "disability"]
    return {attr: float(score) for attr, score in zip(attributes, preds)}

def lambda_handler(event, context):
    body = json.loads(event["body"])
    user_text = body.get("prompt", "")
    scores = detect_bias(user_text)
    # Flag if any attribute exceeds 0.7 threshold
    flagged = any(v > 0.7 for v in scores.values())
    return {
        "statusCode": 200,
        "body": json.dumps({
            "bias_scores": scores,
            "flagged": flagged,
            "action": "block" if flagged else "allow"
        })
    }
```

The Lambda is placed **before** the LLM call. If `flagged`

is true, the orchestrator returns a pre‑written, bias‑mitigated response (e.g., “I’m unable to provide advice based on protected characteristics; here’s a neutral alternative…”) and logs the event for DSA audits.

For toxicity, profanity, and harassment, I integrate the **Perspective API** (Google) as a fallback and a locally hosted `unitary/toxic-bert`

model for GDPR‑compliant data residency. The service returns a toxicity score (0‑1). A score > 0.8 triggers a block.

``` python
# toxicity_guardrail.py
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import boto3
import json

TOKENIZER = AutoTokenizer.from_pretrained("unitary/toxic-bert")
MODEL = AutoModelForSequenceClassification.from_pretrained("unitary/toxic-bert")
MODEL.eval()

def toxicity_score(text: str) -> float:
    inputs = TOKENIZER(text, return_tensors="pt", truncation=True, max_length=128)
    with torch.no_grad():
        logits = MODEL(**inputs).logits
        probs = torch.softmax(logits, dim=-1)
        # Assuming label 1 = toxic
        return probs[0, 1].item()

def lambda_handler(event, context):
    body = json.loads(event["body"])
    user_text = body.get("prompt", "")
    score = toxicity_score(user_text)
    flagged = score > 0.8
    return {
        "statusCode": 200,
        "body": json.dumps({
            "toxicity_score": score,
            "flagged": flagged,
            "action": "block" if flagged else "allow"
        })
    }
```

Both guardrails are invoked via **AWS Step Functions**, which orchestrates the sequence: prompt → bias check → toxicity check → LLM generation → post‑gen moderation → user response. Each step writes a JSON audit record to CloudWatch Logs.

A serverless stack offers automatic scaling, pay‑per‑use pricing, and native integration with logging services. Below is a condensed AWS SAM template that provisions the required resources.

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Guardrails for Generative AI Career Advisor

Globals:
  Function:
    Timeout: 10
    MemorySize: 512
    Runtime: python3.12
    Handler: index.lambda_handler

Resources:
  BiasCheckFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: bias_guardrail/
      Policies:
        - Statement:
            Effect: Allow
            Action: logs:CreateLogGroup
            Resource: "*"
  ToxicityCheckFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: toxicity_guardrail/
      Policies:
        - Statement:
            Effect: Allow
            Action: logs:CreateLogGroup
            Resource: "*"
  GenerationFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: llm_generator/
      Environment:
        Variables:
          MODEL_ENDPOINT: !GetAtt LlmEndpoint.Attributes.Endpoint
      Policies:
        - Statement:
            Effect: Allow
            Action: sagemaker:InvokeEndpoint
            Resource: "*"
  PostGenModerationFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: post_gen_moderation/
      Policies:
        - Statement:
            Effect: Allow
            Action: logs:CreateLogGroup
            Resource: "*"
  GuardrailStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: statemachine/
      DefinitionSubstitutions:
        BiasCheckFunctionArn: !GetAtt BiasCheckFunction.Arn
        ToxicityCheckFunctionArn: !GetAtt ToxicityCheckFunction.Arn
        GenerationFunctionArn: !GetAtt GenerationFunction.Arn
        PostGenModerationFunctionArn: !GetAtt PostGenModerationFunction.Arn
      Policies:
        - Statement:
            Effect: Allow
            Action: lambda:InvokeFunction
            Resource: !Join [
                "",
                [
                  !GetAtt BiasCheckFunction.Arn,
                  ",",
                  !GetAtt ToxicityCheckFunction.Arn,
                  ",",
                  !GetAtt GenerationFunction.Arn,
                  ",",
                  !GetAtt PostGenModerationFunction.Arn,
                ],
              ]
Outputs:
  StateMachineArn:
    Description: ARN of the Step Functions orchestrator
    Value: !GetAtt GuardrailStateMachine.Arn
```

The state machine ensures **exactly‑once** execution and captures the input/output of each step in its execution history, which can be exported to S3 for DSA‑required impact assessments.

Compliance is not a one‑time setup. I recommend:

`flagged`

metrics (bias > 0.7, toxicity > 0.8).
All logs are retained for **24 months** in S3 Glacier Deep Archive, satisfying both GDPR’s storage limitation principle (by encrypting and restricting access) and DSA’s transparency obligations.

Implementing these guardrails yields measurable outcomes:

At CVChatly we already provide a conversational AI avatar that transforms every professional profile into a 24/7 recruiter‑ready showcase. By embedding the guardrail architecture described above, we ensure that the avatar’s recommendations remain **unbiased, safe, and fully compliant** with the UK Online Safety Act and DSA. This turns a powerful engagement tool into a trustworthy career partner that scales globally without legal exposure.

Learn more about how CVChatly can power your talent platform: [https://www.cvchatly.com](https://www.cvchatly.com)

How have you approached real‑time safety and bias mitigation in generative AI systems? Which open‑source models or cloud services have you found most effective for balancing compliance with low latency? Share your experiences and any lessons learned in the comments below.

*Author Bio*

Maria José González Antelo is a CPO and ICT Project Director with over 20 years of experience leading AI‑powered product strategies and compliance‑first architectures. She has scaled platforms to millions of users while navigating GDPR, UK OSA, and DSA requirements, and now adv
