FactLang
FactLang is a small formal language for verifying factual claims against data warehouses.
You write a claim such as "the orders table has at least 1,000 rows" or "every
order_items.order_id exists in orders.id". FactLang checks it statically, compiles it to
read-only SQL for your dialect, runs it over all the data, and returns a verdict
(pass, fail, no_data, error, …) with the numbers and the SQL behind it.
Why: numbers in LLM answers, schema registries, data contracts and migration plans are claims about data, and claims drift. FactLang makes them checkable, line by line, with evidence a human can audit. The core is pure standard-library Python with no dependencies.
Status: Alpha (0.1.1). Python 3.10–3.13. Language version 1.0.
Documentation ·
Wiki ·
Changelog
Thirty seconds
pip install "factlang[duckdb]"
import duckdb
import factlang as fl
conn = duckdb.connect()
conn.execute("""
CREATE TABLE orders AS
SELECT i AS id,
CASE WHEN i % 10 = 0 THEN 'refunded' ELSE 'paid' END AS status
FROM range(1, 1001) AS t(i)
""")
claims = """
ROWS orders VALUE >=1000
UNIQUE orders.id
VALUES orders.status IN ['paid', 'open']
"""
report = fl.Checker(fl.DBAPIConnector(conn, "duckdb")).check(claims)
for o in report.outcomes:
print(f"{o.verdict.value:5} {o.claim_text:42} {o.explanation}")
print("verified:", report.verified)
assert [o.verdict.value for o in report.outcomes] == ["pass", "pass", "fail"]
assert not report.verified
pass ROWS orders VALUE >=1000 row_count 1000 >= 1000
pass UNIQUE orders.id max_rows_per_key 1 <= 1
fail VALUES orders.status IN ['paid', 'open'] undeclared_rows 100 != 0
verified: False
report.verified is the flag to gate on: every measurable claim was measured and passed.
Each outcome also carries facts (every number the verb measured) and evidence (the SQL,
its bind parameters, the dialect and its tier). The
getting-started guide goes further.
Install
pip install factlang # the core: no dependencies at all
pip install "factlang[postgres]" # plus a driver for your warehouse
| extra | adds | for |
|---|---|---|
duckdb |
duckdb |
DuckDB |
postgres |
psycopg[binary] |
PostgreSQL |
mysql |
pymysql |
MySQL |
clickhouse |
clickhouse-connect |
ClickHouse |
mssql |
pyodbc |
SQL Server (tsql) |
trino |
trino |
Trino |
bigquery |
google-cloud-bigquery |
BigQuery |
snowflake |
snowflake-connector-python |
Snowflake |
redshift |
redshift-connector |
Redshift |
databricks |
databricks-sql-connector |
Databricks |
oracle |
oracledb |
Oracle |
sqlalchemy |
sqlalchemy |
any SQLAlchemy URL (--dsn, FACTLANG_DSN) |
sqlglot |
sqlglot |
the long tail of dialects (tier best_effort) |
mcp |
mcp |
the MCP server (factlang mcp) |
all |
every one of the above |
Any PEP 249 connection works through fl.DBAPIConnector(conn, "<dialect>"); the extras only
install drivers.
The standard verbs
Nineteen verbs, one claim per line. The verb reference lists every attribute.
| verb | example | passes when |
|---|---|---|
EXISTS |
EXISTS orders COLUMN shipped_at |
the table (or column) exists in the catalog |
TYPE |
TYPE orders.amount IS decimal |
the column's type family equals the stated one |
ROWS |
ROWS orders VALUE >=1000 |
the row count satisfies the comparison |
COUNT |
COUNT orders VALUE ==1400 WHERE status = 'paid' |
the (filtered) row count satisfies the comparison |
NULLS |
NULLS orders.shipped_at RATE <=5% |
the NULL share satisfies the comparison |
DEAD |
DEAD orders.legacy_flag |
every value is NULL |
UNIQUE |
UNIQUE order_items.order_id WITH [line_no] |
no (composite) key is held by more than one row |
FANOUT |
FANOUT order_items.order_id MAX_PER_KEY <=50 |
the busiest key's row count satisfies the comparison |
VALUES |
VALUES orders.status IN ['paid', 'open'] |
every non-NULL value is declared |
RANGE |
RANGE orders.amount BETWEEN [0, 10000] |
min and max lie inside the closed range |
PATTERN |
PATTERN customers.email LIKE '%@%.%' RATE >=99% |
the share matching the pattern satisfies the comparison |
FRESH |
FRESH events.created_at WITHIN 24h |
the newest value is no older than the duration |
JOIN |
JOIN orders.customer_id -> customers.id CARD N:1 MATCH >=99% |
the share of left keys that find a right key satisfies the comparison |
DISTINCT |
DISTINCT orders.customer_id VALUE ==512 |
the distinct non-NULL count satisfies the comparison |
SUM |
SUM orders.amount VALUE ==209000 TOLERANCE 1% |
the sum satisfies the comparison |
AVG |
AVG orders.amount VALUE ==104.5 ABS_TOLERANCE 0.01 |
the average satisfies the comparison |
MIN |
MIN orders.amount VALUE >=0 |
the smallest value satisfies the comparison |
MAX |
MAX orders.placed_on VALUE <=DATE '2026-12-31' |
the largest value satisfies the comparison |
NOTE |
NOTE orders.amount IS eur BECAUSE 'all prices are stored in euro' |
never measured: a recorded semantic fact |
Every verb accepts a WHERE filter where it makes sense. You can add, override, alias or
remove verbs in your own pack.
Dialects
Eleven dialects are rendered natively by the pure-Python core; every outcome reports the tier of the SQL that produced it.
| tier | dialects | what it means |
|---|---|---|
verified |
duckdb, postgres, mysql, clickhouse, tsql, trino |
the native SQL runs against a real engine in CI, verb by verb |
native |
bigquery, snowflake, redshift, databricks, oracle |
native SQL checked by parsing it with sqlglot in that dialect and by golden tests; an opt-in live suite runs it when credentials are present |
best_effort |
any other dialect sqlglot can write (presto, athena, hive, teradata, …) |
rendered through the optional sqlglot bridge (pip install "factlang[sqlglot]") |
Command line
factlang lint facts.fl # diagnostics only; never executes
factlang fmt --check facts.fl # canonical formatting, for CI
factlang explain --dialect bigquery facts.fl # print each claim's SQL
export FACTLANG_DSN="postgresql+psycopg://reader@warehouse/shop"
factlang check facts.fl # run the claims (needs factlang[sqlalchemy])
factlang check --format json facts.fl # also: --format markdown
factlang verbs # list the standard verbs
| exit code | meaning |
|---|---|
0 |
every claim passed (or is unprobeable) |
1 |
a claim failed; for fmt --check, a file would be reformatted |
2 |
invalid input: bad usage, an unreadable file, a claim with front-end errors, a connector that cannot be loaded |
3 |
errors or undecidable: an error or no_data outcome, a claim that cannot be rendered |
When several apply, 2 wins over 1, and 1 over 3. See the
CLI guide.
Agents and MCP
FactLang ships tool definitions for LLM agents (factlang_check, factlang_lint,
factlang_explain) and serves the same tools over MCP on stdio:
pip install "factlang[mcp,sqlalchemy]"
export FACTLANG_DSN="postgresql+psycopg://reader@warehouse/shop"
factlang mcp # register this as a stdio command in your MCP client
factlang mcp --dialect snowflake # no warehouse: lint and explain only
Have the model emit claims alongside its answer and gate on verified. See
Agents and LLMs.
Guarantees
- No sampling in claims. Every verdict is computed over every row that matches the
claim's filter. Sampling exists only in
Checker.measure, the raw-measurement API, when you ask for it, and its evidence says so (details). - Read-only SQL, typed parameters. Probes are
SELECT-only, claims cannot contain raw SQL, and every literal travels as a typed bind parameter. Use a database role that can only read: your credentials are the security boundary. - Undecidable stays undecidable. A fact the data cannot support is
no_data, never a pass; a query the warehouse rejects iserror. - Stable API. The names in
fl.__all__and the stable API table follow SemVer: no breaking change within a0.xminor series.
Project
- Documentation and the in-browser playground
- Wiki: concepts, architecture, design decisions
- Changelog
- Contributing
- Security policy
- License: MIT
Metadata
Release files for factlang 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| factlang-0.1.1.tar.gz | 405.3 kB | With provenance | Details for factlang-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| factlang-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for factlang-0.1.1-py3-none-any.whl |
Total release size: 609.2 kB
Release files / factlang-0.1.1.tar.gz
| Download URL | factlang-0.1.1.tar.gz |
|---|---|
| Size | 405.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
04b0b01bcb9b5fffde945b80c1a2baaadb380d63235ac915f60f38617508e24b
|
|
BLAKE2b-256 checksum How to use checksums |
7aaab79538a059643813e27dac851ec7094355515ae1520ab72c41ac1a9bbf7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.
Transparency logRelease files / factlang-0.1.1-py3-none-any.whl
| Download URL | factlang-0.1.1-py3-none-any.whl |
|---|---|
| Size | 203.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
729334a2e6482060a842f12307fb9261ec7df03fe53afe0801172652d9664527
|
|
BLAKE2b-256 checksum How to use checksums |
dce49a38a56c53eaf3d4ff7722a4c1c5b8bf39f718aac886ec1639c97a85ff5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.
Transparency logSecurity for factlang 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for factlang-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.