ai_enrich function
Applies to: Databricks SQL
Databricks Runtime
This feature is in Beta. Workspace admins can control access to this feature from the Previews page. See Manage Databricks previews.
The ai_enrich() function generates new columns for a row from a schema you define. Given input content and a target schema, the function calls an AI model to fill in each field. It can optionally ground the generated values in one or more knowledge sources like an AI Search index or a live web search, so the values reflect your own data or up-to-date information rather than the model's training data alone.
Use ai_enrich to add derived attributes to a table at scale. You can tag and categorize records, fill in missing metadata, or attach researched context to each row from a single SQL function call. By default, each generated field is returned with a short rationale that explains how the value was derived.
Requirements
- Databricks Runtime 18.2 or above.
- If you are using Serverless compute, the serverless environment version must be set to 3 or above, as this enables features like
VARIANT. - To ground enrichment in an AI Search index, you need one or more AI Search indexes to use as knowledge sources.
- The
ai_enrichfunction is available using Databricks notebooks, SQL editor, Databricks workflows, jobs, or Spark Declarative Pipelines on Lakeflow.
Data security
Your document data is processed within the Databricks security perimeter. Databricks does not store the parameters that are passed into the AI function calls, but does retain metadata run details, such as the Databricks Runtime version used.
Syntax
ai_enrich(content, schema [, knowledge_sources] [, options])
Arguments
-
content: ASTRINGorVARIANTexpression. The row to enrich.VARIANTinput, such as the output of another AI function likeai_parse_document, is serialized to a JSON string internally. -
schema: ASTRINGliteral that defines the columns to generate. It uses the same grammar asai_extract. The schema can be:-
Simple schema: A JSON array of field names, which are generated as strings.
JSON["industry", "headquarters_country", "year_founded"] -
Advanced schema: A JSON object with type information, descriptions, and nested structures.
- Supports
string,integer,number,boolean, andenumtypes. Performs type validation. Maximum of 500 enum values. - Supports nested objects using
"type": "object"with"properties". - Supports arrays of primitives or objects using
"type": "array"with"items". - Optional
"description"field for each property to guide the generated value.
JSON{
"hq_address": {
"type": "object",
"description": "Registered headquarters address",
"properties": {
"city": { "type": "string" },
"country": { "type": "string" }
}
},
"founding_team": { "type": "array", "description": "Full names of the founders", "items": { "type": "string" } },
"founding_year": { "type": "integer", "description": "Year the company was founded" }
} - Supports
-
-
knowledge_sources: An optionalVARIANTorSTRINGexpression containing a JSON array of knowledge source configurations used to ground the generated values. See Knowledge source configuration. -
options: An optionalMAP<STRING, STRING>. Supported keys:'version': The function version to use.'instructions': ASTRINGof up to 20,000 characters. Natural-language guidance that describes the enrichment task. Optional; the schema field names alone can drive the enrichment. For example,'Infer attributes for each company from its public profile.''enableRationale':'true'(default) or'false'. When'true', each generated field is returned as a{rationale, value}object, whererationaleexplains how the value was derived. Set to'false'to return{value}only.
Knowledge source configuration
The knowledge_sources argument is a JSON array. Each element is a {type, description, config} envelope. The type field identifies how ai_enrich retrieves grounding context, and the config field contains the source-specific configuration.
Key | Required | Description |
|---|---|---|
| Yes | The knowledge source type. One of |
| No | A natural-language description of the source. Used to help the function decide when and how to retrieve from it. |
| Yes | An object containing the source-specific configuration. See AI Search index configuration for |
AI Search index configuration
For an AI Search index with type set to vector_search, config accepts the following keys:
Key | Required | Description |
|---|---|---|
| Yes | The Unity Catalog three-level name of the AI Search index, for example |
| Yes | The column in the index that contains the document text. |
| Yes | The column in the index that contains the document URI. |
| No | A comma-separated string or JSON array of columns available for metadata filtering. When omitted, the list is derived from the index schema, excluding reserved, text, and document URI columns. |
You can configure more than one vector_search source in a single call.
Web search configuration
For a web search with type set to web_search, config accepts the following optional keys. Web search runs through web search on Databricks; see Limitations for availability.
Key | Required | Description |
|---|---|---|
| No | A JSON array of domains to restrict the search to. When set, only results from these domains are used. |
| No | A JSON array of domains to exclude from the search. |
You can configure at most one web_search source per call.
The following example configures an AI Search index and a web search as knowledge sources:
[
{
"type": "vector_search",
"description": "Internal product catalog",
"config": {
"index_name": "prod_catalog.docs.product_catalog",
"text_col": "description",
"doc_uri_col": "product_url"
}
},
{
"type": "web_search",
"config": {
"allowed_domains": ["wikipedia.org"]
}
}
]
Returns
A VARIANT with the following schema:
{
"response": { ... }, // Generated columns matching the provided schema. Each leaf is returned as an object (see below).
"error_message": null, // null on success, or an error message on failure
"metadata": { ... } // Metadata about the response, including grounding sources.
}
The response field contains the generated columns:
- Field names and types match the schema definition. Nested objects and arrays keep their original shape.
- By default (
enableRationaleis'true'), each leaf is a{rationale, value}object, whererationaleis a short explanation of how the value was derived andvalueis the generated value, typed according to the schema. WhenenableRationaleis'false', each leaf is a{value}object. - A field's
valueisnullwhen it cannot be generated.
The metadata field contains metadata about the response. When the row is grounded by a knowledge source, metadata.sources is an array of the source document identifiers that grounded the row. Grounding is row-level, so sources applies to the whole row rather than to individual fields.
If content is NULL, the result is NULL.
Examples
Basic enrichment
The following example generates two columns for each company name using the model's own knowledge. Because rationale is on by default, each field is returned as a {rationale, value} object:
SELECT ai_enrich(
company_name,
'["industry", "headquarters_country"]'
) AS result
FROM sales.accounts.companies;
Structured schema with instructions
The following example defines a typed schema, adds instructions to steer the task, and disables rationale so each field returns a plain value:
SELECT ai_enrich(
review_text,
'{
"sentiment": {"type": "string", "description": "positive, negative, or neutral"},
"topics": {"type": "array", "items": {"type": "string"}},
"requires_follow_up": {"type": "boolean"}
}',
options => map(
'instructions', 'Analyze the customer review and categorize it for the support team.',
'enableRationale', 'false'
)
) AS result
FROM support.reviews.customer_reviews;
Generate a nested schema
The following example generates a nested schema for each company — a structured address object, an array of founder names, a typed year, and a nested funding round:
SELECT ai_enrich(
company_name,
'{
"hq_address": {
"type": "object",
"description": "Registered headquarters address",
"properties": {
"city": {"type": "string"},
"country": {"type": "string"}
}
},
"founding_team": {"type": "array", "description": "Full names of the founders", "items": {"type": "string"}},
"founding_year": {"type": "integer", "description": "Year the company was founded"},
"latest_funding_round": {
"type": "object",
"properties": {
"stage": {"type": "string", "description": "Funding stage, for example Seed or Series A"},
"amount_usd": {"type": "number", "description": "Amount raised in USD"}
}
}
}'
) AS result
FROM sales.accounts.companies;
Ground enrichment in an AI Search index
The following example enriches each support ticket with fields grounded in an AI Search index of product documentation, so the generated values are drawn from your own content:
SELECT
ticket_id,
ai_enrich(
customer_description,
'{
"affected_product": {"type": "string"},
"suggested_resolution": {"type": "string"},
"documentation_url": {"type": "string"}
}',
PARSE_JSON('[{
"type": "vector_search",
"description": "Product documentation and troubleshooting guides",
"config": {
"index_name": "support.docs.product_documentation",
"text_col": "content",
"doc_uri_col": "doc_url"
}
}]')
) AS result
FROM support.tickets.open_tickets;
Ground enrichment with web search
The following example enriches each company row with up-to-date information retrieved from the web:
SELECT ai_enrich(
company_name,
'["recent_funding_round", "latest_headline"]',
PARSE_JSON('[{
"type": "web_search",
"config": {"allowed_domains": ["reuters.com", "bloomberg.com"]}
}]'),
options => map('instructions', 'Find the most recent, verifiable information for each company.')
) AS result
FROM sales.accounts.companies;
Limitations
- Grounding with a
web_searchknowledge source is only available in some regions and workspaces. See web search on Databricks. - The
instructionsoption is limited to 20,000 characters.