Skip to main content

SQL Experiments in Python

Project description

SQLSpec

PyPI Python License Docs

SQLSpec is a SQL execution layer for Python. You write the SQL -- as strings, through a builder API, or loaded from files -- and SQLSpec handles connections, parameter binding, SQL injection prevention, dialect translation, and mapping results back to typed Python objects. It uses sqlglot under the hood to parse, validate, and optimize your queries before they hit the database.

It works with PostgreSQL (asyncpg, psycopg, psqlpy), SQLite (sqlite3, aiosqlite), DuckDB, MySQL (asyncmy, mysql-connector, pymysql), Oracle (oracledb), CockroachDB, BigQuery, Spanner, and anything ADBC-compatible. Sync or async, same API. It also includes a built-in storage layer, native and bridged Arrow support for all drivers, and integrations for Litestar, FastAPI, Flask, Sanic, and Starlette.

Quick Start

pip install sqlspec
from pydantic import BaseModel
from sqlspec import SQLSpec
from sqlspec.adapters.sqlite import SqliteConfig

class Greeting(BaseModel):
    message: str

spec = SQLSpec()
db = spec.add_config(SqliteConfig(connection_config={"database": ":memory:"}))

with spec.provide_session(db) as session:
    greeting = session.select_one(
        "SELECT 'Hello, SQLSpec!' AS message",
        schema_type=Greeting,
    )
    print(greeting.message)  # Output: Hello, SQLSpec!

Write SQL, define a schema, get typed objects back. Or use the query builder -- they're interchangeable:

from sqlspec import sql

# Builder API -- same driver, same result mapping
users = session.select(
    sql.select("id", "name", "email")
       .from_("users")
       .where("active = :active")
       .order_by("name")
       .limit(10),
    {"active": True},
    schema_type=User,
)

Features

  • Connection pooling -- sync and async adapters with a unified API across all supported drivers
  • Parameter binding and dialect translation -- powered by sqlglot, with a fluent query builder and .sql file loader
  • Result mapping -- map rows to Pydantic, msgspec, attrs, or dataclass models, or export to Arrow tables for pandas and Polars
  • Storage layer -- read and write Arrow tables to local files, fsspec, or object stores
  • Framework integrations -- Litestar plugin with DI, Starlette/FastAPI/Sanic middleware, Flask extension
  • Observability -- OpenTelemetry and Prometheus instrumentation, structured logging with correlation IDs
  • Event channels -- LISTEN/NOTIFY, Oracle AQ, and a portable polling fallback
  • Migrations -- schema versioning CLI built on Alembic

Documentation

Playground

Want to try it without installing anything? The interactive playground runs SQLSpec in your browser with a sandboxed Python runtime.

Reference Applications

  • PostgreSQL + Vertex AI Demo -- Vector search with pgvector and real-time chat using Litestar and Google ADK. Shows connection pooling, migrations, type-safe result mapping, vector embeddings, and response caching.
  • Oracle + Vertex AI Demo -- Oracle 23ai vector search with semantic similarity using HNSW indexes. Demonstrates NumPy array conversion, large object handling, and real-time performance metrics.

Contributing

Contributions are welcome -- whether that's bug reports, new adapter ideas, or pull requests. Take a look at the contributor guide to get started.

License

MIT

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

sqlspec-0.46.2.tar.gz (2.1 MB view details)

Uploaded Source

Built Distributions

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

sqlspec-0.46.2-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

sqlspec-0.46.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

sqlspec-0.46.2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

sqlspec-0.46.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

sqlspec-0.46.2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

sqlspec-0.46.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

sqlspec-0.46.2-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

sqlspec-0.46.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

sqlspec-0.46.2-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

sqlspec-0.46.2-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

sqlspec-0.46.2-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

File details

Details for the file sqlspec-0.46.2.tar.gz.

File metadata

  • Download URL: sqlspec-0.46.2.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlspec-0.46.2.tar.gz
Algorithm Hash digest
SHA256 bcdf8de7731fb24ee6ca02a66bbe3e0e55651027b129e75c02e22514d74c9282
MD5 05474c9a3a1029fff12c0b1a4142b57f
BLAKE2b-256 3e5e1e6aa5030a4e5f2d6414d6975fe09c81ea97dd90e8b2347274b664cb0e4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2.tar.gz:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-py3-none-any.whl.

File metadata

  • Download URL: sqlspec-0.46.2-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlspec-0.46.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c31e6401b722021db82528c1ee54ddcfa5acffd953afffac1308063f89553270
MD5 96e00cf4f76c3ce0ef4d8bbee2384053
BLAKE2b-256 d079bec5e8a0c12b413967409b43fe778a008a3ccd3153e74ebff40e819187e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-py3-none-any.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 475ad21c61cd814b763c69f3d4955231e03102ad91463f6b1fc007d433d48c54
MD5 9a30001d6b54cc124f56257020c3c63c
BLAKE2b-256 ab713dfa8a9e6e8e9f43ab1f6a0c58264587abffaf431a4a1b7d9d1117888605

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 276d9debe7235503fbf21d6241301c87bdc6e9d1b74ddd95000e8c6ed4644361
MD5 2cb1e50230ac96aa47a9cd2fa4853ced
BLAKE2b-256 66faf17bf9b300bb7bfcd90749dc0e1b80b3b4ea3651c48b24684001b05aa0f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d00f4a975806767cea914fd6cfdd19161f48c5f2f18864e2bf9232f67b1eceec
MD5 af6982b263b8af6f439ff60eedfc881d
BLAKE2b-256 2583e6a719c636df0b9342920f398b8b0614be5cc84fa330af2084dd5229c995

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 dac4a54b764f3245af859c1369de68726d16fb9d3a0bd01105eed0b757e7d652
MD5 a41c865a3f76f20fdb021e3f7fbaa244
BLAKE2b-256 b3f63db98dd355769ee78b44d3c157274a4760aaaed00de462aae3231fb5f693

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bb317a2e05f2a814d63712cb45ef2e872ae53144a86a493e420b3640f36a47fb
MD5 268271a4e35cf7600eef32a6b3c40599
BLAKE2b-256 3d02fa21b60571702f79058f617af1224275762fa073d2cf2f8e2c884fe39901

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3b9a828eb5b670dcb1b70b057470f214b0b76b9bc09286596c752132fda9de4f
MD5 7e4bd08a98b4969fec9c147d97c34992
BLAKE2b-256 e9eec056b89fb24fb882cf71d07d0729aa5a57386b69175d81665f7e5bb5bcaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ed2a660cc0bc83702fcbefc70665bc54a5caf4946c12a072e76da649112ff3e7
MD5 7b16ba80942421c101d605bd8038075e
BLAKE2b-256 d69b00d2b24dd8242a4371559971cbb5a13db8932ed1212d0359677b086fdc72

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 66d2dc65e30b3ea21c7d3cf2411798d4c112e0116bf7736547807f0890313bdf
MD5 7e5d33ffd7df7c12619c879b0581d5a3
BLAKE2b-256 4dceeefa8d01f08b1dcfaee4f63ccafcd5b9d5c92f2b304680273e6807bf34d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c964cb875960028bd912a7668c950fd806bd2243cb6e535a18eee79253f9fd64
MD5 50c604e5d672a93b8622fe168aa2a273
BLAKE2b-256 296b4953c11efecd7b2fec63e2be26afbedd2079c5b96d5e52f3df1957f2eb67

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlspec-0.46.2-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.46.2-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3cd73b4ca29d1469c4dd43fdf3321709fa8a7d3996b88e18f6b4f9fac252225d
MD5 9d205feeebd3114e7abf8860b7e7b79b
BLAKE2b-256 d622fbefe7503896b7bd49ff4a4acc819159cb3befa209408b14f8f34ad93cfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.2-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on litestar-org/sqlspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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