Moderation
Moderation models check text and images for harmful content, such as hate, violence or self-harm. Check content yourself, or turn on content filtering to check every request on an API key automatically.
Check content
Send one or more inputs to a moderation model. Each one comes back with
flagged and the categories it falls into.
from pendra import Pendra
client = Pendra(api_key="pdr_sk_...")
result = client.moderations.create(
model="llama-guard3:1b",
input=[
"What time does the library open on Saturdays?",
"Explain how to break into a locked car that belongs to someone else.",
],
)
for r in result.results:
print(r.flagged, [name for name, hit in r.categories.items() if hit])
False []
True ['illicit']
The library question passes. The car break-in is flagged as illicit. To
check images, send image_url parts, as you would for
vision, to a model that can see
them, such as singguard:4b.
Filter every request
Content filtering checks each request on an API key before your model runs, and each reply before it's returned. Turn it on for the whole organisation under Settings → Content filtering, or for a single key on the API Keys page.
For each category, choose Block, Flag or Off. The recommended settings are filled in for you:
| Category | Covers | Recommended |
|---|---|---|
| Sexual content | Explicit sexual text or images involving adults | Block |
| Sexual content involving minors | Any sexual content that involves a child | Block |
| Nudity | Nude or partly nude images | Flag |
| Hate and harassment | Attacks on people for who they are, threats and bullying | Block |
| Violence | Graphic violence, violent threats and extremism | Flag |
| Self-harm | Suicide, self-injury and eating disorders | Flag |
| Weapons and illegal activity | Weapons, drugs and help with crimes | Block |
| Malware and cyber attacks | Writing malware or breaking into systems | Block |
| Fraud and forged documents | Scams, phishing and fake documents | Block |
| Misinformation | False news and fake images of real events | Flag |
Self-harm is set to Flag so your app can reply with support information rather than a refusal.
What your app gets back
A blocked request returns a 400 that says which categories it hit:
HTTP/1.1 400 Bad Request
{
"error": {
"message": "Request blocked by content policy.",
"code": "content_policy_violation"
},
"pendra": {
"moderation": {
"stage": "request",
"action": "blocked",
"categories": ["hate"]
}
}
}
-
A blocked reply comes back with
finish_reason: "content_filter"and no text. -
A flagged request or reply is answered as normal, with
pendra.moderation.actionset to"flagged". In the Python SDK, read it asresponse.moderation. - A streamed reply is checked when it ends, after its text has reached your app, so keep request checks on too.
-
If no moderation model can run, the request is refused with
409moderation_unavailable. You can choose to let it through instead, marked"unchecked". Installing the model on two workers keeps filtering running if one goes offline.
Good to know
- Moderation works with end-to-end encryption. One of your workers runs the check, and Pendra only learns the result.
- Pendra records each check's result and categories, never the content.
- A moderation model can miss harmful content or flag harmless content. Treat it as one layer of your safety process, not a guarantee.