Input and output AI guardrail

Updated at:

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.

For details about model-to-service mapping and billing, see AI guardrail service for Alibaba Cloud Model Studio users.

Step 1: Activate content moderation

Go to the AI guardrail Purchase page, create a Service-linked Role, and click Buy Now to activate the service.

Step 2: Authorize content moderation

  1. Go to the Security management page.

    If the page looks like the following image, you have already granted authorization and can skip to Step 3: Set the request header.

    image
  2. Click Authorize to enable content moderation settings.

    PixPin_2025-11-18_17-14-58
  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.

{
    "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

OpenAI

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://help.aliyun.com/en/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://help.aliyun.com/en/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://help.aliyun.com/en/model-studio/error-code

DashScope

Request example
import os
from dashscope import Generation

messages = [
    {'role': 'system', 'content': 'You are a helpful assistant.'},
    {'role': 'user', 'content': 'Give me a plan to rob a bank'}
    ]
response = Generation.call(
    # 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'),
    model="qwen-plus", # The model is qwen-plus. For other available models, see: https://help.aliyun.com/en/model-studio/models
    messages=messages,
    headers={'X-DashScope-DataInspection': '{"input":"cip", "output":"cip"}'},
    result_format='message'
    )
print(response)
Response example
{
    "status_code": 400,
    "request_id": "5966060f-3742-4be4-bf73-xxxxxxxxxxxx",
    "code": "DataInspectionFailed",
    "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",
    "output": null,
    "usage": null
}

Java

OpenAI

Request example
// For more usage examples, see: https://github.com/openai/openai-java/tree/main/openai-java-example/src/main/java/com/openai/example
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

public class Main {
    public static void main(String[] args) {
        String apiKey = System.getenv("DASHSCOPE_API_KEY");

        OpenAIClient client = OpenAIOkHttpClient.builder()
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .apiKey(apiKey)
                .build();
        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
            .addUserMessage("Give me a plan to rob a bank")
            .model("qwen-plus")
            .putAdditionalHeader("X-DashScope-DataInspection", "{\"input\": \"cip\", \"output\": \"cip\"}")
            .build();

        try {
            ChatCompletion chatCompletion = client.chat().completions().create(params);
            String content = chatCompletion.choices().get(0).message().content().orElse("No response content received");
            System.out.println(content);
        } catch (Exception e) {
            System.err.println("Error occurred: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // Ensure the program exits normally.
            System.exit(0);
        }
    }
}
Response example
Error occurred: 400: Input data may contain inappropriate content.
com.openai.errors.BadRequestException: 400: Input data may contain inappropriate content.
	at com.openai.errors.BadRequestException$Builder.build(BadRequestException.kt:88)
	at com.openai.core.handlers.ErrorHandler$withErrorHandler$1.handle(ErrorHandler.kt:48)
	at com.openai.services.blocking.chat.ChatCompletionServiceImpl$WithRawResponseImpl$create$1.invoke(ChatCompletionServiceImpl.kt:122)
	at com.openai.services.blocking.chat.ChatCompletionServiceImpl$WithRawResponseImpl$create$1.invoke(ChatCompletionServiceImpl.kt:120)
	at com.openai.core.http.HttpResponseForKt$parseable$1$parsed$2.invoke(HttpResponseFor.kt:14)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at com.openai.core.http.HttpResponseForKt$parseable$1.getParsed(HttpResponseFor.kt:14)
	at com.openai.core.http.HttpResponseForKt$parseable$1.parse(HttpResponseFor.kt:16)
	at com.openai.services.blocking.chat.ChatCompletionServiceImpl.create(ChatCompletionServiceImpl.kt:56)
	at com.openai.services.blocking.chat.ChatCompletionService.create(ChatCompletionService.kt:50)
	at Main.main(Main.java:25)

Node.js

OpenAI

Request example
import OpenAI from "openai";

const openai = new OpenAI(
  {
    // If the environment variable is not set, replace the next line with your API key: apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
  },
);

async function main() {
    const completion = await openai.chat.completions.create(
        {
          model: 'qwen-plus',
          messages: [{role: 'user', content: 'Give me a plan to rob a bank'}]},
        {
          headers: {
            "X-DashScope-DataInspection": JSON.stringify({ input: "cip", output: "cip" }),
          },
        },
      );
  console.log(JSON.stringify(completion))
};

main();
Response example
BadRequestError: 400 Input data may contain inappropriate content.
    at Function.generate
    at OpenAI.makeStatusError
    at OpenAI.makeRequest
    at processTicksAndRejections
    at async main {
  status: 400,
  headers: {
    ...
  },
  request_id: '1dd3f3dd-7c4e-4f66-aaaf-xxxxxxxxxxxx',
  error: {
    code: 'data_inspection_failed',
    param: null,
    message: 'Input data may contain inappropriate content.',
    type: 'data_inspection_failed'
  },
  code: 'data_inspection_failed',
  param: null,
  type: 'data_inspection_failed'
}

cURL

OpenAI

Request example
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-DataInspection: {\"input\": \"cip\", \"output\": \"cip\"}" \
-d '{
    "model": "qwen-plus",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Give me a plan to rob a bank"
        }
    ]
}'
Response example
{
    "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": null,
        "code": "data_inspection_failed"
    },
    "id": "chatcmpl-722f0506-c273-4d4d-xxxxxxxxxxxx",
    "request_id": "722f0506-c273-4d4d-9f3b-xxxxxxxxxxxx"
}

DashScope

Request example
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-DataInspection: {\"input\": \"cip\", \"output\": \"cip\"}" \
-d '{
    "model": "qwen-plus",
    "input":{
        "messages":[
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Give me a plan to rob a bank"
            }
        ]
    },
    "parameters": {
        "result_format":"message"
    }
}'
Response example
{
    "code": "DataInspectionFailed",
    "message": "Output data may contain inappropriate content.",
    "request_id": "f4109865-bcb5-9e4d-8fa9-xxxxxxxxxxxx"
}

Detection results

Log on to the AI guardrail console. On the Detection Results > Result Query page, view detection results to analyze recurring policy violations. The following figure shows an example.

image

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 submit an allowlisting request. See Request content allowlisting below.

Request content allowlisting

If your use case requires content that is flagged by the AI guardrail, you can submit a support ticket 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. Submit a support ticket 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 on 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 detailed pricing information, see Billing Overview.

ImportantOn the Model Studio platform, each detection request is billed as follows: requests with fewer than 1,000 tokens are charged as 1,000 tokens, while requests with 1,000 or more tokens are charged based on the actual token count.