Snowflake ML Python Library
The Snowflake ML Python Library (snowflake-ml-python) is the official client for interacting with Snowflake to build and deploy machine learning solutions. It provides interfaces for machine learning lifecycle management including session management, model development, model registry, feature store, and experiment tracking. The library is actively maintained with frequent minor releases, typically on a monthly to bi-monthly cadence, and is currently at version 1.34.0.
Warnings
- gotcha Incorrect permissions can prevent model inference. Previously, `model_version.run()` required `READ` privilege on the model instead of `USAGE`, leading to failures. While fixed in 1.27.0, ensure your Snowflake role has appropriate `USAGE` grants on the model and other necessary objects.
- gotcha Handling case-sensitive or Unicode identifiers (e.g., Japanese column names) can lead to errors due to incorrect quoting in generated SQL. This particularly affected `generate_dataset()` and `generate_training_set()` for Feature Store operations.
- gotcha Using `ParamSpec` (inference parameters) with table functions or partitioned model methods has historically had issues, leading to runtime failures or incorrect behavior. While fixes have been released, understanding the nuances of `ParamSpec` with various model types (custom, sklearn, PyTorch) and deployment configurations is crucial.
Install
-
pip install snowflake-ml-python
Imports
- Session
from snowflake.snowpark import Session
- get_session
from snowflake.ml.session import get_session
- Model
from snowflake.ml.model import Model
- Registry
from snowflake.ml.registry import Registry
Quickstart
import os
from snowflake.snowpark import Session
from snowflake.ml.session import get_session
# Ensure environment variables are set for connection:
# SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, SNOWFLAKE_PASSWORD, SNOWFLAKE_ROLE,
# SNOWFLAKE_WAREHOUSE, SNOWFLAKE_DATABASE, SNOWFLAKE_SCHEMA
connection_parameters = {
"account": os.environ.get("SNOWFLAKE_ACCOUNT", "your_account"),
"user": os.environ.get("SNOWFLAKE_USER", "your_user"),
"password": os.environ.get("SNOWFLAKE_PASSWORD", "your_password"),
"role": os.environ.get("SNOWFLAKE_ROLE", "your_role"),
"warehouse": os.environ.get("SNOWFLAKE_WAREHOUSE", "your_warehouse"),
"database": os.environ.get("SNOWFLAKE_DATABASE", "your_database"),
"schema": os.environ.get("SNOWFLAKE_SCHEMA", "your_schema"),
}
session = None
try:
# Establish a Snowpark Session
session = Session.builder.configs(connection_parameters).create()
print("Snowpark Session created successfully.")
print(f"Current database: {session.get_current_database()}, schema: {session.get_current_schema()}")
# Obtain the Snowflake ML Session (uses the Snowpark session)
ml_session = get_session(session)
print("Snowflake ML Session obtained successfully.")
# You can now use ml_session for various ML tasks, e.g.,
# ml_session.model.deploy(...)
# ml_session.feature_store.register_feature_view(...)
except Exception as e:
print(f"An error occurred: {e}")
finally:
if session:
session.close()
print("Snowpark Session closed.")