Skip to main content

Secure Natural Language Database Interface

Project description

CogniDB 3.0

Secure natural-language SQL for applications — a Python library, not a chat product.

CogniDB turns natural language into SELECT-oriented SQL against PostgreSQL, MySQL, and SQLite. Security is the product: sanitize → generate → validate → allowlists → execute → audit.

Status: Source release 3.0.1 (GitHub Release). Prefer pip install -e . from a clone until cognidb==3.0.1 appears on PyPI.

Default: read mode (SELECT only) → validate → optional table/column allowlists → execute → audit.
Opt-in: write mode (INSERT/UPDATE/DELETE), schema linking, one repair attempt, intent mode (generation_mode="intent").

Install

# From source (recommended until PyPI 3.0.1 is published)
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"

# Optional extras (see pyproject.toml)
# pip install -e ".[redis]"   # Redis cache
# pip install -e ".[mongo]"   # Mongo driver experiments
pytest -q

Offline benchmarks

# Full suite (no network / no API keys)
python -m benchmarks.run --track all

# CI smoke subset (also covered by tests/benchmarks/)
python -m benchmarks.run --track all --smoke --fail-under 1.0

See benchmarks/README.md for metrics, case formats, and what is not claimed (no competitor SoTA without comparative data).

Quick start (offline SQLite, under 30 lines)

No network LLM — uses FakeSQLGenerator with canned SQL:

from cognidb.drivers import SQLiteDriver
from cognidb.pipeline import SecureQueryPipeline
from cognidb.security import QuerySecurityValidator, InputSanitizer, StatementPolicy, StatementMode
from cognidb.ai.fake_generator import FakeSQLGenerator

drv = SQLiteDriver({"database": ":memory:"})
drv.connect()
drv.execute_native_query("CREATE TABLE users (id INTEGER, name TEXT)")
drv.execute_native_query("INSERT INTO users VALUES (1, 'Ada')")

pipe = SecureQueryPipeline(
    driver=drv,
    generator=FakeSQLGenerator("SELECT id, name FROM users"),
    validator=QuerySecurityValidator(),
    sanitizer=InputSanitizer(),
    schema=drv.fetch_schema(),
    policy=StatementPolicy(mode=StatementMode.READ),
    enable_audit=False,
)
print(pipe.run("list users").to_dict())
drv.disconnect()

Same script: python examples/sqlite_offline_demo.py.

High-level client (needs DB + LLM credentials)

from cognidb import CogniDB, create_cognidb

# Env: DB_* and LLM_API_KEY, or pass config / cognidb.yaml
with CogniDB(config_file="cognidb.yaml") as db:
    result = db.query("Show the top 10 customers by revenue", explain=True)
    if result["success"]:
        print(result["sql"], result["results"])

examples/basic_usage.py demonstrates the high-level API but requires a live database and LLM API key — it is not offline.

What ships in 3.0

Capability Notes
Dialects SQLite, PostgreSQL, MySQL
SecureQueryPipeline Single NL→SQL→execute path
Statement policy Read default; write opt-in; DDL always rejected
Allowlists Table + column access control when enabled
Intent mode Opt-in structured QueryIntent → deterministic SQL render
Offline testing FakeSQLGenerator / FakeIntentGenerator
Benchmarks Offline multi-track suite under benchmarks/ (correctness, security, policy, robustness)
LLM providers OpenAI, Anthropic, Azure OpenAI; HuggingFace/local need extra deps
Docs SECURITY.md, docs/threat-model.md, CHANGELOG.md, ROADMAP.md

Security (brief)

  • Parameterized execution via drivers; validator + sanitizer on generated SQL
  • Optional AccessController for table/column allowlists (fail-closed on SELECT * when columns are restricted)
  • Audit logging hooks; adversarial corpus under tests/security/corpus/
from cognidb.security import AccessController

access = AccessController()
access.create_restricted_user(
    user_id="analyst_1",
    table_permissions_dict={
        "customers": {
            "operations": ["SELECT"],
            "columns": ["id", "name", "email", "country"],
            "row_filter": "country = 'USA'",
        }
    },
)
# Pass access_controller into SecureQueryPipeline with enable_access_control=True
# or configure via CogniDB settings / yaml.

Configuration

Environment variables (DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASSWORD, LLM_PROVIDER, LLM_API_KEY, …) or YAML — see cognidb.example.yaml.

Development

git clone https://github.com/boxed-dev/cognidb
cd cognidb
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest

Contributing: CONTRIBUTING.md. Conduct: CODE_OF_CONDUCT.md.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognidb-3.0.1.tar.gz (56.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cognidb-3.0.1-py3-none-any.whl (68.7 kB view details)

Uploaded Python 3

File details

Details for the file cognidb-3.0.1.tar.gz.

File metadata

  • Download URL: cognidb-3.0.1.tar.gz
  • Upload date:
  • Size: 56.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cognidb-3.0.1.tar.gz
Algorithm Hash digest
SHA256 f47168860e39ca48824d8086d0f4a8f5e38d4f0416d76f9eede2da9f069fb86a
MD5 0c1302dadafc9e0aece553874e7a90bf
BLAKE2b-256 f5ad69da47dfead03d0bddef0f11bb1af486da6642c93df2e065d1a1407d8fd3

See more details on using hashes here.

File details

Details for the file cognidb-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: cognidb-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 68.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cognidb-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dbd54079afbe00e345ee9a119a3f2ada924ab3cdf8635f89e9f06d973218dc2e
MD5 c8f37d317a52e151f219fdac5f960802
BLAKE2b-256 7508594389393f5dc3b2041917935c0b6cdbc081dae8fb30040199ab41a62a88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page