Edit

AI_GENERATE_RESPONSE (Transact-SQL)

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

AI_GENERATE_RESPONSE generates text from a prompt and optional supporting data.

Note

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

Syntax

Transact-SQL syntax conventions

AI_GENERATE_RESPONSE ( prompt [ , data ] [ (NULL | ERROR | DEFAULT <value>) ON ERROR ] )

Arguments

prompt

An expression of a character type that defines the instruction.

data

Optional text input used as supporting context for the prompt.

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 generated 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. Generate a short response

SELECT ai_generate_response('Reply in 20 words:', 'The room was noisy.') AS response;

B. Generate responses per row

The following query sends every review_text value to external AI service to generate response.

SELECT review_id,
       ai_generate_response('Draft a professional response in 30 words:', review_text NULL ON ERROR) AS response_text
FROM dbo.hotel_reviews;

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