Snowflake Cortex AI Function: Multirow error handling improvements (Preview)¶
Attention
This behavior change is in the 2026_02 bundle.
For the current status of the bundle, refer to Bundle history.
When this behavior change bundle is enabled, most Snowflake Cortex AI Functions return NULL on error, rather than raising the error. This allows multi-row queries to complete even if some rows have errors. Additionally, the AI_PARSE_DOCUMENT function changes its return value to be more consistent with other AI Functions’ error handling.
Before the change |
Most AI Functions raise an error when the function does not succeed, preventing multi-row queries from completing when even one row cannot be processed. |
|---|---|
After the change |
Most AI Functions return NULL when the function does not succeed, allowing multi-row queries to complete when some rows cannot be processed. Rows with errors can easily be excluded from multi-row results. A new, optional, final parameter in the affected AI Functions, Additionally, minor changes to the AI_PARSE_DOCUMENT function’s return value make it more consistent with other AI Functions, as follows:
|
Affected AI functions¶
The following AI Functions are affected by this behavior change:
AI_COMPLETE: Generates text responses from text or image prompts using a specified large language model (LLM).
AI_CLASSIFY: Classifies text or images into user-defined categories.
AI_FILTER: Applies semantic filters on text and images expressed in natural language.
AI_PARSE_DOCUMENT: Extracts document structure, text, images, and tables as Markdown.
AI_TRANSCRIBE: Transcribes audio or video files with speaker identification and timestamps.
AI_TRANSLATE: Translates text between supported languages.
AI_SENTIMENT: Performs sentiment classification on text content.
AI_COUNT_TOKENS: Estimates token usage for prompts.
Unaffected AI functions¶
The following AI Functionsare not affected by this behavior change:
AI_EXTRACT: This function already handles errors by returning error information as a separate field in the result and does not cause multi-row queries to fail due to a single error. AI_EXTRACT’s behavior is similar to the new behavior of other AI Functions when
return_error_detailsis TRUE, although this function does not acceptreturn_error_details.AI_AGG and AI_SUMMARIZE_AGG: Aggregate functions are not in scope for this BCR. Snowflake is still considering how rows that cause errors should behave in aggregation. These functions’ behavior might change in a future BCR.
AI_EMBED: This function returns a VECTOR, which is not currently supported for VARIANT objects. This function’s behavior might change in a future BCR.
Older AI Functions in the SNOWFLAKE.CORTEX namespace. Snowflake does not intend to change these functions’ behavior.
Helper functions¶
This BCR includes two helper functions that assist with extracting information from the error details object returned when return_error_details is set to TRUE.
These functions provide convenient access to alternative error handling behavior when return_error_details is set to TRUE.
AI_NULL_IF_ERROR: Returns NULL if the
errorfield of the given value is not NULL, otherwise returns thevaluefield. This is the same behavior as whenreturn_error_detailsis set to FALSE.AI_THROW_IF_ERROR: Raises an error from
errorfield of the provided object if it is not NULL, otherwise returns thevaluefield. This is the same behavior that AI Functions had before this behavior change.
Examples¶
The following examples illustrate the new error handling behavior. These examples use AI_TRANSLATE, but the behavior is the same for all affected functions.
New behavior with and without error¶
The first code sample shows output when the function succeeds, and the second example shows output when the function fails due to an invalid language code.
-- succeeds
SELECT AI_TRANSLATE(spanish_comment, 'es', 'en') as english_comment, "Este es un commentario" as spanish_comment;
Result:
+-------------------+------------------+
| ENGLISH_COMMENT | SPANISH_COMMENT |
|-------------------+------------------|
| This is a comment | Este es un |
| | comentario |
+-------------------+------------------+
-- fails
SELECT AI_TRANSLATE(spanish_comment, 'es', 'xx') as english_comment, "Este es un commentario" as spanish_comment;
Result
+-------------------+------------------+
| ENGLISH_COMMENT | SPANISH_COMMENT |
|-------------------+------------------|
| NULL | Este es un |
| | comentario |
+-------------------+------------------+
New behavior with error details¶
As before, the first code sample is the success case and the second is the error case.
-- succeeds
SELECT AI_TRANSLATE(spanish_comment, 'es', 'en', TRUE) as result, "Este es un commentario" as spanish_comment;
Result:
+--------------------------------+------------------+
| RESULT | SPANISH_COMMENT |
|--------------------------------|------------------|
| { | Este es un |
| "value": "This is a comment",| comentario |
| "error": NULL | |
| } | |
+--------------------------------+------------------+
-- fails
SELECT AI_TRANSLATE(spanish_comment, 'es', 'xx', TRUE) as result, "Este es un commentario" as spanish_comment;
Result:
+--------------------------------+------------------+
| RESULT | SPANISH_COMMENT |
|--------------------------------|------------------|
| { | Este es un |
| "value": NULL, | comentario |
| "error": "Invalid language | |
| \"xx\" | |
+--------------------------------+------------------+
Multirow query¶
The following examples show how to use the new error handling behavior in a multirow query. If an error occurs when processing a row, that row is not included in the result. The example data is assumed to be a table containing user comments in various languages, and the query attempts to translate them all into English using AI_TRANSLATE.
SELECT
AI_TRANSLATE(comment, comment_language, 'en') as translation_result,
comment_language,
comment
FROM comments
WHERE translation_result IS NOT NULL;
The example below shows how to use the return_error_details parameter to achieve the same result as the previous example.
SELECT
AI_TRANSLATE(comment, comment_language, 'en', TRUE) as translation_result,
comment_language,
comment
FROM comments
WHERE translation_result:value IS NOT NULL;
Ref: 2184