Create configuration in code
Create a Config in Python when you need configuration to be explicit, self-contained, or overridden programmatically.
This is especially useful for tests and in situations where config files are impractical, such as Snowflake notebooks.
- You have access to a Snowflake account with the RelationalAI Native App installed. If you are unsure, contact your Snowflake administrator.
- You have working PyRel installation. See Set Up Your Environment for instructions.
Create a Config instance
Section titled “Create a Config instance”To create a Config instance, call create_config() with config sections as keyword arguments.
You can provide config sections as plain Python dictionaries or as typed config objects.
Dictionaries are the closest match to the shape of raiconfig.yaml.
Option 1: Use plain dictionaries
Section titled “Option 1: Use plain dictionaries”Use this style when you want the closest match to the shape of raiconfig.yaml:
import os
from relationalai.config import create_configfrom relationalai.semantics import Model
cfg = create_config( connections={ "sf": { "type": "snowflake", "authenticator": "username_password", "account": os.environ["SNOWFLAKE_ACCOUNT"], "warehouse": os.environ["SNOWFLAKE_WAREHOUSE"], "user": os.environ["SNOWFLAKE_USER"], "password": os.environ["SNOWFLAKE_PASSWORD"], }, }, default_connection="sf",)
# Use the configm = Model("MyModel", config=cfg)Option 2: Use typed connection objects
Section titled “Option 2: Use typed connection objects”Use this style when you want IDE type hints and constructor-style validation:
import os
from relationalai.config import UsernamePasswordAuth, create_configfrom relationalai.semantics import Model
cfg = create_config( connections={ "sf": UsernamePasswordAuth( account=os.environ["SNOWFLAKE_ACCOUNT"], warehouse=os.environ["SNOWFLAKE_WAREHOUSE"], user=os.environ["SNOWFLAKE_USER"], password=os.environ["SNOWFLAKE_PASSWORD"], ), }, default_connection="sf",)
# Use the configm = Model("MyModel", config=cfg)Override file-based configuration with programmatic config
Section titled “Override file-based configuration with programmatic config”You can keep a raiconfig.yaml for shared defaults and override only what changes at runtime.
Use this when you want consistent defaults but still need per-run tweaks (tests, CI, notebooks).
Programmatic overrides only affect the current Python process.
Select a profile at runtime
Section titled “Select a profile at runtime”Call create_config() with the active_profile argument to choose which profile overlay to apply for this process:
from relationalai.config import create_configfrom relationalai.semantics import Model
cfg = create_config(active_profile="prod")
# Verify the selected profilem = Model("MyModel", config=cfg)print(m.config.active_profile)Apply one-off runtime overrides without editing files
Section titled “Apply one-off runtime overrides without editing files”Pass explicit keyword arguments to create_config() to override file-based values for this process:
from relationalai.config import create_configfrom relationalai.semantics import Model
cfg = create_config( execution={ "metrics": True, })
# Verify the override took effectm = Model("MyModel", config=cfg)print(m.config.execution.metrics)Use fully programmatic config in tests
Section titled “Use fully programmatic config in tests”For tests, prefer passing connections=... (and any other settings you need) so your test suite does not depend on a developer machine’s config files.
This keeps tests hermetic and avoids surprises from a user’s local configuration.
If you want to test file discovery behavior, treat that as an integration test and set up the expected files explicitly.