pythonbeginner

OpenAI Content Moderation API

Screen user-generated content for policy violations using the OpenAI moderation endpoint.

python
from openai import OpenAI

client = OpenAI()

texts = [
    'How do I make a delicious pasta dish?',
    'I hate everyone and want to harm people.',  # flagged
    'Tell me about machine learning algorithms.',
]

for text in texts:
    result = client.moderations.create(input=text, model='omni-moderation-latest')
    r = result.results[0]
    if r.flagged:
        violated = [cat for cat, val in r.categories.model_dump().items() if val]
        print(f'FLAGGED: {text[:50]!r}')
        print(f'  Categories: {violated}')
    else:
        print(f'OK: {text[:50]!r}')

Use Cases

  • content safety
  • user input filtering
  • policy enforcement

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.