Skip to content

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.

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.

  1. Import Model from relationalai.semantics:

    from relationalai.semantics import Model
  2. Instantiate the model:

    from relationalai.semantics import Model
    m = Model("MyModel")
  3. Reuse the model across declarations:

    from relationalai.semantics import Model
    m = Model("MyModel")
    Customer = m.Concept("Customer")

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.

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.

  1. Create a minimal raiconfig.yaml in your project root:

    raiconfig.yaml
    default_connection: snowflake
    connections:
    snowflake:
    type: snowflake
    authenticator: username_password
    account: "my_account"
    user: "my_user"
    warehouse: "my_warehouse"
    password: ${SNOWFLAKE_PASSWORD}
  2. Instantiate Model and confirm what it loaded:

    from relationalai.semantics import Model
    m = 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_connection explicitly so the selection is unambiguous.

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.

  1. Create a config in code with create_config():

    import os
    from relationalai.config import create_config
    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"],
    },
    },
    )
  2. Instantiate Model with config=cfg and confirm what it will use:

    from relationalai.semantics import Model
    m = Model("MyModel", config=cfg)
    # View details for the default connection
    print(m.config.default_connection)

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.strict enables 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_config
from 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": True enables strict mode.
  • "soft_type_errors": True enables 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.