Create a model instance
Create a Model object to declare concepts, add definitions, and run queries against a shared semantic model.
Creating a model is the starting point for semantic modeling in PyRel.
You will typically keep one instance and reuse it across a workflow.
- PyRel is installed and importable in Python. See Set Up Your Environment for instructions.
- You have a
raiconfig.yamlfile in your project, or you are prepared to pass config programmatically when you create the model.
Create a Model instance
Section titled “Create a Model instance”A Model instance is the entry point for semantic modeling in PyRel.
Keep one model object as the shared handle for concept declarations, definitions, and queries in the same workflow.
If you do not pass an explicit config, Model() loads config from the filesystem using discovery rules.
-
Import
Modelfromrelationalai.semantics:from relationalai.semantics import Model -
Instantiate the model:
from relationalai.semantics import Modelm = Model("MyModel") -
Reuse the model across declarations:
from relationalai.semantics import Modelm = Model("MyModel")Customer = m.Concept("Customer")
Configure a model’s connection
Section titled “Configure a model’s connection”A model uses configuration to choose which connection it will use when it runs jobs.
By default, Model() discovers configuration from files on disk (for example raiconfig.yaml) based on your current working directory.
If you need repeatable behavior (for example, tests, CI, or notebooks), pass configuration explicitly when you create the model.
Use a configuration file
Section titled “Use a configuration file”Choose this when you want a repo-friendly default and you do not need runtime overrides.
This workflow lets Model() discover configuration automatically from your project.
-
Create a minimal
raiconfig.yamlin your project root:raiconfig.yaml default_connection: snowflakeconnections:snowflake:type: snowflakeauthenticator: username_passwordaccount: "my_account"user: "my_user"warehouse: "my_warehouse"password: ${SNOWFLAKE_PASSWORD} -
Instantiate
Modeland confirm what it loaded:from relationalai.semantics import Modelm = Model("MyModel")print(m.config.default_connection)print(type(m.config.get_default_connection()))
- If you run the same code from a different directory, discovery can find a different
raiconfig.yaml. - If you define multiple connections, set
default_connectionexplicitly so the selection is unambiguous.
Use explicit programmatic configuration
Section titled “Use explicit programmatic configuration”Choose this when you need runtime isolation or explicit overrides (for example, tests, CI, or notebooks). This workflow bypasses file discovery and uses only the config you build in Python.
-
Create a config in code with
create_config():import osfrom relationalai.config import create_configcfg = 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"],},},) -
Instantiate
Modelwithconfig=cfgand confirm what it will use:from relationalai.semantics import Modelm = Model("MyModel", config=cfg)# View details for the default connectionprint(m.config.default_connection)
create_config(...)creates a validated config instance.Model("MyModel", config=cfg)bypasses filesystem discovery and uses your explicit config for execution.- When you define multiple connections, always set
default_connectionsom.config.get_default_connectionis well-defined. See Use profiles to manage multiple configurations for details.
Configure compiler strictness for type errors
Section titled “Configure compiler strictness for type errors”Compiler settings control whether type issues stop compilation or show up as warnings while translating your model and queries.
Strict mode is a good fit for tests and CI.
Soft error mode is a good fit for early iteration.
Both settings default to false.
compiler.strictenables stricter validation during compilation.compiler.soft_type_errors(soft error mode) treats type errors as warnings instead of failures.
Set these options when you create a config for your model.
Although you can set them in a raiconfig.yaml file, setting them in code is more explicit and easier to change at runtime:
from relationalai.config import create_configfrom relationalai.semantics import Model
cfg = create_config( compiler={ "strict": True, "soft_type_errors": True, },)
m = Model("MyModel", config=cfg)
print(m.config.compiler.strict)print(m.config.compiler.soft_type_errors)create_config(...)creates a validated config instance with compiler overrides."strict": Trueenables strict mode."soft_type_errors": Trueenables soft error mode.- Printing
m.config.compiler.*confirms what the model will use.
- If you pass a config without connection details, the model still needs a connection from a discoverable config source, or from an active session.
- Strict mode can surface new compilation errors. Plan to fix missing types and other schema issues when you enable it.