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])
Response
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:

CategoryCoversRecommended
Sexual contentExplicit sexual text or images involving adultsBlock
Sexual content involving minorsAny sexual content that involves a childBlock
NudityNude or partly nude imagesFlag
Hate and harassmentAttacks on people for who they are, threats and bullyingBlock
ViolenceGraphic violence, violent threats and extremismFlag
Self-harmSuicide, self-injury and eating disordersFlag
Weapons and illegal activityWeapons, drugs and help with crimesBlock
Malware and cyber attacksWriting malware or breaking into systemsBlock
Fraud and forged documentsScams, phishing and fake documentsBlock
MisinformationFalse news and fake images of real eventsFlag

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:

Response
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.action set to "flagged". In the Python SDK, read it as response.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 409 moderation_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.
For every field, model and error, see the Moderations API reference.