CREATE AI FUNCTION

statementnot supportedAI & ML

docs.snowflake.com ↗

Object: AI FUNCTION

CREATE AI FUNCTION lets you turn custom AI logic into a reusable, governed Snowflake function that can be used across applications, workflows, and SQL queries. Use CREATE AI FUNCTION when you want to package AI-powered logic behind a stable SQL interface and manage it consistently with the rest of your Snowflake environment. You define the function using Snowflake Cortex AI functions such as AI_COMPLETE, AI_CLASSIFY, or AI_FILTER, then invoke it like any other scalar function. Because it is a first-class Snowflake object, you can manage it using familiar Snowflake capabilities for access control, object lifecycle, discovery, and governance. AI functions can also participate in Snowflake’s evaluation and optimization workflows. This makes it possible to measure quality and cost, improve prompts or model choices, and promote an optimized implementation into a production-ready function without changing how downstream users call it. This command supports the following variants: - CREATE OR ALTER AI FUNCTION: Creates an AI function if it doesn’t exist or alters an existing AI function. - DEFINE AI FUNCTION: Declarative definition of an AI function’s target state inside a DCM project.

Syntax

supportedpartialnot supportednot scored

Create an AI function from a SQL expression

CREATE [ OR REPLACE ] AI FUNCTION [ IF NOT EXISTS ] <name> (
    [ <arg_name> <arg_data_type> [ , ... ] ] )
  RETURNS <result_data_type>
  [ COMMENT = '<string_literal>' ]
  AS $$
    <sql_expression_calling_an_ai_function>
  $$

Create an AI function from the winning candidate of an optimization experiment

CREATE [ OR REPLACE ] AI FUNCTION [ IF NOT EXISTS ] <name> (
    [ <arg_name> <arg_data_type> [ , ... ] ] )
  RETURNS <result_data_type>
  FROM EXPERIMENT <experiment_name> RUN <run_name>

For more information, see CREATE OR ALTER AI FUNCTION usage notes and CREATE OR ALTER <object>.

CREATE OR ALTER AI FUNCTION <name> (
    [ <arg_name> <arg_data_type> [ , ... ] ] )
  RETURNS <result_data_type>
  [ COMMENT = '<string_literal>' ]
  AS $$
    <sql_expression_calling_an_ai_function>
  $$

Parameters

NameRequiredDescription
namerequiredThe identifier for the AI function. Can be fully qualified (db.schema.name). The argument data types are part of the function’s signature, so MY_FN(VARCHAR) and MY_FN(VARCHAR, VARCHAR) are distinct functions (standard UDF overloading rules).
( arg_name arg_data_type [ , ... ] )requiredZero or more input arguments, each an identifier plus a SQL data type (VARCHAR, FLOAT, BOOLEAN, OBJECT, FILE, and so on). The argument list may be empty: MY_FN().
RETURNS result_data_typerequiredThe scalar SQL type the function returns (for example VARCHAR, FLOAT, BOOLEAN, OBJECT). A TABLE(...) return type is not supported — an AI function’s body is a single scalar expression.
AS $$ sql_expression $$requiredA single scalar SQL expression that calls AI_COMPLETE at least once. It may also call other AI functions (for example AI_CLASSIFY, AI_FILTER) or a nested user-defined AI function, but at least one AI_COMPLETE call is required. The expression can freely reference the function’s arguments and can post-process the AI call (for example concatenate a prompt, cast the result to the return type).
FROM EXPERIMENT experiment_name RUN run_namerequiredInstead of an inline body, materialize the function from a specific run of an existing optimization experiment (typically the frontier / winning ITER_<N> run). Snowflake copies that run’s tuned implementation into the new function. The declared signature and return type must match the optimized function.
OR REPLACErequiredReplace an existing function with the same name and signature. Cannot be combined with IF NOT EXISTS.
IF NOT EXISTSrequiredDo nothing (no error) if a function with the same name and signature already exists.
COMMENT = 'string_literal'requiredAn optional description stored with the function (the WITH keyword is optional: WITH COMMENT = '...' is equivalent).

Usage notes

CREATE OR ALTER AI FUNCTION usage notes

- All limitations of the ALTER FUNCTION command apply.

DEFINE AI FUNCTION usage notes

- All limitations of a CREATE OR ALTER AI FUNCTION statement apply. - The FROM EXPERIMENT clause isn’t supported.

Access control requirements

Privilege | Object | Notes CREATE FUNCTION | Schema | Same privilege as a regular UDF; there is no separate “CREATE AI FUNCTION” privilege. Database role SNOWFLAKE.CORTEX_USER | — | Required to run the AI calls in the body at invocation time. USAGE | Function’s database + schema | To call the function. OWNERSHIP | Function | Required only when executing a CREATE OR ALTER AI FUNCTION statement for an existing AI function.

Limitations

- Scalar SQL only. Procedural (Snowflake Scripting) bodies and non-SQL languages (Python/Java/Scala handlers) are not supported for AI functions. - Model availability. The models named in the body must be available and authorized in your account and region; otherwise calls fail at invocation time.

Legal

The data classification of inputs and outputs are as set forth in the following table. Input data classification | Output data classification | Designation Usage Data | Customer Data | Generally available functions are Covered AI Features. Preview functions are Preview AI Features. [1] [1] Represents the defined term used in the AI Terms and Acceptable Use Policy. For additional information, refer to Snowflake AI and ML.