Implementation:Openai Openai node Moderations Resource
| Knowledge Sources | |
|---|---|
| Domains | SDK, Content Moderation, Safety |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Moderations resource class provides a method for classifying text and image inputs as potentially harmful across multiple content categories.
Description
The Moderations class extends APIResource and exposes a single create method that sends content to the OpenAI moderation endpoint (/moderations). It accepts text strings, arrays of strings, or multimodal inputs combining text and images. The API returns detailed classification results across 13 content categories including harassment, hate speech, self-harm, sexual content, violence, and illicit activities.
Each moderation result includes three complementary data structures: categories with boolean flags indicating whether content was flagged in each category, category_scores with numerical confidence scores (0 to 1) for each category, and category_applied_input_types indicating which input types (text or image) contributed to each category score. The top-level flagged boolean provides a quick check for whether any category was triggered.
The module supports two model families: the omni-moderation models that handle both text and image inputs, and the text-moderation models for text-only classification. Image inputs are provided via the ModerationImageURLInput type, which accepts either direct URLs or base64-encoded data URLs.
Usage
Use this resource to screen user-generated content for policy violations before displaying or processing it. It is commonly integrated into content pipelines, chat applications, and user-facing platforms to detect harmful content such as harassment, hate speech, self-harm content, sexual material, violence, and illicit activity.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/moderations.ts
Signature
export class Moderations extends APIResource {
create(body: ModerationCreateParams, options?: RequestOptions): APIPromise<ModerationCreateResponse>;
}
Import
import OpenAI from 'openai';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | Array<string> | Array<ModerationMultiModalInput> | Yes | Text or multimodal content to classify |
| model | ModerationModel | No | Model to use: omni-moderation-latest, omni-moderation-2024-09-26, text-moderation-latest, or text-moderation-stable |
Outputs
| Name | Type | Description |
|---|---|---|
| id | string |
Unique identifier for the moderation request |
| model | string |
The model used to generate results |
| results | Array<Moderation> |
List of moderation objects, one per input |
| results[].flagged | boolean |
Whether any category is flagged |
| results[].categories | Moderation.Categories |
Boolean flags for each of 13 content categories |
| results[].category_scores | Moderation.CategoryScores |
Numerical scores (0-1) for each category |
| results[].category_applied_input_types | Moderation.CategoryAppliedInputTypes |
Input types that each score applies to |
Content Categories
| Category | Description | Input Types |
|---|---|---|
| harassment | Harassing language towards any target | text |
| harassment/threatening | Harassment including violence or serious harm | text |
| hate | Hate based on protected characteristics | text |
| hate/threatening | Hateful content with violence towards targeted groups | text |
| illicit | Instructions for wrongdoing or illicit acts | text |
| illicit/violent | Illicit instructions including violence or weapons | text |
| self-harm | Content promoting acts of self-harm | text, image |
| self-harm/instructions | Instructions for self-harm acts | text, image |
| self-harm/intent | Expressions of self-harm intent | text, image |
| sexual | Sexually explicit content | text, image |
| sexual/minors | Sexual content involving minors | text |
| violence | Content depicting death, violence, or injury | text, image |
| violence/graphic | Graphic depictions of violence | text, image |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI();
// Moderate a single text string
const result = await client.moderations.create({
input: 'Some text to check for policy violations',
});
console.log(result.results[0].flagged);
console.log(result.results[0].category_scores.harassment);
// Moderate multiple text inputs
const batchResult = await client.moderations.create({
input: ['First message', 'Second message'],
model: 'omni-moderation-latest',
});
for (const moderation of batchResult.results) {
if (moderation.flagged) {
console.log('Content flagged:', moderation.categories);
}
}
// Moderate multimodal input (text + image)
const multiModalResult = await client.moderations.create({
input: [
{ type: 'text', text: 'Check this image' },
{ type: 'image_url', image_url: { url: 'https://example.com/image.png' } },
],
model: 'omni-moderation-latest',
});
Key Types
ModerationModel
type ModerationModel =
| 'omni-moderation-latest'
| 'omni-moderation-2024-09-26'
| 'text-moderation-latest'
| 'text-moderation-stable';
ModerationMultiModalInput
type ModerationMultiModalInput = ModerationImageURLInput | ModerationTextInput;
interface ModerationImageURLInput {
image_url: { url: string };
type: 'image_url';
}
interface ModerationTextInput {
text: string;
type: 'text';
}