- Categories:
String & binary functions (AI Functions)
AI_ REDACT¶
Personally identifiable information (PII) includes names, addresses, phone numbers, email addresses, tax identification numbers, and other data that can be used (alone or with other information) to identify an individual. Most organizations have regulatory and compliance requirements around handling PII data. AI_REDACT is a fully-managed Cortex AI Function that uses a large language model (LLM) to help you detect, locate, and redact PII from unstructured text data.
AI_REDACT can help you prepare text for call center coaching, sentiment analysis, insurance and medical analysis, and machine learning (ML) model training, among other use cases.
Tip
Use AI_PARSE_DOCUMENT or AI_TRANSCRIBE to convert document or speech data into text before applying AI_REDACT.
AI_REDACT has two modes of operation: redact (default) and detect.
- Use AI_REDACT in
redactmode to replace PII in the input text with placeholder values. - Use AI_REDACT in
detectmode to identify PII locations, then programmatically choose which PII to redact.
Important
AI_REDACT performs detection and redaction in a best-effort manner using AI models. Always review the output to ensure compliance with your organization’s data privacy policies. If AI_REDACT fails to detect or redact any PII in your data, contact Snowflake Support.
Syntax¶
Arguments¶
Required:
inputA VARCHAR value that contains text data that may contain personally identifiable information (PII).
Optional:
categoriesAn ARRAY of string values that specify the types of PII to be redacted. If not specified, all supported PII categories are redacted. See Detected PII categories for a list of supported categories.
Passing an unsupported category results in an error.
return_error_detailsA BOOLEAN flag that indicates whether to return error details in case of error. When set to TRUE, the function returns an OBJECT that contains the value and the error message, one of which is NULL depending on whether the function succeeded or failed.
Requires the session parameter AI_SQL_ERROR_HANDLING_USE_FAIL_ON_ERROR to be set to FALSE.
modeA VARCHAR value that specifies the operating mode. Accepted values:
redact(default): Replaces detected PII with category placeholders, such as [NAME] and [ADDRESS].detect: Returns an OBJECT that contains aspansarray that identifies the location and category of each detected PII instance without redacting the text.
Note
The mode argument is case insensitive.
Returns¶
The return value of AI_REDACT depends on the mode argument.
Redact mode (default)¶
Returns a VARCHAR that contains the input text with PII replaced by category placeholders, such as [NAME] where the input
text was “John Smith”.
Detect mode¶
Returns an OBJECT that contains a spans array. Each element in the array is an OBJECT with the following fields:
| Field | Type | Description |
|---|---|---|
category | VARCHAR | The PII category, such as NAME or ADDRESS. See Detected PII categories for supported categories. |
start | NUMBER | The start index of the detected PII in the input text. |
end | NUMBER | The end index of the detected PII in the input text. |
text | VARCHAR | The matched PII text from the input. |
Error behavior¶
By default, if AI_REDACT cannot process the input, the function returns an error. If the query processes multiple rows, the entire query fails.
When AI_SQL_ERROR_HANDLING_USE_FAIL_ON_ERROR is set to FALSE, the return value on error depends on the return_error_details
argument. The following table shows the return value based on the return_error_details argument:
return_error_detailsReturn value Description FALSE Not passed NULL TRUE OBJECT with valueanderrorfieldsvalue: A VARCHAR value that contains the redacted text, or NULL if an error occurred.error: A VARCHAR value that contains the error message if an error occurred, or NULL if the function succeeded.
Handle row-level errors in multi-row queries¶
Important
If your query fails on every row, the cause might be a known constraint rather than a row-level error. See Limitations for details on token limits, language support, and other restrictions.
AI_REDACT raises an error if it cannot process the input text. When a query redacts multiple rows, an error causes the
entire query to fail. To allow processing to continue with other rows, set the session parameter
AI_SQL_ERROR_HANDLING_USE_FAIL_ON_ERROR to FALSE. Errors then return NULL instead of stopping the query.
With this parameter set to FALSE, you can also pass TRUE as the return_error_details argument to AI_REDACT, which
causes the return value to be an OBJECT that contains separate fields for the redacted text and any error message. One
of these fields is NULL depending on whether the AI_REDACT call processed successfully.
The following example shows how to use error handling when processing multiple rows:
-
Create a table with unredacted text.
-
Set the session parameter.
-
Create a redaction table with columns for
valueanderror. -
Redact PII from
raw_tableand insert the rows intoredaction_tableto store the redacted text and error messages.
Access control requirements¶
Users must use a role that has been granted the SNOWFLAKE.CORTEX_USER database role. See Cortex LLM privileges for more information on this role.
Detected PII categories¶
AI_REDACT supports the detection and redaction of the following categories of PII. The values in the Category column are the strings
that are supported in the optional categories argument.
Category Notes NAME Recognizes full name, first name, middle name, and last name PHONE_NUMBER DATE_OF_BIRTH GENDER Recognizes male, female, and nonbinary AGE ADDRESS Identifies:
- complete postal address (US, UK, CA)
- street address (US, UK, CA)
- postal code (US, UK, CA)
- city (US, UK, CA)
- state (US) or province (CA)
- county, borough, or township (US)
NATIONAL_ID Identifies Social Security numbers (US) PASSPORT Identifies passport numbers (US, UK, CA) TAX_IDENTIFIER Identifies Individual Taxpayer Numbers (ITNs) PAYMENT_CARD_DATA Identifies complete card information, card number, expiration date, and CVV DRIVERS_LICENSE Identifies US, UK, and CA licenses IP_ADDRESS
Note
AI_REDACT supports partial matches for some PII categories. For example, a first name alone is sufficient to trigger redaction with the [NAME] placeholder.
Retain specific PII with detect mode¶
By default, AI_REDACT replaces all detected PII with placeholder values. In some cases, you might want to retain certain PII while redacting the rest. For example, you might want to redact all names in call center transcripts or customer reviews except for known employee names.
Use detect mode to build a selective redaction workflow:
- Call AI_REDACT with the
modeargument set todetectto identify and locate PII in the input text. - Compare the detected spans against an allowlist of values you want to keep.
- Redact only the PII that is not in the allowlist.
For examples of using detect mode, see Detection and selective redaction examples.
Regional availability¶
Limitations¶
-
AI_REDACT uses AI models and may not find all personally identifiable information. Always review the output to ensure compliance with your organization’s data privacy policies. If AI_REDACT fails to redact certain PII, contact Snowflake Support.
-
AI_REDACT works best with well-formed English text. Performance may vary with other languages or text with many spelling, punctuation, or grammatical errors.
-
AI_REDACT currently supports only US PII and some UK and Canadian PII, where noted in Detected PII categories.
-
AI_REDACT is currently limited in the number of tokens it can input and output. Input and output together can be up to 4,096 tokens. Output is limited to 1,024 tokens. If the input text is longer, split it into smaller chunks and redact each chunk separately, perhaps using SPLIT_TEXT_RECURSIVE_CHARACTER. See Chunking example for an example of redacting text that exceeds token limits.
Note
A token is the smallest unit of data processed by the AI model. For English text, industry guidelines consider one token to be approximately four characters, or 0.75 words.
Cost considerations¶
AI_REDACT incurs costs based on the number of input and output tokens processed, as with other Cortex AI Functions. See the Snowflake Pricing Guide for details.
Estimate token usage for large datasets¶
Before you run AI_REDACT on a large dataset, estimate input tokens on representative rows so you can plan for cost and
token limits. Sample text from your table and call AI_COUNT_TOKENS with
'ai_redact' as the function name, using the same input text and optional categories argument you plan to pass to
AI_REDACT. Compare the result against AI_REDACT token limits. If a row exceeds the
limit, split the text into chunks before redacting. See Chunking example.
AI_COUNT_TOKENS returns an estimate of input tokens only. Output token counts depend on the redacted text and aren’t included in the estimate.
For examples, see AI_COUNT_TOKENS examples for AI_REDACT.
Redaction examples¶
Basic redaction examples¶
The following example redacts a name and an address from the input text.
Basic redaction output:
The following example redacts only names and email addresses from the input text. Note that the text only contains a first name, which is recognized and redacted as [NAME]. The input text does not contain an email address, so no email placeholder appears in the output.
Selective redaction output:
End-to-end example¶
The following example processes rows from one table and inserts the redacted output into another table. You could use a similar approach to store the redacted data in a column in an existing table. After redaction, the text is passed to the AI_SENTIMENT function to extract overall sentiment information.
-
Create a table with unredacted text.
-
View unredacted data.
-
Create a redaction table.
-
Redact PII from
raw_tableand insert the rows intoredaction_table. -
View redacted results.
-
Run the AI_SENTIMENT function on redacted text.
Chunking example¶
This example illustrates how to redact PII from long text by splitting the text into smaller chunks, redacting each chunk separately, and then recombining the redacted chunks into the final output. This approach works around AI_REDACT’s token limits.
-
Create a table with patient data.
-
Split the text into chunks, apply AI_REDACT to each chunk, and concatenate the redacted chunks.
-
Query the results.
Detection and selective redaction examples¶
Basic detection example¶
The following example identifies and returns the category, location, and text of each detected PII instance without redacting the input.
Basic detection output:
End-to-end with allowlist example¶
The following example demonstrates a selective redaction workflow that uses detect mode and an allowlist. It loads a
list of names to retain from a staged file, uses AI_REDACT in detect mode to identify PII locations, and then passes
the results to a Python UDF that redacts only the PII not in the allowlist.
-
Retain an allowlist of values by loading the list from a stage into a temporary table.
-
View the allowlist table
Allowlist table output:
-
Create a Python UDF that selectively redacts PII based on the allowlist.
-
Test the UDF.
-
Run AI_REDACT in
detectmode.
End-to-end with allowlist example output:
| MESSAGE | SPANS | RESULT |
|---|---|---|
| My old manager, Washington, used to live in Washington. His first name was Mike. | My old manager, [NAME], used to live in [ADDRESS]. His first name was Mike. |
Legal notices¶
The data classification of inputs and outputs are as set forth in the following table.
| Input data classification | Output data classification |
|---|---|
| Usage Data | Customer Data |
For additional information, refer to Snowflake AI and ML.