Snowflake Gen AI Functions · Topic 21 of 26

Building Chat Interfaces: Environment Setup, Privileges and Streamlit in Snowflake

Set up a Snowflake account for Cortex chat apps: required roles and privileges, region and model access, and calling Cortex from Streamlit in Snowflake.

  • 4 min
In a nutshell
  • SNOWFLAKE.CORTEX_USER is the base database role and is granted to PUBLIC by default.
  • CORTEX_EMBED_USER, CORTEX_ANALYST_USER and CORTEX_REST_API_USER are narrower roles.
  • Query a Cortex Search service: USAGE on service, database and schema.

What it is

A chat interface lets people ask questions of their data in natural language. You can use a ready made interface such as Snowflake Intelligence, or build your own app, for example a Streamlit in Snowflake app that calls Cortex.

Before either works, the account, the users' roles and the objects they touch must be set up. Most exam questions here are really about which privilege is missing.

Account setupregion, modelsgrantRolesdatabase roles, USAGEused byApp codeStreamlit, RESTcallsCortexAI_COMPLETE, Search, Analyst

How it works

Account level setup

  • Region and model availability. Cortex features are available in selected regions. If a model is not in your region, cross region inference lets a request run in another region. It is controlled by the account parameter CORTEX_ENABLED_CROSS_REGION (only ACCOUNTADMIN sets it), and it has data movement implications you should review.
  • Model access. An administrator can limit which models roles can call with model RBAC (application roles in SNOWFLAKE.MODELS), or with the legacy CORTEX_MODELS_ALLOWLIST account parameter, which is being retired.
  • Warehouse. SQL calls and Cortex Analyst generated SQL run on a warehouse. For AI functions the docs recommend MEDIUM or smaller, because a bigger warehouse does not make the model faster.
  • Source objects. Create the tables or stages, the Cortex Search service and the semantic view the app needs.

Required privileges

To do thisYou need
Call AI functions such as AI_COMPLETE, AI_EXTRACTThe account level USE AI FUNCTIONS privilege (granted to PUBLIC by default) plus the SNOWFLAKE.CORTEX_USER or SNOWFLAKE.AI_FUNCTIONS_USER database role
Call AI_EMBED or create a Cortex Search serviceCORTEX_USER or CORTEX_EMBED_USER
Call the Cortex REST APICORTEX_USER or CORTEX_REST_API_USER on the user's default role
Use Cortex AnalystCORTEX_USER or CORTEX_ANALYST_USER, SELECT on referenced tables, READ on the stage for YAML models
Query a Cortex Search serviceUSAGE on the service, its database and its schema
Create a Cortex Search serviceCREATE CORTEX SEARCH SERVICE on the schema, SELECT on the source, USAGE on the warehouse, change tracking on the source
Use files with TO_FILEREAD on the stage (for an internal stage) and a supported stage type

Key facts:

  • CORTEX_USER is granted to PUBLIC by default, so everyone can use the models unless you revoke it.
  • CORTEX_REST_API_USER is not granted to PUBLIC, and it cannot be granted directly to a user, only to a role.
  • To tighten access: revoke CORTEX_USER from PUBLIC and grant a narrower role to chosen roles.
USE ROLE ACCOUNTADMIN;
REVOKE DATABASE ROLE SNOWFLAKE.CORTEX_USER FROM ROLE PUBLIC;
GRANT DATABASE ROLE SNOWFLAKE.CORTEX_USER TO ROLE chat_app_role;
GRANT USAGE ON CORTEX SEARCH SERVICE kb_db.public.kb_search TO ROLE chat_app_role;
Exam tip

"A user can call the REST API only through a role" and "the default role matters" are classic traps. REST checks the default role.

Streamlit in Snowflake

A Streamlit in Snowflake app runs inside Snowflake, so it uses the Snowpark session of the app and the data never leaves the account. App code can call Cortex in two ways.

  1. SQL through the session: run AI_COMPLETE or a Cortex Search query as SQL.
  2. Python API: the snowflake-ml-python package provides a Python interface to the Cortex functions. Add the package to the app environment.
import streamlit as st
from snowflake.snowpark.context import get_active_session

session = get_active_session()
question = st.chat_input("Ask a question")
if question:
    answer = session.sql(
        "SELECT AI_COMPLETE('claude-sonnet-4-5', ?)", params=[question]
    ).collect()[0][0]
    st.chat_message("assistant").write(answer)

For Cortex Analyst the app calls the REST endpoint with the question and a semantic view. For search it queries the service and passes the top chunks to AI_COMPLETE.

The app's access follows the role that runs it. If the app role lacks USAGE on the search service or SELECT on the tables, the chat fails even though the code is correct.

Common mistakes

Granting CORTEX_REST_API_USER to a user. It can be granted only to roles.

Setting it on a non default role for REST calls. The default role is the one checked.

Forgetting USAGE on the database and schema of a Cortex Search service.

Assuming a larger warehouse fixes slow chat. The model is the bottleneck, not the warehouse.

Watch out

Revoking CORTEX_USER from PUBLIC without granting it to the app role is a common reason a working app suddenly returns a permission error.

What to remember for the exam

  • SNOWFLAKE.CORTEX_USER is the base database role and is granted to PUBLIC by default.
  • CORTEX_EMBED_USER, CORTEX_ANALYST_USER and CORTEX_REST_API_USER are narrower roles.
  • Query a Cortex Search service: USAGE on service, database and schema.
  • Cross region inference is an account parameter, CORTEX_ENABLED_CROSS_REGION.
  • Streamlit in Snowflake calls Cortex through the Snowpark session or the Python package.

Check yourself

  • Which role is checked for a Cortex REST API call?

    The user's default role, which needs CORTEX_USER or CORTEX_REST_API_USER.

  • What does a role need to query a Cortex Search service?

    USAGE on the service and on its database and schema.

  • Who can use Cortex models by default?

    Everyone, because CORTEX_USER is granted to PUBLIC unless revoked.

Topic 21 of 26 in Snowflake Gen AI Functions

Test yourself on Snowflake Gen AI Functions

The full Snowflake GES-C02 question bank has 585 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Snowflake. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.