Prompt
I need an API with safety filters for user-generated prompts
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
Absolutely — if you want an API that accepts user-generated prompts, you should put a safety filter layer in front of your model/API call.
Recommended API flow
- Receive user prompt
- Run safety checks
- profanity / harassment
- self-harm
- sexual content
- hate / extremism
- violence
- PII / sensitive data
- prompt injection / jailbreak patterns
- Decide action
- allow
- refuse
- rewrite / sanitize
- escalate to human review
- If allowed, send to model
- Log outcome for monitoring and tuning
Example architecture
Client → Your API → Safety Filter → Policy Decision → LLM / App Logic → Response
Example FastAPI design
Request schema
from pydantic import BaseModel
class PromptRequest(BaseModel):
prompt: str
user_id: str | None = None
session_id: str | None = None
Safety filter stub
import re
BLOCKLIST = [
r"\bkill yourself\b",
r"\bmake a bomb\b",
r"\bcredit card\b",
r"\bssn\b",
]
def safety_check(prompt: str) -> dict:
text = prompt.lower()
for pattern in BLOCKLIST:
if re.search(pattern, text):
return {
"allowed": False,
"reason": "unsafe_content_detected",
"category": "policy_violation"
}
return {"allowed": True, "reason": "ok"}
API endpoint
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.post("/generate")
def generate(req: PromptRequest):
result = safety_check(req.prompt)
if not result["allowed"]:
raise HTTPException(
status_code=400,
detail=result
)
# call your model here
return {
"allowed": True,
"response": "Model output would go here."
}
Better production options
For real deployments, use a combination of:
1. Rules-based filtering
Good for:
- explicit disallowed content
- PII patterns
- known jailbreak phrases
2. Classifier / moderation model
Good for:
- nuanced hate/harassment
- self-harm
- sexual content
- violence
3. Prompt-injection detection
Look for:
- “ignore previous instructions”
- “reveal system prompt”
- “act as developer mode”
- attempts to exfiltrate secrets
4. Output filtering too
Don’t only filter input. Also inspect model output before returning it.
Policy response types
You may want your API to return one of these:
{
"status": "allowed",
"response": "..."
}
{
"status": "blocked",
"category": "self_harm",
"message": "I can’t help with that request."
}
{
"status": "review",
"message": "Your request needs review before processing."
}
Suggested safety categories
self_harmviolencehatesexual_contentminorsillicit_behaviorpiiprompt_injectionspamunknown_risk
Best practices
- Keep a policy config separate from code
- Log only what you need; avoid storing sensitive prompt data unnecessarily
- Rate-limit abusive users
- Add user/session reputation scoring
- Test with adversarial prompts
- Apply safety to both input and output
- Use human review for borderline cases
If you want, I can generate:
- a full FastAPI implementation,
- a Node.js/Express version, or
- a production-ready moderation pipeline with categories and thresholds.