dbt-semantic-interfaces
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
- breaking Upgrades to dbt-semantic-interfaces versions 0.8.x and 0.10.x have introduced breaking changes, particularly affecting adapter plugins and metadata interfaces within dbt-core and MetricFlow. Users integrating this library directly or maintaining custom adapters should review release notes carefully.
- gotcha dbt-semantic-interfaces provides the *definitions* for the dbt Semantic Layer. While `dbt-core` users can define metrics in their projects using these definitions, full features like dynamic querying via APIs or integrations often require a dbt Cloud plan.
- breaking The semantic definitions were originally integrated directly within `dbt-core` or `MetricFlow`. The introduction of `dbt-semantic-interfaces` as a separate, shared library means that older codebases might look for semantic objects in incorrect locations, leading to import errors or unexpected behavior.
- gotcha When using dbt Mesh with the Semantic Layer, cross-project references for semantic models are currently only supported in the *legacy* YAML specification (where semantic models are top-level resources). In the *latest* YAML specification, where semantic models are defined within model YAML files, cross-project references are not yet supported.
Install
-
pip install dbt-semantic-interfaces
Imports
- SemanticModel
from dbt_semantic_interfaces.models.semantic_model import SemanticModel
- Metric
from dbt_semantic_interfaces.models.metric import Metric
Quickstart
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]}")