EXECUTE CODE BUNDLE

statementpartialContainer services & dev tooling

docs.snowflake.com ↗

Note Notebook Project Objects have been renamed to Code Bundles. The NOTEBOOK PROJECT grammar is still supported; for that syntax, see EXECUTE NOTEBOOK PROJECT. For background on the rename, see the behavior change announcement. Runs a Code Bundle at the specified entrypoint, or runs source files from a stage or workspace without creating a Code Bundle first (an anonymous run). The command runs the code in a non-interactive (headless) mode and waits for the run to finish. It’s useful for CI/CD pipelines and other orchestrated workflows where you want to pass parameters or lock dependency versions for repeatable runs. You can run the command from: - SQL files and SQL worksheets. - Tasks and other Snowflake executables. - External orchestrators that issue SQL (for example, Airflow, Prefect, Dagster, or CI/CD systems). The compute type, runtime, dependencies, external access integrations, and secrets are defined in the code_bundle.yml specification. You can change the specification for a single run with the WITH SPECIFICATION clause, which is merged over the stored specification. Important Before triggering a non-interactive run of a notebook, ensure that your notebook sets its execution context (database and schema) or uses fully qualified object names. For more information, see Editing and running notebooks in Workspaces.

Syntax

supportedpartialnot supportednot scored

Run a Code Bundle

EXECUTE CODE BUNDLE <name>
  ENTRYPOINT = '<entrypoint>'
  [ ARGUMENTS = ( '<arg>' [ , '<arg>' ... ] ) ]
  [ EXECUTION_NAME = '<execution_name>' ]
  [ WITH SPECIFICATION $$ <yaml_spec> $$ ]
  [ INTO :<variable> ]

Run source files without creating a Code Bundle (anonymous run)

EXECUTE CODE BUNDLE FROM '<source>'
  ENTRYPOINT = '<entrypoint>'
  [ ARGUMENTS = ( '<arg>' [ , '<arg>' ... ] ) ]
  [ EXECUTION_NAME = '<execution_name>' ]
  WITH SPECIFICATION $$ <yaml_spec> $$
  [ INTO :<variable> ]

Parameters

NameRequiredDescription
namerequiredIdentifier of the Code Bundle to run. Mutually exclusive with FROM. Must reference an existing Code Bundle created with CREATE CODE BUNDLE. The run uses the Code Bundle’s default version. Must be fully qualified unless it resides in the current database and schema. For more information, see Identifier requirements.
FROM 'source'requiredRuns source files directly, without creating a Code Bundle. Mutually exclusive with name. An anonymous run requires WITH SPECIFICATION, and the inline specification is the whole configuration: a code_bundle.yml file in the source location is ignored. The source you can use depends on the bundle type, compute type, and language in the inline specification: Specification Source type: custom, compute_type: warehouse, language: python A stage directory that contains the entrypoint file, for example '@my_db.my_schema.my_stage/jobs'. A .jar file is rejected. type: custom, compute_type: warehouse, language: java or scala The stage path of the JAR file that contains the main class, for example '@my_db.my_schema.my_stage/app/app.jar'. A directory is rejected. Add other JAR files with properties.java_dependencies.jars; JAR files next to the main JAR aren’t added to the classpath. type: custom, compute_type: compute_pool A workspace path, for example 'snow://workspace/USER$.PUBLIC."my_workspace"/versions/live'. Stages aren’t supported. For a shared workspace, use a committed version; versions/live isn’t supported. type: spark For Scala or Java, the stage path of the .jar file. For Python, the stage directory that contains the entrypoint file, with no trailing slash. See Submit Spark jobs on Snowflake. type: ml Anonymous runs aren’t supported. Create a Code Bundle and execute it by name instead. See Run ML Jobs with Code Bundles.ExpandShow lessSee more
ENTRYPOINT = 'entrypoint'requiredSpecifies what Snowflake runs. The form depends on the language: Python script: the path of a .py file, relative to the bundle root (for example, main.py or jobs/main.py). The file runs as a script, as with python main.py. Python method: a .py file and the name of a function in it, separated by a colon (for example, main.py:run). Snowflake imports the file and calls the function, instead of running the file as a script, so code under if __name__ == "__main__": doesn’t run. The function’s return value becomes the run’s return value. Notebook: the path of a .ipynb file (for example, notebooks/train.ipynb). Notebooks run only on compute pools (Container Runtime). Java or Scala: the fully qualified name of a class that defines public static void main(String[] args) (Java) or def main(args: Array[String]): Unit (Scala), for example com.example.MyApp. Calling another method of the class isn’t supported. Spark: for Scala or Java, the fully qualified main class; for Python, the .py file name.
ARGUMENTS = ( 'arg' [ , 'arg' ... ] )optionalPasses one or more string arguments to the entrypoint at runtime. Arguments are useful for making logic dynamic (for example, selecting an environment such as --env prod). Specify each argument as a separate quoted string in the list: In a Python entrypoint, the arguments appear in sys.argv: sys.argv[0] is the entrypoint name, sys.argv[1] is the first argument, and so on. In a Java or Scala entrypoint, the arguments are passed to main as the String[] argument. Each value must be a string literal or a SQL variable reference such as :my_var. Function calls and other expressions aren’t supported. For an example of passing values computed at runtime, see Pass arguments computed at runtime. Only strings are supported; other data types (such as integers or Booleans) are interpreted as NULL. Don’t pass secret values in ARGUMENTS. They appear in query text and query history. Use secrets in the code_bundle.yml specification instead. The ARGUMENTS syntax differs between the two commands: EXECUTE CODE BUNDLE takes a parenthesized list of strings, while EXECUTE NOTEBOOK PROJECT takes a single string. Example:Copy code ARGUMENTS = ('--env', 'prod')
EXECUTION_NAME = 'execution_name'optionalA name for this run. Snowflake records it with the run, so you can find the run later by name, for example with EXECUTION_NAME => '<execution_name>' in CODE_BUNDLE_HISTORY. The name doesn’t have to be unique. If you omit it, the run has no execution name. Execution names are most useful for anonymous runs, which have no Code Bundle name to filter on.
WITH SPECIFICATION $$ yaml_spec $$optionalProvides a code_bundle.yml specification inline, for this run only. The stored Code Bundle isn’t changed. Running a Code Bundle by name: the inline specification is merged over the stored code_bundle.yml. Name only the fields you want to change; every other field keeps its stored value. For the merge rules, see How WITH SPECIFICATION is merged. Anonymous run (FROM): the inline specification is required and is the whole configuration. It must include type and compute_type, and every other field the run needs. The merged (or inline) specification is validated with the same rules as a code_bundle.yml file, so an invalid combination fails with the same error. For the fields, see the code_bundle.yml reference.
INTO :variableoptionalIn Snowflake Scripting, assigns the run’s RETURN_VALUE to a variable. Declare the variable as VARCHAR. See Capture the return value in a variable.

Usage notes

Output

Note Before the RETURN_VALUE behavior change, a successful run returned a single column named status or result that contained a success message, such as Statement executed successfully. If your code or tools read that column, update them. A successful run returns one row with one column: Column | Data type | Description RETURN_VALUE | VARCHAR | The value that the run returned, or NULL. For a notebook on a compute pool, the value passed to notebook.exit(value). For a Python method entrypoint (file.py:method), the value that the method returns. A Python str is returned as is; any other value is returned as JSON text, as produced by json.dumps() with its default settings (for example, {"rows_loaded": 42}, with non-ASCII characters escaped). Every other run returns NULL, including a Python script entrypoint and a Java or Scala entrypoint. RETURN_VALUE is also NULL in the following cases: - The statement returns before the run finishes, for example when the run is executed asynchronously. - The stages that Snowflake uses to cache notebook results in your account aren’t encrypted with SNOWFLAKE_FULL encryption. On a compute pool, including notebook runs, returning a value requires Container Runtime 3.0 or later. Container Runtime 2.x versions don’t support return values. A return value that can’t be encoded as JSON fails the run with a ReturnValueError. On a compute pool, including notebook runs, the encoded value can be at most 16 MiB; a larger value fails the run with a ReturnValueError. On a warehouse, the size limits for stored procedure return values apply. If the run fails, the command raises an error and returns no row. Decide whether a run succeeded from whether the statement raises an error, not from RETURN_VALUE. To review past runs, use the CODE_BUNDLE_HISTORY table function.

How WITH SPECIFICATION is merged

When you run a Code Bundle by name, the inline specification is applied as a patch over the stored code_bundle.yml (the same convention as an RFC 7386 JSON merge patch): fields you name override the stored value, and fields you don’t name are inherited. How a field is merged depends on its shape: Field shape | Examples | Behavior Scalar | runtime_version, compute_type, requirements_file | The inline value replaces the stored value. Object or map | compute_options, properties, stage_mounts, spark_conf | Merged key by key: keys you name override the stored keys, and other stored keys are kept. Empty object ({}) | Any object field | Changes nothing; the stored object is kept. List | secrets, external_access_integrations, artifact_repositories, env_vars, dependency lists | The inline list replaces the whole stored list. To add one item, restate the full list. null | Any field | Removes the field for this run. env_vars is a list, so naming one environment variable inline drops the other stored variables for that run. Restate every variable that the run needs. For example, the stored code_bundle.yml contains: The following statement changes only the Python version: The specification used for the run is:

Access control requirements

The role that runs the command must have the following privileges: - To run a Code Bundle by name: OWNERSHIP or USAGE on the Code Bundle, and USAGE on the database and schema that contain it. - For an anonymous run: read access to the FROM source, such as READ on an internal stage or access to the workspace. - For compute_type: warehouse: USAGE on the current warehouse of the session. The run uses that warehouse. - For compute_type: compute_pool: USAGE or OWNERSHIP on the compute pool, and USAGE on the query warehouse. - For objects referenced in the specification: READ or OWNERSHIP on each secret. USAGE on a secret isn’t sufficient. - USAGE or OWNERSHIP on each external access integration and artifact repository. - Read access to the stages in stage_mounts and to the stages that hold properties.java_dependencies.jars. For instructions on creating a custom role with a specified set of privileges, see Creating custom roles. For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.

Known gaps

Parts not fully supported, and recorded divergences from Snowflake.

See also

Referenced by