Skip to main content
Version: Dev

Configure PII Guardrails

The built-in PII guardrail detects and handles sensitive data without sending content to an external moderation service. It can mask matches before content continues to the upstream model or caller, or block the request or response. Guardrail logs, usage fields, and refusals record detector names and counts rather than matched values; What the Gateway Records describes the surrounding request data that other features can retain.

This guide creates an input guardrail for email addresses and API keys, verifies masking and blocking, adds an organization-specific pattern, and configures replacement and failure behavior.

Prerequisites​

Before starting, prepare the following:

  • Review Guardrail Policy Behavior for hook points and enforcement modes.
  • A working model alias, caller API key, and running gateway that can send Chat Completions requests.
  • curl.
  • If you use the Dashboard to configure AISIX Enterprise or AISIX Cloud, sign in with permission to manage guardrails and attachments in the target environment.
  • If you use the Admin API to configure AISIX Enterprise or AISIX Cloud, prepare a write-scoped token, the environment ID, and jq.
  • If you use AISIX OSS, prepare a complete format-v2 resources.yaml file that the gateway loads.

Create a PII Guardrail​

Before creating a policy, decide which sensitive-data formats to detect and whether a match should continue in masked form or stop traffic:

  • mask replaces the match with a redaction token and lets traffic continue.
  • block rejects the request or response with 422 Unprocessable Entity.

The example below masks email addresses and API keys in caller requests before AISIX sends them to the upstream provider. See PII Detector Reference for the complete detector catalog, bank-card formats, masking limitations, and replacement rules.

Export the gateway values used by the verification requests:

# 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"
  1. In the Dashboard, select the environment, then go to Guardrails.
  2. Select New guardrail.
  3. Configure the guardrail:
    • Name: pii-redaction-policy
    • Type: PII redaction
    • Hook point: Input only (pre-LLM)
    • Enforcement: Block
    • Default action: Mask
    • Built-in detectors: select only Email address and API keys & tokens
    • Scope: Whole environment
    • On stream buffer overflow: Fail closed (drop the response — recommended)
    • Max buffer bytes: leave blank to use 262144
New PII guardrail with Mask, email and API-key detectors, and Create guardrail outlined
  1. Select Create guardrail.
  2. Confirm that pii-redaction-policy is enabled on the Guardrails page before verifying traffic. Check that its scope is Whole environment.
pii-redaction-policy saved with enabled status and Whole environment scope outlined

Verify Masking​

Verify that AISIX rewrites sensitive data before it reaches the upstream model. Send a request whose prompt contains an email address.

For AISIX Enterprise and AISIX Cloud, projection is asynchronous. If a managed gateway does not reflect the new rule, wait for the latest revision and retry. See Resource Projection for convergence checks.

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": "email me at alice@example.com about the order"
}
]
}
EOF

The request succeeds with HTTP/1.1 200 OK. AISIX rewrites the prompt before calling the upstream model, so the provider receives the masked prompt:

email me at [EMAIL_REDACTED] about the order

The original value does not reach the upstream provider. Usage records carry per-detector match counts, including detector names but not matched text. A validation error raised before the guardrail runs can still quote the value; see What the Gateway Records.

Block Sensitive Data​

Masking lets traffic continue, but some sensitive data should stop the request entirely. In this section, add a second guardrail that blocks a valid mainland China resident ID.

  1. On the Guardrails page, select New guardrail.
  2. Enter block-id-card for Name and select PII redaction for Type.
  3. Select Input only (pre-LLM) for Hook point, Block for Enforcement, and Block for Default action.
  4. Under Built-in detectors, select only Mainland China resident ID.
  5. Select Whole environment for Scope.
New PII guardrail with Block, the resident ID detector, and Create guardrail outlined
  1. Select Create guardrail.
  2. Confirm that block-id-card is enabled and shows Whole environment before verifying traffic.
block-id-card saved with enabled status and Whole environment scope outlined

Verify Blocking​

A request whose content includes a valid ID number is rejected:

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": "my id is 11010519491231002X"
}
]
}
EOF

A blocked response starts with HTTP/1.1 422 Unprocessable Entity and includes this body:

{
"error": {
"message": "request blocked by content policy (guardrail 'block-id-card')",
"type": "content_filter"
}
}

The message is deliberately generic and does not echo the matched value.

Add a Custom Pattern​

Built-in detectors cover common data formats, but organizations often have identifiers unique to their own systems. In this section, extend pii-redaction-policy to mask employee IDs such as EMP-123456.

The following examples add an employee ID pattern to pii-redaction-policy while preserving its existing email and API-key detectors.

  1. On the Guardrails page, select Edit on the pii-redaction-policy row.
  2. Under Custom patterns, select Add pattern.
  3. Enter employee_id for the pattern name and EMP-[0-9]{6} for the regular expression.
  4. Keep the pattern action set to Default action and leave the replacement blank, then select Save changes.

AISIX masks a matching value such as EMP-123456 as [EMPLOYEE_ID_REDACTED] before forwarding the request.

Employee ID custom pattern with its name and regular expression outlined

Choose What a Masked Span Becomes​

The default redaction token identifies the detector or custom pattern that matched. When an upstream system needs a different placeholder, configure one explicitly. In this section, change the employee ID replacement to six asterisks.

  1. On Guardrails, select Edit on the pii-redaction-policy row.
  2. Under Custom patterns, enter ****** in the replacement field for employee_id.
  3. Select Save changes. Clearing the replacement field restores the default token.
Employee ID custom pattern with a literal six-asterisk replacement outlined

To delete a matched span rather than replace it with a token, use the Admin API or resources file. Clearing the Dashboard field cannot create an explicit empty-string replacement.

Replacement text is literal: $1 and other group references are not expanded. When a regex contains capture groups, AISIX replaces only group 1 and keeps the surrounding match. Without a capture group, it replaces the whole match.

See Custom Pattern Replacements for examples, structured tool-argument behavior, and common mistakes. replacement applies only to mask; a blocking pattern rejects traffic instead of rewriting it.

Handle Content AISIX Cannot Scan​

Choose whether AISIX should reject or pass a request when its body cannot be read. Because PII detection runs inside the gateway, this situation comes from unreadable content, such as invalid UTF-8, rather than an unavailable moderation service.

By default, AISIX rejects the request with 422 and records the failure tag unscannable_body. Enable fail_open when availability matters more than enforcement. AISIX then forwards the request and records the bypass in its usage record.

  1. On the Guardrails page, select Edit on the pii-redaction-policy row.
  2. Enable fail_open.
  3. Select Save changes.
PII guardrail fail_open control enabled with Save changes outlined

fail_open has three limits:

  • PII guardrails have no output_fail_open setting, so one value governs both hooks.
  • on_buffer_exceeded separately controls what happens when a buffered response exceeds max_buffer_bytes.
  • fail_open does not override output buffering. An output-hook guardrail still refuses a stream that leaves nothing scannable after buffering.

Remove the Guardrails​

Remove both example guardrails after verification when they should no longer inspect environment traffic.

  1. On the Guardrails page, select Delete on the block-id-card row and confirm the deletion.
Delete confirmation naming block-id-card with Delete outlined
  1. Select Delete on the pii-redaction-policy row and confirm the deletion.
Delete confirmation naming pii-redaction-policy with Delete outlined
  1. Confirm that neither guardrail appears on the Guardrails page.

Next Steps​

You have configured the built-in PII guardrail and verified mask and block behavior. Use these guides to refine or expand the policy: