Uredi

AI_CLASSIFY (Transact-SQL)

Applies to: SQL analytics endpoint in Microsoft Fabric and Warehouse in Microsoft Fabric

The AI_CLASSIFY function classifies input text into one of the labels you provide.

Note

  • AI_CLASSIFY is in preview.
  • AI_CLASSIFY is available only in SQL analytics endpoint and Warehouse in Microsoft Fabric.

Syntax

Transact-SQL syntax conventions

AI_CLASSIFY ( text, class1, class2 [ , ...n ] [ (NULL | ERROR | DEFAULT <value>) ON ERROR ] )

Arguments

text

An expression of a character type, for example nvarchar, varchar, nchar, or char.

class1, class2, ...n

One or more candidate class labels, provided as string literals or string expressions.

ON ERROR

The ON ERROR clause controls how an AI function handles processing errors.

  • NULL ON ERROR returns NULL when the function can't process a value. This is the default behavior and doesn't need to be explicitly specified.
  • ERROR ON ERROR causes the entire query to fail if an error occurs while processing any input value.
  • DEFAULT <value> ON ERROR returns the specified default value instead of NULL when an error occurs.

Errors can be caused by Responsible AI safety checks, input size limits, transient service issues, or other processing failures.

Return types

Returns nvarchar containing the selected class label.

Remarks

AI functions return NULL if the AI model can't process the text. Common reasons include:

  • Responsible AI rules block inappropriate content in the input text.
  • Input text exceeds token limits. The current model supports up to 15 KB of text.

Examples

A. Classify text with custom labels

SELECT ai_classify('Room was dirty', 'service', 'dirt', 'food') AS classification;

Expected result: dirt

B. Classify rows in a table

The following query sends every review_text value to external AI service to classify it.

SELECT review_id,
       ai_classify(review_text, 'service', 'dirt', 'food', 'other' DEFAULT 'unknown' ON ERROR) AS category
FROM dbo.hotel_reviews;

The optional DEFAULT 'unknown' ON ERROR clause instructs AI function to return the value 'unknown' for every input value that cannot be processed.