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, 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 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


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.41.0.tar.gz (2.0 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.41.0-py3-none-any.whl (1.0 MB view details)

Uploaded Python 3

sqlspec-0.41.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.5 MB view details)

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

sqlspec-0.41.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.7 MB view details)

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

sqlspec-0.41.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.4 MB view details)

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

sqlspec-0.41.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.8 MB view details)

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

sqlspec-0.41.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.4 MB view details)

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

sqlspec-0.41.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.7 MB view details)

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

sqlspec-0.41.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.4 MB view details)

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

sqlspec-0.41.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.7 MB view details)

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

sqlspec-0.41.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.4 MB view details)

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

sqlspec-0.41.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.7 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.41.0.tar.gz.

File metadata

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

File hashes

Hashes for sqlspec-0.41.0.tar.gz
Algorithm Hash digest
SHA256 36b583376c0728a79a4d17cb68d71bc29e41a991d165f51f9210c002fb0ad432
MD5 7c7a4af7fba208741c9dc5962633130c
BLAKE2b-256 576f54c8471cbb9e2e0cedb0fefb44f415ecd046c43cbbb0c460d3000bdc3e6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0.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.41.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sqlspec-0.41.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df390c4083d9bc67a6529c335a2ac5bd9f8723395f2cba5408d99acdf9365d7e
MD5 80f96f0937558d167d0ce1273bbac2d6
BLAKE2b-256 9541d58e2250a4385aae4cee657d323a22aa31e6609a26ea62e1fa28b36a3f82

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a3885f5c99df043c2f7c3fb12b4e353beeff1fb29e5cff36827cd0703d48feb1
MD5 514822796337d4bc431671189d4f5495
BLAKE2b-256 4fc738b0e81f1bc3fcacd90b28c35e4b8c57ce0cc55a252a4aba9a13f8bf07ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e944a0185345df89a795dd909c3c2b118a875fe30eb44a442ce9e4fb57b7e8ba
MD5 8131548448aebf1cb071a1644178dcb0
BLAKE2b-256 f671762813b5c46624ea58b19b982c0d5c1f4f89481b3178d4d47a7f9493dd46

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 15d40f4475bd0644ed79991ab88c2b2ba22b8ab5075d391029204c59ef324867
MD5 4d2416e83afdfc43eeee30c3296e287d
BLAKE2b-256 6e8753e30d9909d6aa683d4aa2ffa4caf38a69d49a252950ebe857a81d5a99ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e0fc762f4bab30f1cf73508303985f2c27fad57cf11e57e88783adb530a64ea3
MD5 9be34a2be9c2b4f36a50c444d9a0d40a
BLAKE2b-256 f840bbae947b2f7399b8cf89ee3aff89b37639e8c9d7cc6fd7fb0b7010e28794

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f6dd0405c6e3f5e282a45cbbdd76f93d46218361fdbc1b2c383ef03e95a7e8bb
MD5 ed8cb9540403b64fbc832923c1973f94
BLAKE2b-256 258848f626910dded4611cd277b8daa74ee50e720ca4521d8a12707e2815184e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 dd19bae065caea9055f8e3de57266f6a7758911d2dd808fa89953e7cbc951e5d
MD5 17ff0e39e30891fa118bda166ebaa834
BLAKE2b-256 00284f8e580b13675129c6ebcdce21f1ea738ad0373eb1bfca6f4cc848505745

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6c45b820a2137b6af0b0eaf583e10e8116ba3de93905709d928ca9559b196f3d
MD5 1d96b7eb1d72e336db105f57666a0b24
BLAKE2b-256 c8d04dff7050d7c481829f21d28e236041c88ea79f2d641afe75415d6e2db4d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 347fda95e27357dd419d87f7b55a526b020ca4ad00fcc28270a6cd8a17f0114b
MD5 4c8c186b8d33be631fa2a6b65ad04a71
BLAKE2b-256 e4ada051f6873d18f50ea7e379e664eb0bdb10f12de9c17b1d958436e129e58b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e3f8b27782d3dfca1b6a180673de2598a4885581c2da415c456943738271e392
MD5 3a251fcac29d24bee222f673f418c2b3
BLAKE2b-256 00ba61a1f967ed60cbfb1c9af6ed20ae53b5966201b5433a44cf0c6062abd252

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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.41.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for sqlspec-0.41.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 8883ebaec0c79fcd48cf19f085a2ec6887f28f832a417d7ce7efd6b3b0452588
MD5 8c98b41d872bd573ae7ad1c0af5ea2af
BLAKE2b-256 692d652e6576590694f4e1922646df72d3f9f596f2ef35c3f74c6d65bb1a2c3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.41.0-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