Topic 2 of 5 · 5 min read
Moderation and safety classifier models
Goal: Place a moderation or guard model in a pipeline and set what happens on each label it returns.
A moderation or guard model is a classifier that labels text as safe or unsafe, and your code decides what happens for each label. It does not chat. It reads a prompt or a reply and returns categories such as violence, self-harm or sexual content, usually with scores.
Check yourself · 1 of 3
What does a guard model such as Llama Guard return?
Why it matters
A general chat model can refuse things, but refusal is part of the same model you are trying to protect, and it can be talked around. A separate classifier gives you a second, independent check that you can log, test and tune. It also lets you react differently by category.
Where to put it
- On the input, before the main model. Block or route abusive and policy-breaking requests, and save the cost of a full reply.
- On the output, before the user sees it. This catches harmful text the main model produced anyway, or text copied from retrieved documents.
- On both for public-facing apps. For internal tools one side may be enough.
Hosted options include the OpenAI moderation endpoint. Open-weight guard models, such as Llama Guard and ShieldGemma, run on your own servers, which matters when text cannot leave your network. Open guard models also let you state your own policy in the prompt, so categories can match your product instead of a fixed list.
Setting actions per label
Do not use one threshold for everything. Decide per category:
- Block and log for clear violations.
- Route to a human for borderline scores or sensitive categories such as self-harm, where a canned refusal can be the wrong response.
- Allow with a warning for low-risk categories your product permits.
Before and after
# Weak: one global switch
if moderation.flagged:
return "Sorry, I can't help with that."
# Better: category-specific actions and a log line
r = moderation.categories
if r["self_harm"]:
return show_support_resources(user)
if r["violence"] or r["harassment"]:
log_event("blocked", categories=r, request_id=req.id)
return "I can't help with that request."
What goes wrong
- Treating it as perfect. Classifiers miss novel phrasing and flag harmless text. Measure false positives and misses on samples of your own traffic.
- Checking only the input. A safe prompt can still produce an unsafe reply.
- Ignoring language. Many guard models are weaker outside English; test the languages your users write in.
- No fallback. If the moderation call fails or times out, decide in advance whether to fail open or closed.
Examples
Two checks on a support bot
A public support bot runs the user message through a moderation call and blocks abusive input before any model cost is spent. After the reply is generated, a guard model checks it against a policy that also bans legal advice, a rule no generic category list covers.
Fail closed or open
The moderation service times out during a traffic spike. A children's education app returns a short retry message (fail closed). An internal engineering helper lets the message through and logs it (fail open). Both choices were made in advance.
Key points
- A moderation or guard model labels text; your code decides the action for each label.
- Check inputs before the main model and outputs before the user, for public apps both.
- Open guard models run on your servers and let you state your own policy; hosted endpoints are simpler.
- Set different actions per category rather than one global threshold.
- Measure false positives and misses on your own traffic and languages, and define what happens when the call fails.
Common mistakes
- Moderating only the user's message and trusting the model's reply.
- Using one threshold and one canned refusal for every category.
- Assuming English test results hold for every language users write in.
- Leaving the failure behaviour of the moderation call undefined.
Check yourself · 2 of 3
A public chatbot checks only the user's message with a moderation endpoint. What gap remains?
Check yourself · 3 of 3
Your policy bans legal advice, which no standard moderation category covers. What fits best?
Practice items, not counted towards your mastery.
Start here
- MTEB embedding leaderboard (opens in a new tab)ToolFree
Compare embedding models by task, language and size; a shortlist, not a verdict for your data.
- Sentence Transformers documentation (opens in a new tab)DocsFree
How to run, compare and fine-tune embedding and cross-encoder models locally.
- Retrieve and re-rank (Sentence Transformers) (opens in a new tab)ArticleFree
The two-stage pattern: a fast embedding search, then a cross-encoder rerank of the top hits.
More for this topic
- Moderation guide (OpenAI) (opens in a new tab)DocsFree
What a hosted moderation endpoint returns: categories, scores and how to act on them.
- Llama Guard 3 model card (opens in a new tab)DocsFree
An open guard model: its hazard categories, prompt format and stated limits.