Skip to main content

Input and output AI guardrail

Inputs to and outputs from large models can contain sensitive or high-risk content, such as pornographic material, political references, or advertisements. While a model's built-in safety mechanisms typically provide effective protection, Alibaba Cloud Model Studio lets you integrate an AI guardrail service. This service adds a security layer that scans both inputs and outputs for non-compliant content, ensuring safety and compliance.

Configure the AI guardrail service

When you call a large model in Alibaba Cloud Model Studio, the system automatically matches it with the appropriate AI guardrail service.
The AI guardrail service is available only in the China (Beijing) region. Other regions are not supported.
Text and image models are supported. For model-to-service mapping, see Service and model mapping. For billing information, see Billing.

Step 1: Activate the AI guardrail service

Go to the AI guardrail Purchase page, create a Service-linked Role, and complete the activation.

Step 2: Authorize the AI guardrail service

  1. Go to the Security management page. The Security management entry appears only under the Management group in the China (Beijing) region. If the page displays the Global Settings title, an Enabled (cannot be canceled) status, and the full text of the Self-built Security Mechanism Commitment Letter, you have already granted authorization and can skip to Step 3: Set the request header.
  2. Click Authorize to enable the AI guardrail.
  3. Confirm the authorization.

Step 3: Set the request header

When calling Alibaba Cloud Model Studio, include the following in the request header to enable the AI guardrail service. Note: the parameter value is a JSON string, not a nested JSON object.
{
    "X-DashScope-DataInspection": "{\"input\":\"cip\",\"output\":\"cip\"}"
}
Example
Set the DASHSCOPE_API_KEY environment variable before making a call. For instructions, see Obtain an API key.
  • Python
  • Java
  • Node.js
  • cURL
  • OpenAI
  • DashScope
Request example
import os
from openai import OpenAI

try:
    client = OpenAI(
        # If the environment variable is not set, replace the next line with your API key: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    )

    completion = client.chat.completions.create(
        model="qwen-plus",  # For a list of models, see: https://www.alibabacloud.com/help/model-studio/models
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': 'Give me a plan to rob a bank'}
            ],
        extra_headers={
        'X-DashScope-DataInspection': '{"input":"cip","output":"cip"}'
        }
    )
    print(completion.choices[0].message.content)
except Exception as e:
    print(f"Error: {e}")
    print("For more information, see the documentation at: https://www.alibabacloud.com/help/model-studio/error-code")
Response example
Error: Error code: 400 -
{
    "error":
    {
        "message": "Input data may contain inappropriate content. For details, see: https://help.aliyun.com/en/model-studio/error-code#input-or-output-data-may-contain-inappropriate-content-input-data-may-contain-inappropriate-content-output-data-may-contain-inappropriate-content",
        "type": "data_inspection_failed",
        "param": "None",
        "code": "data_inspection_failed"
    },
    "id": "chatcmpl-db364068-8222-48c5-a1ca-xxxxxxxxxxxx",
    "request_id": "db364068-8222-48c5-a1ca-xxxxxxxxxxxx"
}
For more information, see the documentation at: https://www.alibabacloud.com/help/model-studio/error-code

Detection results

Log on to the AI guardrail console. On the Detection Results > Result Query page, view detection results to analyze recurring policy violations.

Service and model mapping

After the cip identifier is passed in the request header, the system automatically matches the model to one of the following guardrail services:

Service name

service identifier

Applicable models

Detection scope

Input content guardrail

qwen_query_check, bl_query_guard

Mainstream models (excluding Qwen3-Max, Qwen-Max, and Qwen-VL-Max)

Red-line violations in inputs (pornographic, political, or violent content), harmful inducement, and certain sensitive topics

Output content guardrail

qwen_response_check, bl_response_guard

Mainstream models (excluding Qwen3-Max, Qwen-Max, and Qwen-VL-Max)

Red-line violations in outputs, harmful inducement, abusive language, bias, and undesirable values

Input content guardrail (Pro)

qwen_query_check_pro, bl_query_guard_pro

Only Qwen3-Max, Qwen-Max, and Qwen-VL-Max

Same detection scope as the input guardrail, tuned for the Qwen3-Max series

Output content guardrail (Pro)

qwen_response_check_pro, bl_response_guard_pro

Only Qwen3-Max, Qwen-Max, and Qwen-VL-Max

Same detection scope as the output guardrail, tuned for the Qwen3-Max series

Input image guardrail

bl_img_query_guard

Image models, independent of model series

Content compliance risks in input images

Output image guardrail

bl_img_response_guard

Image models, independent of model series

Content compliance risks in output images

Risk label management

Risk labels in detection results carry a confidence score from 0 to 100, where a higher score indicates higher confidence. The main categories are:
  • Pornographic content (such as pornographic_adult and sexual_prompts)
  • Politics (such as political_figure and political_prompts)
  • Violence and extremism (such as violent_extremist and violent_prompts)
  • Prohibited items (such as contraband_drug and contraband_act)
  • Inappropriate content (such as inappropriate_discrimination and inappropriate_profanity)
  • Traffic and advertising (such as pt_to_sites and pt_to_contact)
  • Religion (such as religion_b and religion_c)
  • Custom (the customized label, returned when a custom vocabulary hits)
For the full label list, per-label detection scopes, and custom vocabulary configuration, go to the Model Protection page of the AI guardrail console: switch to the destination region in the top menu bar, find the target service, and click Manage. Then, on the Configuration Management page of a protection dimension, edit the detection status of fine-grained scenarios. All risk labels except those under red-line control can be toggled, and configuration changes take effect in about 2 to 5 minutes.

Use guardrails through AI Gateway

If you call Model APIs through AI Gateway, you can configure guardrail inspection and interception policies by consumer in the AI Gateway console. Multiple protection dimensions are supported, such as content compliance, prompt attacks, sensitive information, model hallucination, and malicious URLs, each with low, medium, or high protection levels. For detailed instructions, see Enable the AI guardrails service on AI Gateway.

Knowledge base retrieval and the AI guardrail

When you use the knowledge base feature in Model Studio, documents uploaded to your knowledge base are retrieved and injected as context into model inputs. If the retrieved documents contain sensitive or non-compliant content, that content is subject to the same AI guardrail inspection as direct user inputs, and the request is intercepted with a DataInspectionFailed error. If your knowledge base retrieval requests are blocked by the AI guardrail, follow these steps to troubleshoot:
  1. Review the documents uploaded to your knowledge base for content that may violate content policies, including politically sensitive content, adult content, violent content, illegal information, private data, or content that could induce policy violations.
  2. Simplify or rewrite your prompts to reduce the likelihood that retrieved context triggers the guardrail.
  3. If the issue persists, collect the full error details (HTTP status code, error code, and error message) and contact your account manager to request allowlisting. See Request content allowlisting below.

Request content allowlisting

If your use case requires content that is flagged by the AI guardrail, you can contact your account manager to request allowlisting for specific content. Once the security team approves your request, the specified content is added to the exemption list. Before submitting your request, collect the following information from the failed API response:
  • HTTP status code (for example, 400)
  • Error code (DataInspectionFailed for DashScope or data_inspection_failed for OpenAI-compatible mode)
  • Full error message (for example, Input data may contain inappropriate content.)
To submit an allowlisting request:
  1. Collect the complete error details and a description of the issue, including the knowledge base ID, the model name, and the operation that triggered the interception.
  2. Contact your account manager with the error details, your use case description, and the specific content you need allowlisted.
  3. Wait for the security team to complete the review. Allowlisting takes effect after the review is approved.

Billing

After you enable the AI guardrail service in the Model Studio console and grant the required service-linked role (SLR) permissions, billing starts. The service is pay-as-you-go, with charges based on the number of processed tokens. Fees are settled daily, and you will not be charged if the service is not used. For pricing information, see the AI Guardrails product page.