Skip to main content

GroundedQL

GroundedQL - Reliable natural-language analytics through deterministic SQL compilation

Intent-driven, deterministic natural language to SQL for Postgres.
Instead of letting an LLM generate free-form SQL, the LLM extracts a lightweight QueryIntent, and GroundedQL deterministically compiles it into parameterized SQL and executes it safely.

Install

pip install groundedql

To use the OpenAI SDK adapter shown below:

pip install "groundedql[openai]"

With optional few-shot memory (recommended for production):

pip install "groundedql[memory]"
Install from source
git clone https://github.com/Certifore/groundedql
cd groundedql
pip install -e ".[dev]"

Quick Start

1. Generate your schema from the database

groundedql init --db "postgresql://user:pass@host/db"
# → config/schema.yaml  (tables, columns, types, PKs, links — all auto-detected)

2. Enrich with LLM-generated descriptions (optional, recommended)

export LLM_API_KEY=sk-...   # works with any OpenAI-compatible provider
groundedql describe --schema config/schema.yaml --db "postgresql://user:pass@host/db"
# → Adds table + column descriptions using sample data for context

3. Ask questions

from sqlalchemy import create_engine
from openai import OpenAI
from groundedql.agent import QueryAgent

engine = create_engine("postgresql+psycopg2://user:pass@host/db")

agent = QueryAgent(
    engine=engine,
    schema_path="config/schema.yaml",
    llm=OpenAI(api_key="sk-..."),
)

result = agent.ask("how many plumbing issues last year?")
print(result["rows"])
print(result["sql"])

Use Mistral instead of OpenAI

export MISTRAL_AI=...
# optional
export MISTRAL_MODEL=mistral-small-latest
agent = QueryAgent(
    engine=engine,
    schema_path="config/schema.yaml",
    llm="mistral",  # or "mistral:mistral-small-latest"
)

Use a local Ollama model

export OLLAMA_MODEL=groundedql-gemma4
# optional
export OLLAMA_BASE_URL=http://127.0.0.1:11434
export OLLAMA_NUM_CTX=8192
agent = QueryAgent(
    engine=engine,
    schema_path="config/schema.yaml",
    llm="ollama",  # or "ollama:groundedql-gemma4"
)

CLI Reference

Command Description
groundedql init --db URL Introspect Postgres and generate schema.yaml
groundedql describe --schema PATH --db URL Enrich schema with LLM-generated descriptions

Run groundedql --help for full options.

Documentation

Full documentation, benchmarks, and guides are at certifore.github.io/groundedql_docs (source).

See ROADMAP.md for planned capabilities (lookup, trends, ratios, multi-step NL, and expressiveness goals).

Existing users of the previous package name should read MIGRATION.md.

Contributing

Contributions are welcome through pull requests. Please read CONTRIBUTING.md before submitting changes. The lead maintainer reviews and merges all changes into the official repository and is the only person who publishes official releases.

License

GroundedQL is licensed under the Apache License 2.0.

Metadata

Release files for groundedql 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for groundedql 0.3.0
File Size Uploaded
groundedql-0.3.0.tar.gz 147.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for groundedql 0.3.0
File Interpreter ABI Platform
groundedql-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 302.0 kB

Release files / groundedql-0.3.0.tar.gz

Download URL groundedql-0.3.0.tar.gz
Size 147.5 kB
Tags Source
SHA-256 checksum
How to use checksums
31a18a25e40caed28db2b4cd40b790176b45796f820d248c4aac1f6417fea16f
BLAKE2b-256 checksum
How to use checksums
846c83be03d82a2a36e8acc01cb84a98329327de64147f60fa6443ed384b990f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / groundedql-0.3.0-py3-none-any.whl

Download URL groundedql-0.3.0-py3-none-any.whl
Size 154.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd6f92cd90e393934a443e4e00c1ab84cce13de8e54d9e9b690b71c786cabfcd
BLAKE2b-256 checksum
How to use checksums
3648914a5531becc49593c686efc1c459a788343f5850a330de6911aeb3dc613
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page