top of page

How to classify text in SQL with Snowflake Cortex AI_CLASSIFY

2 minutes ago
8 min read
Cover with the title How to classify text in SQL with Snowflake Cortex AI_CLASSIFY over a teal city skyline, Data Studios original cover

Snowflake's AI_CLASSIFY function assigns one or more labels from a list you supply to each row of text, directly inside a SELECT statement, and returns the result as an object with a labels array. For an analyst sitting on a table of support tickets, survey comments or product reviews, that means the categorization step runs where the data already lives, under the same roles and warehouse controls as any other query, without exporting rows to a spreadsheet or a chatbot.


The function is documented in the Snowflake SQL reference as part of the Cortex AI Functions family, and it replaces the older CLASSIFY_TEXT function. It accepts plain category names or category objects with short descriptions, an optional configuration object with a task description, few-shot examples and a single or multi-label mode, and an optional flag that returns error details instead of NULL. All of the syntax and limits below were checked against Snowflake's documentation on November 23, 2026.


The hard part is the review. A label that looks plausible on screen can still be wrong, and a classification run over 200,000 tickets is only as good as the sample someone checked by hand. The worked example below uses twelve illustrative tickets, a reviewer's labels and a comparison query whose correct output was computed in Python, so you can test your own AI_CLASSIFY results the same way.


··········


SNOWFLAKE AI_CLASSIFY REQUIREMENTS AND LIMITS.


Snowflake's documentation sets out who can call the function, which inputs it accepts and where it runs. The AI_CLASSIFY reference page states that users need a role with the SNOWFLAKE.CORTEX_USER database role, and the general AI Functions page adds that a role needs the USE AI FUNCTIONS account-level privilege together with either the CORTEX_USER or the AI_FUNCTIONS_USER database role. If a call fails with a privilege error, that combination is the first thing to check with your account administrator.


Text input is available in every region listed on the reference page, including AWS US West 2 (Oregon), AWS US East 1 (N. Virginia), Azure East US 2 and AWS cross-region inference. Image input is listed for fewer AWS regions, and document input is in preview and available only through AWS cross-region inference, so a production pipeline on support text has the widest regional coverage.


........


Item

What Snowflake documents

Access

USE AI FUNCTIONS privilege plus CORTEX_USER or AI_FUNCTIONS_USER

Input types

Text, image, document (document in preview)

Categories

At least two unique values, case sensitive

Category count

Bounded by the token window; more than 20 may reduce accuracy

Label description

25 words or fewer

Task description

50 words or fewer

Output modes

single (default) or multi

Errors

NULL by default; error details with the fourth argument set to TRUE

Documents

Up to 100 categories, 100 pages, 22 MB, single mode only


........


Billing follows tokens. The reference page explains that the function adds its own prompt around your text, and that every label, description and example is sent as input tokens on each call, so a long category list with detailed descriptions costs more per row than two bare labels. The current AI Credit rates for AI_CLASSIFY are covered in our breakdown of Snowflake Cortex AI Functions pricing per million tokens, and the warehouse that runs the query is billed on top as usual.


··········


WRITING THE FIRST AI_CLASSIFY QUERY ON A TICKETS TABLE.


The simplest call passes the text column and an array of category names. Because the function returns an object, you extract the first element of the labels array and cast it to a string to get a column you can group and filter. The documented examples read the result with the :labels path, and the [0] index with a ::STRING cast is standard Snowflake syntax for semi-structured values.


SELECT
  ticket_id,
  ticket_text,
  AI_CLASSIFY(
    ticket_text,
    ['Billing', 'Login and access', 'Bug report', 'Feature request']
  ):labels[0]::STRING AS ai_label
FROM support_tickets;

Snowflake's usage notes ask for plain English categories that are descriptive and mutually exclusive, with no abbreviations, codes or jargon in the labels. Names such as "Category 1" or "L2-ACC" give the model nothing to work with, and overlapping labels such as "Account" and "Login" force arbitrary choices that no review can settle afterward.


The second version adds the two features that usually improve accuracy on business text. Label descriptions state what separates one category from another, and the task_description key in the configuration object explains the purpose of the classification. A single few-shot example covers the case the team argues about most often.


SELECT
  ticket_id,
  AI_CLASSIFY(
    ticket_text,
    [
      {'label': 'Billing', 'description': 'charges, refunds, invoices, taxes and plan prices'},
      {'label': 'Login and access', 'description': 'passwords, single sign-on, two-factor codes and locked accounts'},
      {'label': 'Bug report', 'description': 'a feature that exists in the product but fails or shows wrong results'},
      {'label': 'Feature request', 'description': 'a capability or plan limit the customer wants changed or added'}
    ],
    {
      'task_description': 'Classify each customer support ticket by the team that should handle it',
      'examples': [
        {
          'input': 'The authenticator code is refused after I changed phones',
          'labels': ['Login and access'],
          'explanation': 'Sign-in failures go to the access team even when an app is involved'
        }
      ]
    }
  ):labels[0]::STRING AS ai_label
FROM support_tickets;

Keep the few-shot examples out of the sample you later use for evaluation. An example that repeats a test row inflates the agreement rate and tells you nothing about the rest of the table.


When a ticket can belong to more than one category, set 'output_mode' to 'multi' in the configuration object. The labels array can then hold several values, and you keep the whole array instead of the first element. Snowflake notes that document inputs support only the single mode and do not accept examples.


··········


WORKED EXAMPLE: CHECKING AI_CLASSIFY LABELS AGAINST A REVIEWED SAMPLE.


The table below is illustrative. It contains twelve tickets, the label a support lead assigned by hand, and a hypothetical AI label column with two disagreements, built to show how the check query behaves. It serves as test data for the verification step, and none of it is output observed from Snowflake.


........


ticket_id

ticket_text

reviewer_label

ai_label

101

I was charged twice for my November invoice

Billing

Billing

102

The password reset email never arrives

Login and access

Login and access

103

Export to CSV crashes when the report has more than 50 columns

Bug report

Bug report

104

Please add a dark mode to the dashboard editor

Feature request

Feature request

105

Can I get a refund for the unused months on my annual plan

Billing

Billing

106

My account is locked after the SSO change

Login and access

Login and access

107

The date filter shows October data when I pick November

Bug report

Bug report

108

It would help to schedule reports to Slack

Feature request

Feature request

109

Two-factor codes are rejected on the mobile app

Login and access

Bug report

110

Our VAT number is missing from the invoice PDF

Billing

Billing

111

Charts render blank in Safari since yesterday

Bug report

Bug report

112

Allow more than five users on the starter plan

Feature request

Billing


........


In practice you would create this table by joining the stored AI_CLASSIFY output to a column of reviewer labels on a random sample of a few hundred tickets. The first check is the agreement rate, and the second is the confusion count, which shows where the disagreements sit. Both queries use standard SQL that runs unchanged in Snowflake, and they were executed locally on the illustrative rows above.


SELECT
  COUNT(*) AS reviewed,
  SUM(CASE WHEN ai_label = reviewer_label THEN 1 ELSE 0 END) AS matches,
  ROUND(100.0 * SUM(CASE WHEN ai_label = reviewer_label THEN 1 ELSE 0 END) / COUNT(*), 1) AS agreement_pct
FROM ticket_labels;

SELECT reviewer_label, ai_label, COUNT(*) AS n
FROM ticket_labels
GROUP BY reviewer_label, ai_label
ORDER BY reviewer_label, ai_label;

On the illustrative data, the first query returns 12 reviewed rows, 10 matches and an agreement rate of 83.3 percent. The confusion query returns six rows, and the per-label figures below were computed in Python from the same data.


........


Label

Reviewer count

AI count

Correct

Precision

Recall

Billing

3

4

3

75.0%

100.0%

Login and access

3

2

2

100.0%

66.7%

Bug report

3

4

3

75.0%

100.0%

Feature request

3

2

2

100.0%

66.7%


........


The per-label view says more than the single agreement figure. Billing collects a ticket about a user limit on the starter plan, which the reviewer filed as a feature request, and Bug report collects a two-factor failure that belongs to the access team. Both errors sit on boundaries the category descriptions in the second query were written to clarify, which is the usual fix: tighten the description or add a few-shot example for the contested case, rerun on the same sample and compare the confusion counts again.


A third query lists the disagreements so a reviewer can read them in full.


SELECT ticket_id, ticket_text, reviewer_label, ai_label
FROM ticket_labels
WHERE ai_label <> reviewer_label
ORDER BY ticket_id;

On the illustrative data it returns tickets 109 and 112. In a real sample, read every row it returns before deciding whether the problem is the label set, the descriptions or tickets that genuinely need two labels.


··········


REVIEW STEPS FOR AI_CLASSIFY RESULTS IN PRODUCTION.


Several failure patterns are predictable from Snowflake's own usage notes. Non-English text, log lines and code snippets fall outside the plain English input the documentation recommends, so a ticket table that mixes languages needs a filter or a separate route for those rows. Labels are case sensitive, which means a downstream filter on 'billing' will silently miss rows labeled 'Billing'. Rows that fail return NULL by default while the rest of the query completes, so count NULL labels after every run, or pass TRUE as the fourth argument to get an object with value and error fields that explains each failure.


Store the output with the run date and the exact category list used. When someone edits a description six months later, the old and new labels are no longer comparable, and a stored version column is the only way to tell which rows came from which definition. Snowflake's newer tooling for testing AI functions against labeled data is described in our article on Cortex AI Function Studio and cheaper model testing, which builds on the same idea of a reviewed sample.


Decisions with consequences stay with people. A label can route a ticket or feed a dashboard of complaint volumes, but refunds, reopening locked accounts and anything touching a contract should go through a human queue regardless of what the function returns.


For smaller jobs outside the warehouse, the same review method applies to spreadsheet tools such as Fill with Gemini in Google Sheets, and teams on Google Cloud can compare the approach with BigQuery AI.CLASSIFY and its optimized mode.


··········


SNOWFLAKE AI_CLASSIFY DATA HANDLING AND GOVERNANCE.


Snowflake's AI Functions documentation states that the large language models it provides are deployed within the Snowflake Service perimeter, and its legal notices table classifies function inputs as Usage Data and outputs as Customer Data. Generally available functions are listed as Covered AI Features and preview functions as Preview AI Features, which places document input under preview terms for now.


Because the function runs as SQL, the usual controls apply. Row access policies and masking policies on the source table decide which text a role can send to the function, and the database roles above decide who can call it at all. The two documentation pages checked for this piece do not address model training on customer data, so teams with that requirement should confirm it in their Snowflake agreement.


··········


CLASSIFYING TICKETS WITH SNOWFLAKE AI_CLASSIFY: WHAT TO SET BEFORE THE FIRST FULL RUN.


Snowflake documents AI_CLASSIFY as a SQL function that returns a labels array, accepts category descriptions of up to 25 words and a task description of up to 50 words, supports single and multi-label output, and returns NULL on errors unless you request error details. Access requires the USE AI FUNCTIONS privilege with the CORTEX_USER or AI_FUNCTIONS_USER database role, text input is available in all listed regions, and document input remains in preview through cross-region inference only.


What the documentation leaves to you is the quality bar. Keep the category list short and mutually exclusive, describe the boundaries the team disputes, check a reviewed sample with the agreement and confusion queries before trusting the full table, and store the category definition alongside each run. Cost depends on how many tokens your text, labels, descriptions and examples add per row, so test the final prompt on a sample before running it over the full history.


··········


FOLLOW US FOR MORE.


·····


DATA STUDIOS


·····


datastudios.org


bottom of page