Snowflake Gen AI Functions · Topic 6 of 26

SUMMARIZE, AI_SUMMARIZE_AGG and AI_AGG - Condensing Text Across Rows

Pick between SUMMARIZE and AI_SUMMARIZE for one value, and AI_SUMMARIZE_AGG or AI_AGG for many rows. Know syntax, GROUP BY use and context window behavior.

  • 4 min
  • Includes a diagram
In a nutshell
  • SUMMARIZE (legacy SNOWFLAKE.CORTEX) and AI_SUMMARIZE summarize one value; AI_SUMMARIZE also takes files and is in preview.
  • AI_SUMMARIZE_AGG(expr) summarizes many rows; AI_AGG(expr, instruction) follows your instruction.
  • Both aggregate functions handle data larger than one model context window.

What it is

Three Cortex functions turn text into shorter text, and the difference is how many rows go in.

  • SUMMARIZE summarizes one text value. It is the older SNOWFLAKE.CORTEX.SUMMARIZE function. The current counterpart is AI_SUMMARIZE, which also accepts a staged image or document.
  • AI_SUMMARIZE_AGG is an aggregate function. It summarizes a whole column of text, such as every review for a product, into one summary.
  • AI_AGG is an aggregate function that applies an instruction you write to a column of text across rows.

Both aggregate functions "support datasets larger than the maximum language model context window". This is the key fact: unlike AI_COMPLETE and AI_SUMMARIZE, they do not fail because the combined text is too long.

How it works

Many rowsreviews per productGROUP BYAI_SUMMARIZE_AGGgeneral summaryorAI_AGGyour instructionreturnsOne stringper group

SUMMARIZE and AI_SUMMARIZE

SELECT SNOWFLAKE.CORTEX.SUMMARIZE(article_text) FROM articles;

SELECT AI_SUMMARIZE(article_text) FROM articles;
SELECT AI_SUMMARIZE(TO_FILE('@docs', 'report.pdf'));

Both return a string summary. AI_SUMMARIZE is marked as a preview feature in its documentation. A very short input, 110 characters or fewer, is returned unchanged, and so is an input where the generated summary would be longer than the source. All processed tokens are billed.

Summary quality varies by language, though many are supported. These functions add their own prompt tokens, so billed tokens exceed your input.

Exam tip

A scenario that asks to summarize each row of a column individually points to SUMMARIZE or AI_SUMMARIZE. A scenario that asks for one summary of many rows points to an aggregate function.

AI_SUMMARIZE_AGG

AI_SUMMARIZE_AGG(<expr>)

It takes one text expression and returns one string. Use GROUP BY to get one summary per group.

SELECT product_id, AI_SUMMARIZE_AGG(review_text) AS review_summary
FROM reviews
GROUP BY product_id;

To summarize several columns, combine them first with CONCAT or ||. The summary is general purpose and there is no way to steer it, which is the reason AI_AGG exists.

AI_AGG

AI_AGG(<expr>, <instruction>)

The second argument is a natural language instruction. Typical instructions are "Summarize the reviews", or "Identify all people mentioned and write a short biography for each of them".

SELECT restaurant_id,
       AI_AGG(review, 'List the three most common complaints') AS complaints
FROM reviews
GROUP BY restaurant_id;

Guidance from the documentation:

  • Use plain English and declarative instructions rather than questions.
  • Describe the text and the intended use case.
  • Split complex instructions into several steps.
  • If the instruction is vague or the data lacks the answer, the function may say that the data does not contain it.

Comparison

Decision between SUMMARIZE or AI_SUMMARIZE for one value, AI_SUMMARIZE_AGG for a general summary of many rows, and AI_AGG when you write the instruction. Both aggregates handle text beyond one context window; LISTAGG into AI_COMPLETE can fail.

AspectSUMMARIZE or AI_SUMMARIZEAI_SUMMARIZE_AGGAI_AGG
Rows per resultOne valueMany rowsMany rows
InstructionNoneNone, general summaryRequired, written by you
Beyond one context windowNoYesYes
Typical useSummarize each documentSummary per product or groupTargeted result, such as top complaints

For a long list of rows, prefer AI_AGG to building one string with LISTAGG and sending it to a single call, since that string could exceed the context window.

Access and billing

All of them need USE AI FUNCTIONS and the CORTEX_USER role. A role that has AI_FUNCTIONS_USER but not CORTEX_USER covers the scalar functions only, so it excludes AI_AGG and AI_SUMMARIZE_AGG. Billing is by input and output tokens, with extra prompt tokens added by the function.

Watch out

Do not concatenate a whole column with LISTAGG and pass it to AI_COMPLETE. It can exceed the context window and fail, while AI_AGG handles it.

Common mistakes

Using AI_COMPLETE to summarize a large table. The combined text may not fit in the context window; use AI_SUMMARIZE_AGG or AI_AGG.

Choosing AI_SUMMARIZE_AGG when a specific outcome is needed. It has no instruction argument; use AI_AGG.

Forgetting GROUP BY. Without it you get one summary for the whole table, not one per group.

Writing the instruction as a question. Declarative instructions work better.

Granting only AI_FUNCTIONS_USER and expecting AI_AGG to work. That role excludes the aggregate functions.

What to remember for the exam

  • SUMMARIZE (legacy SNOWFLAKE.CORTEX) and AI_SUMMARIZE summarize one value; AI_SUMMARIZE also takes files and is in preview.
  • AI_SUMMARIZE_AGG(expr) summarizes many rows; AI_AGG(expr, instruction) follows your instruction.
  • Both aggregate functions handle data larger than one model context window.
  • Use GROUP BY for one result per group, and combine columns with CONCAT or ||.
  • Inputs of 110 characters or fewer are returned unchanged by AI_SUMMARIZE.

Check yourself

  • Which function summarizes thousands of reviews per product without hitting the context limit?

    AI_SUMMARIZE_AGG, grouped by product (or AI_AGG when an instruction is needed).

  • What argument does AI_AGG have that AI_SUMMARIZE_AGG lacks?

    An instruction string that tells it how to aggregate.

  • What happens when you pass 100 characters to AI_SUMMARIZE?

    The input is returned unchanged because it is 110 characters or fewer.

Topic 6 of 26 in Snowflake Gen AI Functions

Test yourself on Snowflake Gen AI Functions

The full Snowflake GES-C02 question bank has 585 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Snowflake. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.