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.1.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.1-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

sqlspec-0.46.1-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.1-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.1-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.1-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.1-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.1-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.1-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.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: sqlspec-0.46.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a036f4413c6ba6e38bb8f3f774c406b0f6f3b3d8938210bd255efbc9142a90da
MD5 c84cf0340fd006e19d657917a6596150
BLAKE2b-256 33fb0226f08dc2d83802cf8e658b044d69fbbf09508dc956a6b05ea7ca3714c7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: sqlspec-0.46.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 63616eabb26cf952e98ea5ed2be57e766cde966bdee99104e9e6fd90d8a7ab80
MD5 2f0f0a6203cbea2b19785066016102da
BLAKE2b-256 3c9743e2d1e20db0f615bcb484834e5a0e5a4f33e7804d742f9b37b685f6c519

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 55df3801f224807d8c7c8bdcc291fc2c5692bee245f6e158d87616019e8d017b
MD5 d9f5bb9c2b602507044e64b68b7b9793
BLAKE2b-256 54b8a531d389c4bd867db45f2cdf5ac9ace731b8b88bcd52b4a600b3e3a1198f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.1-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.1-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.1-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e71c5737ce60b3675139ac919d164ab70220de29686bf4acd3cb0aeeaf9cb222
MD5 240758740952d5b38847bf0d635a2664
BLAKE2b-256 6811ad51d2d754ae4d0fc13a76f2f701c1ba0e01736c0d46113beb824cbd047c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 29c93fec2039cd556caa3e74160b4c71cab152af76df49d47b619741e8990a62
MD5 ab08e7cbe35cf2e177d31023e45a433a
BLAKE2b-256 bbab45a8051573d2037aaec5a383859de24f5183656ecc5bfa0fd2b98bc081e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.1-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.1-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.1-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0d0341ff1546b17fbc0f8a280b6c1bfb835304f4cae06e3c4fc2742f801d304d
MD5 a3f0b90aa4d5cb0ae2d3704b39743045
BLAKE2b-256 301f3386793a53456e4ac63b745832074d614ed506a79dcb5ac29793c4c23eee

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0451ce58e7a9d16694ee3fce33e18df4b45333e5e68b62b89df8b83a69bb8d1d
MD5 ff830b79a3f0a46bfb960c768d4dde40
BLAKE2b-256 452a13b075eee7c0662b5cc922f3411d3d72db927696f4fdc0f6b09d3bd3e97d

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.1-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.1-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.1-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 926d95527c5e2d23f9810d8494e94cfff99724944b8f206db6d372066d19816f
MD5 64865c17faf430d253772ae5054b0fa6
BLAKE2b-256 3d53f944aa6a10bbec3de644e4b372abefc6744492ed84f662458d7a20b3bc9b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 69e2e0f53ae230de3f7f78d5c8143554104cae4c02e4c85dd7967ee896714828
MD5 8bea5b098f8c047f7264d3aecd9fe410
BLAKE2b-256 e5d9ffb5d2d547b7ba107e49c2033ba1b9a80ba35bc3d1d6b7eb93c8208c59f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.1-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.1-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.1-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 197c6f020adac7c87dfd0022c4fa75f96e84bb147c741aa97afdcb621c37e9ed
MD5 36566a901087912c79c354ce4c6b7e47
BLAKE2b-256 a202af517297b46eb5c242d8f8797f6c8799639ccc0a9afae3d331cc9eb8f5f9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 86b7643d8f6945f41c6fc7eaa72993b602482b40d115f351071e6443f0340ebe
MD5 1ff5138ca1146a89abd73a0a3185a73b
BLAKE2b-256 d5eb26d54ac1320bedcb9d387379cd52fe2d8f23b54cc6cf5a2bdbbc4d4ee2af

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.1-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.1-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.1-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 254d885ff529d19ac2de953e1848573d32a7f707cd42eaafeaa819d2c30801cf
MD5 c1635a750a66ec72ec8354c6985c5f07
BLAKE2b-256 be15e51919d042f2599513ebbc39f8d53c197b8cdced2bd0f3a2787cbd0c2acd

See more details on using hashes here.

Provenance

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