dbt-semantic-interfaces

0.10.5 · active · verified Sun Mar 29

dbt-semantic-interfaces is a Python library that centralizes the shared semantic layer definitions used by dbt-core and MetricFlow. Its primary purpose is to maintain consistency and reduce code duplication across these projects by providing common semantic classes, default validation, and tests. The current version is 0.10.5, with frequent updates indicated by its release history and ongoing development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically define a basic `SemanticModel` object using the Pydantic models provided by `dbt-semantic-interfaces`. This showcases the library's core function of providing standardized definitions for semantic layer components, which are then typically used by dbt-core and MetricFlow for validation and processing. Note that in a full dbt project, semantic models are usually defined in YAML files and not directly instantiated in Python by end-users. This example is for understanding the library's internal structure.

from dbt_semantic_interfaces.models.semantic_model import SemanticModel
from dbt_semantic_interfaces.models.entities import PydanticEntity
from dbt_semantic_interfaces.models.measures import PydanticMeasure
from dbt_semantic_interfaces.type_enums import EntityType, TimeGranularity

# A minimal example of defining a semantic model programmatically
my_entity = PydanticEntity(
    name='customer_id',
    type=EntityType.PRIMARY,
    expr='id'
)

my_measure = PydanticMeasure(
    name='total_orders',
    agg='count',
    expr='order_id'
)

my_semantic_model = SemanticModel(
    name='customers_semantic_model',
    description='A semantic model for customer data.',
    node_relation=None, # This would typically link to a dbt model, omitted for brevity
    entities=[my_entity],
    measures=[my_measure],
    dimensions=[]
)

print(f"Created Semantic Model: {my_semantic_model.name}")
print(f"Entities: {[e.name for e in my_semantic_model.entities]}")
print(f"Measures: {[m.name for m in my_semantic_model.measures]}")

view raw JSON →