AI_COMPLETE function to enrich records with AI-generated structured attributes directly within NQL. The entire inference pipeline runs inside your Snowflake data plane—your data never leaves your infrastructure.
AI_COMPLETE is available on Snowflake data planes only.What you will build
A materialized view that takes raw customer support tickets and enriches each record with:- A standardized issue category
- A priority level
- A sentiment score
- Extracted product mentions
Prerequisites
- A Snowflake data plane with AI model access configured
- A dataset with text content to enrich
- Familiarity with NQL syntax and materialized views
Step 1: Define the output schema
Before writing NQL, design the JSON Schema that describes the structured attributes you want the model to produce. This schema constrains the model’s output, guaranteeing predictable, typed results.| Decision | Why |
|---|---|
enum on category and priority | Restricts output to known values, preventing free-form drift |
number for sentiment | Enables numeric filtering and aggregation downstream |
additionalProperties: false | Prevents the model from adding unexpected fields |
description on each field | Guides the model’s interpretation of what each field means |
Step 2: Build prompt strings in a CTE
AI_COMPLETE requires its prompt parameter to be a column reference, not a string literal. Use a Common Table Expression (CTE) to assemble prompts from your source columns.
- Prefix each field with a label (e.g.,
' Ticket subject: ') so the model can distinguish between data elements - Filter rows before calling
AI_COMPLETEto avoid unnecessary inference costs - Use
COALESCEto handle nulls:COALESCE(company_data."support_tickets".subject, 'No subject')
Step 3: Call AI_COMPLETE
Pass the prompt column toAI_COMPLETE along with your schema. The response is a JSON string that you parse in the next step.
| Parameter | Value | Reason |
|---|---|---|
model | 'openai-gpt-5' | Choose the model that best fits your accuracy and cost requirements |
model_parameters | '{"temperature": 0}' | Temperature 0 produces deterministic, consistent classifications |
show_details | TRUE | Returns the full structured_output array for reliable JSON parsing |
Step 4: Parse the response into columns
UsePARSE_JSON and path notation to extract individual fields from the AI response into typed columns.
['structured_output'][0]['raw_message'] navigates to the model’s schema-validated output. Each field within raw_message corresponds to a property in your response_format schema.
Complete example
Here is the full query wrapped as a materialized view that refreshes weekly:Adapting this pattern
The CTE → AI_COMPLETE → PARSE_JSON pattern works for any enrichment task. Here are variations for common scenarios.Product classification
Classify products into a taxonomy from their name and description:Entity extraction
Extract structured entities from free-text fields:Best practices
Prompt design:- Label each data element clearly (e.g.,
' Brand: ',' Description: ') - Include a brief instruction at the start of the prompt describing the task
- Use
COALESCEto handle nullable columns and avoid sending nulls to the model
- Use
enumto constrain categorical outputs to known values - Set
additionalProperties: falseto prevent unexpected fields - Add
descriptionto each property to guide the model - Use
numbertypes for scores you want to filter or aggregate
- Filter rows with
WHEREbefore theAI_COMPLETEcall to reduce inference volume - Use
FETCH NEXT N ROWS ONLYto limit batch sizes during development and testing - Set
temperatureto0for deterministic, reproducible results - Keep schemas focused—fewer fields means fewer tokens per response
Related content
AI_COMPLETE Reference
Function syntax and parameter details
Model Inference
How AI inference works within your data plane
Structured Output
Designing JSON schemas for AI responses
Creating Materialized Views
Automating queries with scheduled materialized views

