Skip to main content

Using the Moderation API for Content Safety

In this lesson, you’ll learn how to use OpenAI’s Moderation API to detect potentially harmful or policy-violating content in user input.

Prerequisites

Install the OpenAI SDK:

pip install openai

The Code

from openai import OpenAI
client = OpenAI()

response = client.moderations.create(
model="omni-moderation-latest",
input="I am going to kill you."
)

print(response.to_json)

Explanation

  • model="omni-moderation-latest": Uses the latest available moderation model.
  • input: The text to analyze for policy violations.
  • response.to_json: Returns a structured JSON output indicating flagged categories and scores.

Use Case

Best for:

  • Pre-filtering user-generated content
  • Flagging messages before showing them in chat
  • Compliance checks for app safety and moderation workflows