← Technology, Science & Infrastructure ▣ Distilled locally

Introducing new AI functions for AlloyDB: ai.classify and ai.extract

By Paul Ramsey
November 11, 2026  ·  Google Cloud Blog ↗
Introducing new AI functions for AlloyDB: ai.classify and ai.extract

Transactional databases store vast amounts of unstructured text — clinical notes, customer reviews, support tickets, invoices, and property descriptions. Historically, working with this text meant building complex application code: You had to export data from the database, call external model endpoints, write custom prompt wrappers, parse unpredictable responses, manage retry logic, and update database records. This application-side loop creates operational friction. It introduces query latency, bloats application code, increases model token costs, and creates data compliance risks by moving sensitive information across multiple network boundaries.

We built AlloyDB AI to bring Gemini world knowledge directly into your operational database. Rather than treating artificial intelligence as a disconnected external service, we integrated model intelligence directly into the database engine. Today, we are announcing the preview of two new SQL functions in the AlloyDB AI query engine — ai.classify and ai.extract — available in the google_ml_integration extension (version 1.6.1 or later) on PostgreSQL 18, allowing you to perform classification and structured extraction directly inside your database using standard SQL. Supported by smart batching for AI functions (previously known as AI Function Acceleration), these functions deliver production scalability and performance. 

Now, let’s take a look at these new functions and how to use them.

Classifying text directly in SQL with ai.classify

The ai.classify function categorizes text into user-defined classes without requiring manual prompt engineering. You can specify categories as a simple list of labels or provide a list containing label names and descriptions to guide model context.

Key capabilities include:

Here’s a quick example showing how you can classify customer feedback directly within a SQL query:

code_block <ListValue: [StructValue([('code', "SET google_ml_integration.enable_preview_ai_functions = TRUE;\r\n\r\nCREATE TABLE product_reviews (id INT, review TEXT);\r\n\r\nINSERT INTO product_reviews VALUES\r\n(1, 'Fantastic, would buy again!'),\r\n(2, 'Broken on arrival.'),\r\n(3, 'Mediocre, not worth the price.');\r\n\r\nSELECT unnest(\r\n ai.classify(\r\n inputs => ARRAY(SELECT review FROM product_reviews ORDER BY id),\r\n categories => ARRAY[\r\n ['positive', 'Good sentiment'],\r\n ['neutral', 'Neither good nor bad sentiment'],\r\n ['negative', 'Bad sentiment']\r\n ],\r\n batch_size => 5\r\n )\r\n) AS sentiment;\r\n\r\n-- Expected Output:\r\n-- sentiment\r\n-- ---------\r\n-- positive\r\n-- negative\r\n-- negative"), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x7f7e91d6c550>)])]>

Extracting structured JSON with ai.extract

The ai.extract function transforms unstructured text into structured JSON data matching your target schema — specified as a JSON schema or a text list of fields. It handles system instructions internally, saving you from writing prompt templates or response parsers.

Key capabilities include:

Here’s an example extracting clinical fields from free-form medical notes:

code_block <ListValue: [StructValue([('code', 'SET google_ml_integration.enable_preview_ai_functions = TRUE;\r\n\r\nSELECT ai.extract( \r\n input => \'Patient Jane Smith, 52 years old, visited the clinic on Oct 25. She is currently experiencing a mild fever, persistent cough, and severe body aches. The doctor prescribed Ibuprofen and Amoxicillin.\',\r\n output_schema => \'{\r\n "patient_name": {"type": "string"},\r\n "age": {"type": "integer"},\r\n "symptoms": {\r\n "type": "array",\r\n "items": {"type": "string"}\r\n },\r\n "prescribed_medications": {\r\n "type": "array",\r\n "items": {"type": "string"}\r\n }\r\n }\'\r\n);\r\n\r\n--- Expected output \r\n-- {\r\n-- "patient_name": "Jane Smith",\r\n-- "age": 52,\r\n-- "symptoms": [\r\n-- "mild fever",\r\n-- "persistent cough",\r\n-- "severe body aches"\r\n-- ],\r\n-- "prescribed_medications": [\r\n-- "Ibuprofen",\r\n-- "Amoxicillin"\r\n-- ]\r\n-- }'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x7f7e919c7990>)])]>

Speeding up queries with smart batching

Running AI evaluations row-by-row can slow down query execution. AlloyDB addresses this with smart batching for AI functions. When you use ai.classify or ai.extract in SQL WHERE clause expressions, the database engine automatically groups inputs into optimized micro-batches before sending them to Gemini Enterprise Agent Platform models.

Smart batching delivers significant performance and efficiency improvements:

Real-world industry applications

Native classification and extraction functions support operational workflows across key industry sectors:

Next steps and getting started

We are continuing to expand in-database AI capabilities in AlloyDB, with upcoming enhancements planned for advanced cost optimization, line-level provenance tracking, and query pushdowns beyond WHERE clauses.

To start using ai.classify and ai.extract in your database:

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.