← Technology, Science & Infrastructure ▣ Distilled locally

Turn unstructured data into structured SQL insights with BigQuery AI.AGG (now GA)

By Alicia Williams
December 1, 2026  ·  Google Cloud Blog ↗

Since we first introduced AI.AGG() in Preview, data teams have used it to synthesize millions of rows of unstructured text and multimodal data directly in SQL, from distilling product feedback to investigating application logs.

The AI.AGG() function in BigQuery recently reached general availability (GA). Alongside the GA release, we also rolled out one of the most requested capabilities from our preview users: custom structured output support. Combined with BigQuery's new token-based cost management controls, you can now build predictable, production-ready semantic aggregation pipelines straight from SQL.

Moving from free-form text to structured outputs

Using Gemini models, AI.AGG() brings SQL aggregation to unstructured text and multimodal data. You can pass natural language instructions to AI.AGG() to synthesize thousands or even millions of rows per group, all without writing custom application pipelines or hitting model context window limits. If you want a closer look at how BigQuery handles multi-level aggregation across massive datasets under the hood, read our deep dive into the BigQuery AI.AGG() function.

During Preview, AI.AGG() returned a paragraph summary for each group. This works well when a human analyst is reading query results in the console. However, teams building automated data pipelines, downstream SQL joins, or business intelligence dashboards need structured fields that they can filter, unnest, and chart.

The new output_schema parameter solves this directly. Instead of returning a block of free-form text, BigQuery automatically formats the model's response to match the exact structure you define, returning neatly organized fields like text, numbers, booleans, or lists that are ready to query right away.

What you can build with AI.AGG() and structured output

Getting structured results directly from AI.AGG() unlocks new workflows for analytics and engineering teams:

Seeing AI.AGG() with structured output in action

Let's look at how the output_schema parameter works in practice. In our previous deep dive blog, we used AI.AGG() to analyze unstructured Apache Spark INFO logs grouped by Component, which returned a free-form text summary of each component's behavior and anomalies.

1 - Results without structured output

Without output_schema, AI.AGG() returns a narrative block of text for each group.

By adding output_schema to that same query, we can turn those free-form summaries into structured data ready for downstream dashboards or alerting. Here, output_schema instructs AI.AGG() to return a description of the component's normal operation alongside an issues array, capturing a concise summary of each detected problem (issue_desc) and how many times it occurred (occurrences):

code_block <ListValue: [StructValue([('code', 'SELECT\r\n Component AS spark_component,\r\n COUNT(*) AS log_count,\r\n AI.AGG(\r\n Content,\r\n \'\'\'Analyze these Spark system INFO logs. For each component, describe the normal operation of this component, and explicitly identify any hidden inefficiencies, latency spikes, repeated retries, or unusual patterns, along with how many instances of each issue occurred, if they are present.\r\n \r\n Example response: `{"description": "distributes, processes, and reports the status of tasks across multiple stages of execution.", "issues": [{"issue_title": "Stage 24 shows a significant backlog of tasks due to high CPU usage", "occurrences": 164]}.\r\n \'\'\',\r\n endpoint => \'gemini-4-argon\',\r\n output_schema=>"description STRING NOT NULL OPTIONS (description=\'Description of the normal operation of the component\'), issues ARRAY<STRUCT<issue_desc STRING NOT NULL OPTIONS (description=\'A brief summary of the nature of the problem detected\'), occurrences INT64 NOT NULL OPTIONS(description=\'Number of distinct times this issue appeared in the logs\')>>"\r\n ) AS performance_analysis\r\nFROM\r\n `bq_logs_demo.spark_logs_unstructured`\r\nGROUP BY\r\n Component\r\nORDER BY\r\n log_count DESC;'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x7f3e67091e10>)])]>

You can see how the results are now neatly organized into nested STRUCT and ARRAY columns, allowing a single component like python.PythonRunner to list multiple distinct issues with their exact occurrence counts:

2 - Results with structured output

With output_schema, AI.AGG() outputs strongly typed STRUCT and ARRAY fields that can be filtered, unnested, and charted directly.

Best practices for maximizing result quality

As you design queries with AI.AGG() and output_schema, keep these four best practices in mind:

Get started today

With AI.AGG() now in GA, new support for structured outputs, and token-based quotas for cost management, you have everything you need to run AI-powered aggregations reliably in production.

To start building with AI.AGG(), check out these resources:

Originally published by Google Cloud Blog ↗ · rebuilt here from its own full-text RSS feed · all rights remain with the publisher, please read and support them there.