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.
NoteThe 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 click Buy Now to activate the service.
Step 2: Authorize the AI guardrail
-
Go to the Security management page. The
Security managemententry appears only under the Management group in the China (Beijing) region.If the page that opens from the link above is as described below, you have already granted authorization and can skip to Step 3: Set the request header.
That is, the page displays the Global Settings title, the status on the right is Enabled (cannot be canceled), and the page body shows the full text of the Self-built security mechanism commitment letter.
-
Click Authorize to enable the AI guardrail.
-
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. If you prefer not to modify your calling code, use the workspace-level switches instead. 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
OpenAI
Request exampleimport 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/getting-started/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 exampleError: Error code: 400 -
{
"error":
{
"message": "Input data may contain inappropriate content. For details, see: https://help.aliyun.com/en/model-studio/error-code#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 exampleimport os
import httpx
import dashscope
from dashscope.v2 import DashScopeError
# Enable the AI guardrail service via the request headers of a custom HTTP client
client = dashscope.Client(
# 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'),
http_client=httpx.Client(
headers={'X-DashScope-DataInspection': '{"input":"cip", "output":"cip"}'},
),
)
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Give me a plan to rob a bank'}
]
try:
response = client.chat.completions.create(
model="qwen-plus", # The model is qwen-plus. For other available models, see: https://help.aliyun.com/en/model-studio/getting-started/models
messages=messages,
)
print(response)
except DashScopeError as e:
print(f"Error code: {e.code}")
print(f"Error message: {e.message}")
print("For more information, see the documentation at: https://help.aliyun.com/en/model-studio/developer-reference/error-code")
Response exampleError code: DataInspectionFailed
Error message: Input data may contain inappropriate content. For details, see: https://help.aliyun.com/en/model-studio/error-code#inappropriate-content
For more information, see the documentation at: https://help.aliyun.com/en/model-studio/developer-reference/error-code
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 exampleError 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 exampleimport 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 exampleBadRequestError: 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 examplecurl -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#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 examplecurl -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": "Input data may contain inappropriate content. For details, see: https://help.aliyun.com/en/model-studio/error-code#inappropriate-content",
"request_id": "f4109865-bcb5-9e4d-8fa9-xxxxxxxxxxxx"
}
View moderation results
Log on to the AI guardrail console. On the Detection result query tab page, view the moderation results to further analyze the violation types that occur frequently in text content. The following is an example of moderation results.
The result query page contains a filter bar (which supports searching by condition, text content, and time range) and a results table. The table columns include Text content, Service, Risk level, Returned label (description), Feedback result, Request time, and Operation. In the example, the two records correspond to the bailian_query_check (request check) and bailian_response_check (response check) services respectively, both flagged as High risk, with the returned label contraband_act (suspected other criminal offense):100. For each row, you can click Details or Feedback to take action.
Workspace-level switches
On the Workspace Management page of the Model Studio console, you can enable the guardrail for a business space when you create or edit it, without modifying your calling code. The AI Security Guardrail column in the business space list shows the guardrail status of each space:
- Find the target business space and click Edit.
- In the AI Security Guardrail section under Advanced Configuration, select Input Detection or Output Detection as needed. The two options are independent of each other and are both deselected by default.
- Click OK to save. The switches take effect for model calls made with all API keys in the business space. The gateway automatically injects the
X-DashScope-DataInspectionrequest header in the inference chain to trigger detection.
Note
- If the page indicates that you must first go to Security Management to authorize the service, complete Step 1: Activate the AI guardrail service and Step 2: Authorize the AI guardrail service first. To manage guardrail policies, go to the AI guardrail console.
- After the switches are turned on, the guardrail service is charged based on actual usage. For billing rules, see Billing.
- The workspace-level switches and the request header parameter work in parallel. If the
X-DashScope-DataInspectionheader is already set in a request, the header setting takes precedence.
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 |
| 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 |
| 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) |
| 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) |
| 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 |
| Image models, independent of model series | Content compliance risks in input images |
Output image guardrail |
| 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_adultandsexual_prompts) - Politics (such as
political_figureandpolitical_prompts) - Violence and extremism (such as
violent_extremistandviolent_prompts) - Prohibited items (such as
contraband_drugandcontraband_act) - Inappropriate content (such as
inappropriate_discriminationandinappropriate_profanity) - Traffic and advertising (such as
pt_to_sitesandpt_to_contact) - Religion (such as
religion_bandreligion_c) - Custom (the
customizedlabel, 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 security interception
When you use the knowledge base retrieval Q&A feature, document content uploaded to the knowledge base is injected into the model input as context after retrieval. If the knowledge base documents contain non-compliant or sensitive content, that content also triggers the AI guardrail, causing the request to be intercepted and a DataInspectionFailed error to be returned.
If you encounter security policy interception in knowledge base retrieval scenarios, troubleshoot by following these steps:
- Check the documents uploaded to your knowledge base to determine whether they contain politically sensitive content, pornographic or violent content, illegal or harmful information, private personal data, or expressions that may induce violations.
- Simplify or rewrite the prompts of your application to reduce expressions that may trigger security interception.
- If the issue persists, collect the complete error details (HTTP status code, error code, and error message) and contact your account manager to request allowlisting. For the detailed process, see Content allowlisting process below.
Content allowlisting process
If your business scenario requires exempting specific content from security interception, you can contact your account manager to request content allowlisting. After the security team approves the review, the specified content is added to the exemption list.
Before requesting allowlisting, prepare the following information:
- HTTP status code (for example,
400) - Error code (for example,
DataInspectionFailedordata_inspection_failed) - Complete error message (for example,
Input data may contain inappropriate content.)
Application steps:
- Collect the complete error details above and a description of the scenario where the issue can be reproduced (including the knowledge base ID, the model name to call, and the operation that triggered the interception).
- Contact your account manager with the error details, your use case description, and the specific content you need allowlisted.
- Wait for the security team to complete the review. Allowlisting takes effect after the review is approved.
Billing
After you activate the SLR authorization for the AI guardrail product on the Model Studio console and enable the product policy through the Model Studio configuration, the system bills based on the actual number of calls. Billing is pay-as-you-go by token count, and daily fees are settled based on the actual usage of the day; no fees are incurred when the service is not called. For detailed billing rules, see Billing Overview.
ImportantWhen a single query/response detection is performed on the Model Studio platform, if the text contains fewer than 1,000 tokens, it is billed as 1,000 tokens; if it reaches or exceeds 1,000 tokens, it is billed based on the actual token count.