Skip to Content
Python SDK

Python SDK

A code-first semantic layer for ClickHouse in Python. Define datasets once, query them by name, and serve them from FastAPI, Django or Flask.

Define a ClickHouse dataset once in Python (dimensions, measures, a tenant key), then query it by name instead of rewriting the same GROUP BY in every endpoint. Requests are validated before any SQL exists, the SQL is parameterized, and every query is scoped to the caller's tenant. The same definition serves authenticated HTTP endpoints, returns pandas, polars or Arrow, and feeds React hooks and MCP agents.

Beta

The Python SDK is in beta (0.1.0b4). Its APIs may still change before 1.0, so pin the version you deploy.

Install

pip install "hypequery[clickhouse]==0.1.0b4"

Python 3.11 or later. Add extras as you need them: server for the dev runner, jwt for token auth, django or flask for native serving, pandas, polars or arrow for DataFrames, clickhouse-async for the async executor. No ClickHouse server to hand? Install hypequery[chdb] and run the same datasets on embedded ClickHouse (chDB), with no server and no credentials.

Define a dataset and query it

from hypequery.datasets import (
    DatasetQuery,
    ExecutionContext,
    create_dataset_client,
    dataset,
    dimension,
    filter,
    measure,
    order,
    tenant,
)
from hypequery.execution import ClickHouseConnection, create_clickhouse_executor

orders = dataset(
    "orders",
    source="orders",
    tenant_key="tenant_id",
    time_key="created_at",
    dimensions={
        "country": dimension.string(),
        "status": dimension.string(),
        "createdAt": dimension.timestamp(column="created_at"),
    },
    measures={
        "revenue": measure.sum("amount"),
        "orderCount": measure.count("id"),
    },
)

analytics = create_dataset_client(
    executor=create_clickhouse_executor(ClickHouseConnection.from_env()),
)

result = analytics.execute(
    orders,
    DatasetQuery(
        dimensions=("country",),
        measures=("revenue", "orderCount"),
        filters=(filter.eq("status", "paid"),),
        order_by=(order.desc("revenue"),),
        limit=10,
    ),
    context=ExecutionContext(tenant=tenant("org_123")),
)
print(result.data)

ClickHouseConnection.from_env() reads CLICKHOUSE_HOST, CLICKHOUSE_PORT, CLICKHOUSE_DATABASE, CLICKHOUSE_USERNAME, CLICKHOUSE_PASSWORD and CLICKHOUSE_SECURE (set it to true for TLS, as ClickHouse Cloud needs), or a single CLICKHOUSE_URL. Already have tables? hypequery init introspects your schema and writes the dataset definitions for you.

Serve it over HTTP

The API is plain ASGI, so it mounts in FastAPI, Starlette or any ASGI app, and is served natively from Django and Flask. Every endpoint authenticates before reading the body and takes the tenant from the caller.

pip install "hypequery[clickhouse,jwt]==0.1.0b4" fastapi uvicorn
from fastapi import FastAPI
from hypequery.serve import HttpSecurity, create_api, jwt_strategy, to_asgi_app

api = create_api(
    auth=jwt_strategy(
        jwks_url="https://example.auth0.com/.well-known/jwks.json",
        issuer="https://example.auth0.com/",
        audience="https://api.example.com",
    ),
    client=analytics,
    datasets={"orders": orders},
    security=HttpSecurity(allowed_hosts=("api.example.com",)),
)

app = FastAPI()
app.mount("/analytics", to_asgi_app(api))

POST /analytics/datasets/orders/query now runs the same validated, tenant-scoped query as the Python call above. The same endpoints feed generated, type-safe React hooks and a governed MCP server that gives agents named metrics instead of raw SQL. See React and MCP clients.

Where next

  • Quick start: the full walkthrough, from generated datasets to calculated measures.
  • Python CLI: guided setup from your ClickHouse schema, with a standalone, FastAPI, Django or Flask app.
  • FastAPI, Django and Flask: a tenant-bound client in every view, and hypequery's endpoints served from your app.
  • Serve over HTTP: auth strategies, tenancy, CORS, rate limits and OpenAPI.
  • pandas, Polars and Arrow: results as typed DataFrames.
  • ClickHouse Python clients: clickhouse-connect, clickhouse-driver and clickhouse-sqlalchemy compared, and where hypequery fits next to them.
  • chDB Python tutorial: embedded ClickHouse in Python, from chdb.query() to MergeTree tables on disk.
  • Feature status: what Python has today compared with TypeScript.
  • Why hypequery and Core concepts: the ideas behind datasets, serving and clients, in both languages.

Package layout

ModulePurpose
hypequery.datasetsDimensions, measures, relationships, the dataset client, caching, and the catalog
hypequery.executionSync and async ClickHouse executors
hypequery.serveAn authenticated-by-default HTTP API with ASGI and WSGI adapters, JWT auth, CORS and rate limiting
hypequery.contribDjango, Flask and SQLAlchemy integrations
hypequery CLIhypequery init, generate datasets, dev and export client-contract
hypequery.protocolThe language-neutral artifact contracts shared with TypeScript

Python and TypeScript implement the same specifications and run against the same conformance fixtures, so equivalent definitions produce identical semantic and deployment artifacts. The APIs are idiomatic to each language rather than line-for-line ports; the Python SDK does not include the TypeScript query builder.

On this page

Get early access to hypequery cloud.

Cloud hosts the same model you run in your backend, with a managed API and MCP server, versions, and rollback. We're letting teams in a few at a time. Join the waitlist and we'll reach out when your project can go live.

How do you want to build your analytics layer? (Optional)
What features do you most need? (Optional)

Pick as many as you like.

Only your email is required. All other questions are optional.