dbt-metricflow
dbt-metricflow is a Python package that bundles dbt-core, MetricFlow, and supported dbt adapters to provide a semantic layer for defining and managing metrics within a dbt project. It compiles metric definitions into reusable SQL, ensuring consistent analysis across data. Part of the Open Semantic Interchange (OSI) initiative, it simplifies metric governance and query generation. The current version is 0.11.0, with a release cadence tied to the underlying dbt-core and MetricFlow libraries, with the bundle aiming to ensure compatibility between them.
Common errors
-
ERROR: Could not find a version that satisfies the requirement dbt-core==X.Y.Z (from dbt-metricflow)
cause This error typically occurs when there's an incompatibility between the requested `dbt-core` version (implied by `dbt-metricflow`) and other installed packages or your Python environment.fixEnsure you are using a Python version compatible with `dbt-metricflow` (currently `<3.13, >=3.9`). Try installing `dbt-metricflow` in a clean virtual environment: `python -m venv venv && source venv/bin/activate && pip install "dbt-metricflow[your_adapter]"`. -
Skipped column 'my_column' because it does not exist in the data connection's schema or type information is missing.
cause This issue arises when semantic projects reference columns that are not present in the target data warehouse's schema, or if the schema in the consuming tool (e.g., Hex) is out of date.fixVerify that all tables and columns referenced in your dbt semantic project exist in your data warehouse. Refresh the schema of your data connection in the consuming tool. As a workaround, you can add column type annotations directly in your dimension definitions in YAML. -
Command 'dbt sl list metrics' not found or failed.
cause This indicates that either `dbt-metricflow` is not correctly installed, or the `dbt` executable is not in your system's PATH, or there's an issue with the dbt project setup. For dbt platform users, the `dbt sl` commands are embedded. For dbt Core users, `dbt-metricflow` must be installed.fixFirst, ensure `dbt-metricflow` is installed: `pip install "dbt-metricflow[your_adapter]"`. Confirm your dbt project is initialized and has semantic models defined. If running locally, check that your virtual environment is activated and `dbt` is accessible.
Warnings
- breaking The `dbt-metricflow` package encapsulates `dbt-core`, `MetricFlow`, and `dbt-adapters` to manage version compatibility. Directly installing individual `dbt-core` or `MetricFlow` packages might lead to version conflicts due to dependencies on `dbt-semantic-interfaces`. Always prefer installing `dbt-metricflow[adapter]` to ensure a compatible bundle.
- gotcha MetricFlow (the underlying library) does not currently support dbt built-in functions or packages within semantic models. This can limit the complexity of logic directly expressible in metric definitions.
- deprecated Older versions of MetricFlow CLI users were required to install a separate `metricflow` package. As of MetricFlow 0.206.0, CLI updates are picked up by installing `dbt-metricflow`.
- gotcha When integrating with tools like Hex, enabling the dbt semantic layer integration on the data connection can break prepared statements required for no-code filtering.
Install
-
pip install "dbt-metricflow[adapter_package_name]" -
pip install dbt-metricflow
Imports
- MetricFlow CLI commands
Interaction is primarily via the dbt CLI using 'dbt sl <command>' or 'mf <command>'. Direct Python API imports for end-user semantic layer querying are not the primary interaction model.
Quickstart
import subprocess
import os
# This assumes a dbt project with semantic models and metrics defined is set up.
# And dbt-metricflow (with an adapter) is installed in the environment.
# Example: List available metrics in your dbt project
print("Listing dbt metrics...")
result = subprocess.run(['dbt', 'sl', 'list', 'metrics'], capture_output=True, text=True, check=False)
print(result.stdout)
if result.stderr:
print(f"Error: {result.stderr}")
# Example: Validate your semantic layer configurations
print("Running dbt semantic layer health checks...")
result = subprocess.run(['dbt', 'sl', 'health-check'], capture_output=True, text=True, check=False)
print(result.stdout)
if result.stderr:
print(f"Error: {result.stderr}")