Skip to content

Expression

relationalai.semantics.frontend.base
Expression(
op: Relationship | Concept,
args: Sequence[Value],
kwargs: dict | None = None,
root: Chain | Relationship | Concept | None = None,
)

Represents an expression in the DSL.

Expression objects are produced when you call a Relationship, Property, Chain, or Concept (and by operators like == and +). You can pass them to Model.define, Model.where, and Model.select.

Because expressions are composable, you can build complex nested expressions that combine multiple relationships, properties, and operators.

Parameters

  • op

    (Relationship | Concept) - The operation being applied.
  • args

    (Sequence[Value]) - Positional arguments. Python primitives are wrapped as Literal.
  • kwargs

    (dict, default: None) - Keyword field values for the call.
  • root

    (Chain | Relationship | Concept, default: None) - Root used for chained expressions.

Examples

Build a query by comparing a relationship’s output to a literal value:

from relationalai.semantics import Model, String
m = Model()
Person = m.Concept("Person")
Person.pets = m.Relationship(f"{Person} has pet {String:pet_name}")
m.define(alice := Person.new(name="Alice"), alice.pets("Boots"))
# The expression alice.pets == "Boots" produces an Expression
# that we can use in a where clause to filter to people whose pet is Boots
m.where(alice.pets == "Boots").select(alice).to_df()

Notes

Most users should not instantiate this class directly.

Methods

.__getattr__()

Expression.__getattr__(item: str) -> Chain

Follow a relationship/property from this expression.

Attribute access returns a Chain rooted at this expression. This is mainly useful when a relationship call leaves its output “open” (by providing fewer than the full number of arguments), and you then want to access another relationship/property from that output.

Names that start with an underscore are treated as normal Python attribute access.

Parameters:

  • item

    (str) - Relationship/property name to access next.

Returns:

  • Chain - A chained value representing the extended relationship path.

Raises:

  • relationalai.util.error.RAIException - If the relationship/property cannot be resolved (for example when the model.implicit_properties config flag is disabled).

Examples:

Access a property on a relationship’s output field via chaining from an expression:

from relationalai.semantics import Model, String
m = Model()
Person = m.Concept("Person")
Company = m.Concept("Company")
works_at = m.Relationship(f"{Person} works at {Company}")
Company.name = m.Property(f"{Company} is named {String:name}")
alice = Person.new(name="Alice")
# Follow the relationship output (Company) and then read its name
m.select(works_at(alice).name).to_df()

.__getitem__()

Expression.__getitem__(field: str | int | Concept) -> FieldRef

Return a reference to one of this expression’s relationship fields.

Indexing an Expression returns a FieldRef for the underlying relationship.

Parameters:

  • field

    (str | int | Concept) - Field selector: a 0-based index, an exact field name, or a field type (concept).

Returns:

  • FieldRef - A reference to the selected field.

Raises:

  • TypeError - If this expression is not a relationship call.
  • IndexError - If field is an integer index that is out of range.
  • KeyError - If no field matches the provided selector.

Inheritance Hierarchy

ExpressionVariableDSLBase

Subclassed By

Used By

Returned By