# Alibaba Cloud Content Moderation

Alibaba Cloud Content Moderation grades content with a risk level through the `TextModerationPlus` API, and AISIX blocks requests whose returned level reaches your configured threshold. Requests can be checked before they reach the upstream model, and responses can be checked before they reach callers.

This is a different Alibaba Cloud product from [Alibaba Cloud AI Guardrails](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/alibaba-cloud-ai-guardrails.md) (`aliyun_ai_guardrail`, the `MultiModalGuard` API). Use AI Guardrails when you manage policy in its console, need composite checks such as prompt-attack detection and sensitive-data masking, or want the calls visible in the AI Guardrails console's records.

In this guide, you will create an Alibaba Cloud Content Moderation resource, send one allowed request, and send one blocked request that AISIX rejects before it reaches the upstream model.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* Review [Guardrail Policy Behavior](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md) for hook points and enforcement, and [Guardrail Failure Handling](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/failure-handling.md) for fail-open and fail-closed behavior.

* One of these configuration paths:

  <!-- -->

  * AISIX Enterprise or AISIX Cloud with an environment, an attached gateway, and a write-scoped admin token. For AISIX Enterprise, follow the [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md). To request AISIX Cloud access, [contact API7](https://api7.ai/contact).
  * An open-source AISIX gateway that loads a declarative [`resources.yaml`](https://docs.api7.ai/ai-gateway/reference/resources-file.md) file.

* A working model alias and caller API key that can send Chat Completions requests.

* [Alibaba Cloud Content Moderation](https://www.alibabacloud.com/help/en/content-moderation) enhanced-edition APIs activated in the region you want to use.

* An Alibaba Cloud AccessKey ID and AccessKey secret allowed to call the `TextModerationPlus` API action.

* `curl`. The control-plane path also uses `jq`.

Export the gateway and provider values used by both paths:

```
# 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 ALIBABA_CLOUD_ACCESS_KEY_ID="YOUR_ALIBABA_CLOUD_ACCESS_KEY_ID"

export ALIBABA_CLOUD_ACCESS_KEY_SECRET="YOUR_ALIBABA_CLOUD_ACCESS_KEY_SECRET"
```

## Create an Alibaba Cloud Content Moderation Guardrail[​](#create-an-alibaba-cloud-content-moderation-guardrail "Direct link to Create an Alibaba Cloud Content Moderation Guardrail")

Choose one configuration path to create the guardrail, then use the shared verification procedure.

### AISIX Enterprise or AISIX Cloud[​](#aisix-enterprise-or-aisix-cloud "Direct link to AISIX Enterprise or AISIX Cloud")

Export the control-plane connection details:

```
# AISIX_CP includes /api and has no trailing slash.

# The local AISIX Enterprise quickstart uses http://localhost:8080/api.

export AISIX_CP="YOUR_AISIX_CONTROL_PLANE_API_URL"

export AISIX_TOKEN="YOUR_ADMIN_TOKEN"

export ENV_ID="YOUR_ENVIRONMENT_ID"
```

Create an Alibaba Cloud Content Moderation resource in the environment and capture its ID:

```
export GUARDRAIL_ID=$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/guardrails" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "aliyun-review",

    "enabled": false,

    "hook_point": "both",

    "fail_open": false,

    "enforcement_mode": "block",

    "kind": "aliyun_text_moderation",

    "config": {

      "region": "cn-shanghai",

      "access_key_id": "'"${ALIBABA_CLOUD_ACCESS_KEY_ID}"'",

      "access_key_secret": "'"${ALIBABA_CLOUD_ACCESS_KEY_SECRET}"'",

      "output_fail_open": false,

      "risk_level_threshold": "medium",

      "timeout_ms": 3000

    }

  }' | jq -r '.guardrail.id')
```

❶ `both` checks caller requests and model responses. See [Guardrail Hook Point](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md#guardrail-hook-point).

❷ `fail_open: false` blocks the request if Alibaba Cloud Content Moderation fails or times out. This is the default.

❸ `enforcement_mode: block` rejects matching content. This is the default. See [Enforcement Modes](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md#enforcement-modes).

❹ `output_fail_open: false` blocks unscanned model output during an Alibaba Cloud outage. This is the default.

❺ `risk_level_threshold` blocks returned risk levels at or above the configured threshold.

❻ `timeout_ms` bounds each call to Alibaba Cloud end to end, from sending the request through reading the full response body, but not the whole check. AISIX sends at most 2,000 characters to `TextModerationPlus` in one call and never truncates content to fit: longer content is split into chunks, and every chunk is checked in its own call. The calls run one after another, the number of chunks is not capped, and there is no overall deadline, so the total time is roughly the number of chunks multiplied by the per-call time. Alibaba Cloud bills each call.

AISIX derives the moderation endpoint from `region` (`green-cip.<region>.aliyuncs.com`). Set `endpoint` in `config` to a full URL only when you need to override that host.

Attach the guardrail to the environment so it applies to all traffic:

```
curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID/attachments" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "scope_type": "env"

  }'
```

The `env` scope omits `scope_id` and covers every request in the environment. To narrow enforcement, set `scope_type` to `model`, `api_key`, or `team` and pass the resource ID as `scope_id`.

Enable the guardrail after its attachment exists:

```
curl -sS -X PATCH "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{"enabled": true}'
```

The enabled configuration projects to attached gateways automatically.

### AISIX OSS[​](#aisix-oss "Direct link to AISIX OSS")

Add the guardrail to the resources file that already defines the example model and caller API key. The example reads credentials from `ALIBABA_CLOUD_ACCESS_KEY_ID` and `ALIBABA_CLOUD_ACCESS_KEY_SECRET`. A running quickstart container does not inherit these variables from later host exports.

resources.yaml (Alibaba moderation guardrail)

```
guardrails:

  - id: 42536523-2b09-48b3-bde0-912792ef89b2

    name: aliyun-review

    enabled: true

    hook_point: both

    fail_open: false

    enforcement_mode: block

    kind: aliyun_text_moderation

    region: cn-shanghai

    access_key_id: ${ALIBABA_CLOUD_ACCESS_KEY_ID}

    access_key_secret: ${ALIBABA_CLOUD_ACCESS_KEY_SECRET}

    output_fail_open: false

    risk_level_threshold: medium

    timeout_ms: 3000



guardrail_attachments:

  - id: d49c6b22-800f-4825-b684-1967ca805916

    guardrail_id: 42536523-2b09-48b3-bde0-912792ef89b2

    scope_type: env

    priority: 100
```

The provider fields sit directly on the guardrail entry rather than under `config`. AISIX derives the endpoint from `region`; set `endpoint` only when the gateway should use another full URL.

A guardrail applies only where an attachment puts it: add a `guardrail_attachments` entry naming it, or it loads and inspects no traffic. Apply the change by validating the complete file before reloading or recreating the gateway. If you are extending the open-source quickstart, follow [Reload a Resources File](https://docs.api7.ai/ai-gateway/deployment/configuration-propagation.md#reload-a-resources-file) to recreate the container only after validation succeeds and pass the additional credential variables.

## Verify the Guardrail[​](#verify-the-guardrail "Direct link to Verify the Guardrail")

Control-plane projection is asynchronous. If the first request does not reflect the guardrail, wait for the gateway to apply the latest revision and retry. See [Resource Projection](https://docs.api7.ai/ai-gateway/cloud/resource-projection.md) for convergence checks.

Send a benign request through AISIX:

```
curl -sSi -X POST "$AISIX_PROXY/v1/chat/completions" \

  -H "Authorization: Bearer ${AISIX_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "'"${AISIX_MODEL}"'",

    "messages": [

      {

        "role": "user",

        "content": "What is the capital of France?"

      }

    ]

  }'
```

A successful response starts with `HTTP/1.1 200 OK` and returns an OpenAI-compatible chat-completions body.

For a repeatable check, use content that your Alibaba Cloud Content Moderation configuration classifies at or above the configured AISIX threshold.

Then send a request containing that content:

```
curl -sSi -X POST "$AISIX_PROXY/v1/chat/completions" \

  -H "Authorization: Bearer ${AISIX_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "'"${AISIX_MODEL}"'",

    "messages": [

      {

        "role": "user",

        "content": "YOUR_POLICY_VIOLATING_TEXT"

      }

    ]

  }'
```

A blocked response starts with `HTTP/1.1 422 Unprocessable Entity` and includes an OpenAI-compatible error:

```
{

  "error": {

    "message": "request blocked by content policy (guardrail 'aliyun-review')",

    "type": "content_filter"

  }

}
```

AISIX blocks the request before dispatching to the upstream model when Alibaba Cloud Content Moderation returns a blocking risk level.

## Tune Risk Threshold[​](#tune-risk-threshold "Direct link to Tune Risk Threshold")

Alibaba Cloud returns a risk level for each moderation decision. Alibaba Cloud determines the level from its moderation result and [configured score thresholds](https://help.aliyun.com/en/document_detail/2684669.html). AISIX does not recalculate it.

AISIX can block the `low`, `medium`, or `high` levels:

| Threshold | Effect                                                |
| --------- | ----------------------------------------------------- |
| `high`    | Blocks only high-risk verdicts. This is the default.  |
| `medium`  | Blocks medium-risk and high-risk verdicts.            |
| `low`     | Blocks low-risk, medium-risk, and high-risk verdicts. |

Use a stricter threshold when the application should reject more uncertain content, or a less strict threshold when you want Alibaba Cloud to block only the strongest matches.

## Trace a Blocked Request to Alibaba Cloud[​](#trace-a-blocked-request-to-alibaba-cloud "Direct link to Trace a Blocked Request to Alibaba Cloud")

A blocked response tells the caller only that content policy rejected the request. To find out why a specific request was blocked, correlate it with the moderation record in the Alibaba Cloud console.

Every AISIX proxy response carries an `x-aisix-request-id` header, including the `422` above:

```
HTTP/1.1 422 Unprocessable Entity

x-aisix-request-id: 12179fb5-5c3c-4a2a-a411-d73746379cc6
```

Ask the caller for that ID, then search the gateway log for it:

```
grep 12179fb5-5c3c-4a2a-a411-d73746379cc6 /path/to/aisix.log
```

The moderation decision is logged at `info` level, below the gateway's default `warn`, so it is written only with `log_level: "info"`. When you need `aliyun_request_id`, raise the level first: the level is read at startup, and a block handled at `warn` does not write this line. See [Collect Access Logs](https://docs.api7.ai/ai-gateway/observability/access-logs-and-request-correlation.md#collect-access-logs). Both IDs appear on the same log line, so one search finds them together. The example below is wrapped to fit the page; in your log it is a single line:

```
INFO request{request_id=12179fb5-5c3c-4a2a-a411-d73746379cc6}: aisix_guardrails::aliyun:

aliyun text moderation blocked content guardrail_name=aliyun-review service="llm_query_moderation"

aliyun_request_id=019F6EF3-6F9A-5A25-9EED-256CB0E26448 aliyun_code=200

aliyun_risk_level=high aliyun_labels=inappropriate_oral,violent_incidents
```

Two different IDs appear here, and they are not interchangeable:

| Field               | Meaning                                                                                                                                                            |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `request_id`        | The gateway request ID, equal to the caller's `x-aisix-request-id`.                                                                                                |
| `aliyun_request_id` | Alibaba Cloud's own `RequestId` for the moderation call. Use it to look up the record in the Alibaba Cloud console or to open a ticket with Alibaba Cloud support. |
| `aliyun_risk_level` | The risk level Alibaba Cloud returned, which is compared against your threshold.                                                                                   |
| `aliyun_labels`     | The categories Alibaba Cloud matched. One request commonly matches several.                                                                                        |

Requests that pass moderation log the same fields at `debug` level, so raise the gateway `log_level` to `debug` when you need the Alibaba Cloud `RequestId` for a request that was not blocked.

When Alibaba Cloud cannot be reached at all, such as a timeout or a connection failure, `aliyun_request_id` is logged empty because Alibaba Cloud never issued one. The accompanying `failure` field names the failure class. This example is also wrapped:

```
WARN request{request_id=...}: aisix_guardrails::aliyun: aliyun text moderation call failed

guardrail_name=aliyun-review aliyun_request_id= failure=Timeout fail_open=false elapsed_ms=3001
```

The fields after `failure` give the underlying cause. `http_status` is the status of the response and appears only when a response arrived, for example when the body stalled past `timeout_ms` after the headers. `error` is the underlying error with its full cause chain, such as a DNS failure or a refused connection, and `error_kind` classifies it as `connect`, `timeout`, `request`, `body`, `decode`, or `other`. Neither appears when the gateway's own `timeout_ms` timer ended the call, as in the example. `elapsed_ms` is the time from sending the request to the failure.

Alibaba Cloud reports its own quota throttling and timeouts inside an HTTP `200` response, as body `Code` `588` (`EXCEED_QUOTA`) and `Code` `581` (`TIMEOUT`). AISIX classifies them as `aliyun_throttled` and `aliyun_timeout`, the same failure tags as an HTTP `429` and as `timeout_ms` elapsing. These calls reached Alibaba Cloud, so `aliyun_request_id` is filled in, and a preceding warning names the code, for example `Code 581 TIMEOUT`. See [Failure Tags Raised by a Guardrail Kind](https://docs.api7.ai/ai-gateway/reference/headers-and-error-codes.md#failure-tags-raised-by-a-guardrail-kind).

### When a 422 Is a Configuration Error[​](#when-a-422-is-a-configuration-error "Direct link to When a 422 Is a Configuration Error")

With `fail_open: false`, a guardrail that cannot get a decision from Alibaba Cloud blocks the request. A wrong credential therefore reaches the caller as the same `422 content_filter` as a genuine policy hit. The log tells them apart: a configuration error logs at `error` level, with `aliyun_code` naming the cause.

| `aliyun_code`                 | Cause                                                 |
| ----------------------------- | ----------------------------------------------------- |
| `SignatureDoesNotMatch`       | The `access_key_secret` is wrong.                     |
| `InvalidAccessKeyId.NotFound` | The `access_key_id` is wrong, or the key is disabled. |
| `InvalidAction.NotFound`      | The `region` or `endpoint` does not serve this API.   |

Alibaba Cloud returns other codes for other failures; [its error reference](https://api.aliyun.com/troubleshoot) resolves any code by name.

AISIX logs the code but never the body Alibaba Cloud returns with it. On a signature error, that body quotes the entire signed request back. The quoted request carries the caller's prompt and your AccessKey ID, so echoing it would put both in your logs.

Two things are deliberately kept out of this path:

* Alibaba Cloud's `RequestId` is never returned to the caller. Callers receive only `x-aisix-request-id`, and you resolve the rest from the log.
* The guardrail's own log lines do not contain the text that Alibaba Cloud matched. Alibaba Cloud returns the offending words in its `RiskWords` and `RiskPositions` fields, but AISIX records only the category labels from them. See [What the Gateway Records](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/overview.md#what-the-gateway-records) for the other text that can carry it.

## Next Steps[​](#next-steps "Direct link to Next Steps")

You have now enforced Alibaba Cloud Content Moderation through AISIX. Use these guides to tune behavior or compare related guardrails:

* [Guardrail Policy Behavior](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md): adjust hook points, scope, and enforcement.
* [Alibaba Cloud AI Guardrails](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/alibaba-cloud-ai-guardrails.md): the MultiModalGuard guardrail whose policy lives in the AI Guardrails console, with sensitive-data masking.
* [Choosing a Guardrail Provider](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/overview.md): compare Alibaba Cloud Content Moderation with other built-in and remote options.
