Implementation:Openai Openai python Moderations Resource
| Knowledge Sources | |
|---|---|
| Domains | Content_Safety |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for the Moderation API resource provided by the openai-python SDK.
Description
The moderations module implements the Moderations (sync) and AsyncModerations (async) resource classes for the POST /moderations endpoint. The create() method accepts an input parameter that can be a single string, a sequence of strings, or an iterable of ModerationMultiModalInputParam objects (for combined text and image moderation). An optional model parameter selects the moderation model. The method transforms the input using maybe_transform() against ModerationCreateParams and returns a ModerationCreateResponse containing flagged categories and scores. Both classes provide with_raw_response and with_streaming_response cached properties that wrap the resource in ModerationsWithRawResponse, AsyncModerationsWithRawResponse, ModerationsWithStreamingResponse, or AsyncModerationsWithStreamingResponse for accessing raw HTTP response data or streaming responses. This file is auto-generated from the OpenAPI spec by Stainless.
Usage
Use this resource to classify text and/or image content for potentially harmful categories (hate, violence, self-harm, sexual, etc.) before displaying or processing user-generated content.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/resources/moderations.py
- Lines: 1-197
Signature
class Moderations(SyncAPIResource):
@cached_property
def with_raw_response(self) -> ModerationsWithRawResponse: ...
@cached_property
def with_streaming_response(self) -> ModerationsWithStreamingResponse: ...
def create(
self,
*,
input: Union[str, SequenceNotStr[str], Iterable[ModerationMultiModalInputParam]],
model: Union[str, ModerationModel] | Omit = omit,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = not_given,
) -> ModerationCreateResponse: ...
class AsyncModerations(AsyncAPIResource):
async def create(
self,
*,
input: Union[str, SequenceNotStr[str], Iterable[ModerationMultiModalInputParam]],
model: Union[str, ModerationModel] | Omit = omit,
...
) -> ModerationCreateResponse: ...
Import
from openai.resources.moderations import Moderations, AsyncModerations
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | str / SequenceNotStr[str] / Iterable[ModerationMultiModalInputParam] | Yes | Text or multi-modal content to classify |
| model | str / ModerationModel / Omit | No | Moderation model to use (omit for default) |
| extra_headers | Headers or None | No | Additional HTTP headers |
| extra_query | Query or None | No | Additional query parameters |
| extra_body | Body or None | No | Additional JSON body properties |
| timeout | float / httpx.Timeout / None / NotGiven | No | Request timeout override |
Outputs
| Name | Type | Description |
|---|---|---|
| create() result | ModerationCreateResponse | Contains moderation results with flagged categories and confidence scores |
Usage Examples
Basic Usage
from openai import OpenAI
client = OpenAI()
# Moderate a single string
response = client.moderations.create(input="Some text to check")
print(response.results[0].flagged) # True/False
print(response.results[0].categories) # Category flags
print(response.results[0].category_scores) # Confidence scores
# Moderate multiple strings
response = client.moderations.create(
input=["First text", "Second text"],
model="omni-moderation-latest",
)
for result in response.results:
print(result.flagged)
# Access raw response headers
raw = client.moderations.with_raw_response.create(input="test")
print(raw.headers["x-request-id"])
moderation = raw.parse()