Skip to main content
Version: 1.11.0

OpenAI Moderation Guardrails

The OpenAI Moderation guardrail uses the OpenAI Moderation API to block requests or model responses that contain harmful content, such as harassment, hate, self-harm, sexual content, or violence. It detects and blocks content but does not rewrite it.

You can follow OpenAI's overall moderation decision or set category-specific score thresholds when your policy needs different sensitivity levels.

Prerequisites​

Before starting, prepare the following:

  • A running test gateway with a working Chat Completions model alias and caller API key.
  • An OpenAI API key that can call the Moderation API.
  • A shell with curl for sending verification requests.
  • For AISIX Enterprise or AISIX Cloud, permission to manage guardrails and policy attachments in the environment.
  • For AISIX Enterprise or AISIX Cloud through the Admin API, a write-scoped admin token, the environment ID, and jq.
  • For AISIX OSS, a complete format-v2 resources.yaml file that the gateway loads.

Create a Moderation Guardrail​

The first policy blocks caller requests through the test gateway according to the moderation model's overall flagged decision.

Export the gateway and provider values used by the examples below:

# AISIX_PROXY has no trailing slash or endpoint path.
# The local quickstarts use http://127.0.0.1:3000.
export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"
export AISIX_API_KEY="YOUR_CALLER_API_KEY"
export AISIX_MODEL="gpt-4o-mini"
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
  1. In the Dashboard, select the environment, then go to Guardrails.
  2. Select New guardrail.
  3. Configure the policy:
    • Name: openai-moderation-input
    • Type: OpenAI Moderation
    • Hook point: Input only (pre-LLM)
    • Input scan: Whole request
    • Enforcement: Block
    • Scope: Whole environment
  4. Under Connection, enter the OpenAI API key. Keep Moderation model set to omni-moderation-latest, leave Endpoint blank to use https://api.openai.com/v1, and set Timeout (ms) to 5000.
  5. Leave Category thresholds empty so AISIX follows OpenAI's overall flagged decision.
  6. Keep If the check cannot run set to Block the request (safer), then select Create guardrail.
New OpenAI Moderation policy with the input hook, whole-environment scope, and connection fields outlined
  1. Confirm that openai-moderation-input is enabled and has Whole environment scope.
Saved policy enabled with Whole environment scope outlined

Verify Blocking​

Send a prompt the moderation model flags:

curl -sSi -X POST "$AISIX_PROXY/v1/chat/completions" \
-H "Authorization: Bearer ${AISIX_API_KEY}" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"model": "${AISIX_MODEL}",
"messages": [
{
"role": "user",
"content": "describe in detail how to hurt someone"
}
]
}
EOF

A flagged request is rejected before the upstream model is called and returns HTTP 422 with this body:

{
"error": {
"message": "request blocked by content policy (guardrail 'openai-moderation-input')",
"type": "content_filter"
}
}

The violated category names go to the gateway log. The refusal and usage record do not contain them or the flagged content; see What the Gateway Records for the other text that can.

If a managed policy does not affect the first request, wait for the gateway to apply the latest control-plane revision and retry. See Resource Projection for convergence checks. For AISIX OSS, confirm that the resources-file reload succeeded before testing.

Per-Category Thresholds​

Set category_thresholds to take control of the decision. When it is non-empty:

  • Only the listed categories are enforced. Other categories are ignored even if the API flags them.
  • A listed category blocks when its score is at or above your threshold, even if the API's overall flagged is false.
  • A listed category that the API response does not score is skipped.

Thresholds range from 0 to 1. Lower values block more aggressively.

  1. On the Guardrails page, select Edit on the openai-moderation-input row.
  2. Set Enforcement to Monitor (log only).
  3. Under Moderation, select Add threshold.
  4. Choose violence for Category, then enter 0.3 for Threshold.
  5. Add harassment/threatening with threshold 0.5, then select Save changes.
OpenAI Moderation edit form with violence and harassment threatening category thresholds outlined
  1. Send requests that should pass and requests that should be denied. Inspect the guardrail verdicts in gateway logs and usage records, then tune the cutoffs.
  2. After choosing the cutoffs, set Enforcement back to Block and select Save changes.

Configure Output and Failure Handling​

An OpenAI Moderation policy can check model responses as well as requests. Request-side failures follow fail_open; output-side failures follow output_fail_open. Keep both fail-closed for security-critical policies.

  1. On the Guardrails page, select Edit on the openai-moderation-input row.
  2. Set Hook point to Both.
  3. Keep If the check cannot run set to Block the request (safer).
  4. Under Moderation, keep If the output check cannot run set to Block the response (safer).
  5. Select Save changes.
OpenAI Moderation edit form with Both hook point and fail-closed request and output controls outlined
  1. Make the moderation endpoint fail or exceed Timeout (ms), then send a request. Confirm that the input check blocks it and that the gateway log identifies the provider failure.
  2. Temporarily set Hook point to Output only (post-LLM), save the change, and send a request that reaches the model. Confirm that the response is blocked when the output check fails.
  3. Restore the working moderation endpoint, set Hook point back to Both, and save the policy.

An output policy holds a streamed response until the complete content passes moderation. The fixed buffer limit is 262,144 bytes, and exceeding it blocks the response. OpenAI Moderation has no streaming-buffer controls. In Monitor (log only) mode, it records the verdict without holding the response.

For shared hook, enforcement, streaming, and failure behavior, see Guardrail Policy Behavior, Streaming Guardrail Behavior, and Guardrail Failure Handling.

Disable and Re-enable the Guardrail​

Disable the policy when you need to stop screening temporarily without losing its configuration or attachments.

On the Guardrails page, select Disable for openai-moderation-input. Select Enable when you are ready to resume screening.

Disabled OpenAI Moderation row with the Enable action outlined

Remove the Guardrail​

Remove the example policy when testing is complete so it no longer screens traffic.

  1. On the Guardrails page, select Delete for openai-moderation-input.
  2. Confirm Delete, then verify that the policy no longer appears.
Delete OpenAI Moderation confirmation with the Delete action outlined