Edit

AI_FIX_GRAMMAR (Transact-SQL)

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

The AI_FIX_GRAMMAR function corrects grammar and improves sentence quality for the input text.

Note

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

Syntax

Transact-SQL syntax conventions

AI_FIX_GRAMMAR ( text [ (NULL | ERROR | DEFAULT <value>) ON ERROR ] )

Arguments

text

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

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(max) with corrected text.

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. Fix grammar in a string

SELECT ai_fix_grammar('Th room are clean and staff were nice') AS fixed_text;

Expected result: The rooms are clean, and the staff were nice.

B. Update a column safely

UPDATE dbo.hotel_reviews
SET review_text = ISNULL(ai_fix_grammar(review_text), review_text);