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

Uploaded Python 3

sqlspec-0.46.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.9 MB view details)

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

sqlspec-0.46.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3.tar.gz.

File metadata

  • Download URL: sqlspec-0.46.3.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.3.tar.gz
Algorithm Hash digest
SHA256 74a1e1c391b83035dc3c62f12a57cedabb4dfdcd3335647c5fbe0b69e6234e18
MD5 237c9fcff6d87b97ae6f47bfa6e91127
BLAKE2b-256 7f88c76573609910465142262082ac8f2b58732be597c2680a92731101ec2821

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: sqlspec-0.46.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0fea2579d6f018d8b9c0e406243bd3443bc6903959dbfc96f6a71c0255d03525
MD5 5cb2987e3137f73bb98cb783c34dedb9
BLAKE2b-256 31a9318b3d883af5b7eee229876a70b31b190e13c3e0d4111c44ffbce94748bd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 031709b008efaa4430b579041a98a1fe52ffcb799a6eebf5cda52601e6338e31
MD5 8ca5abcf473dfcfd34818bdcfa8c2008
BLAKE2b-256 c274ec77edff9ad2b382fb98a65241d3039ad4c00046ea321f0613dbff9f0f58

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.3-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.3-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.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0984af3f6fdc7364bc2a09eb0fde4999ed34850f6eebc67c18d9ad806770b89f
MD5 fab882ccd35fff860e940e4603fbfd00
BLAKE2b-256 e3ae2b1b98c4b616a34e9fb5839d732c6267d2b2e2fbd21d4659d81d518871d7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a7229239d3899e569695801fc9ae22cdac1685b2f16715f5300aca3d67f6be4a
MD5 291e9d19c02fdd9435d7f380395b7ac6
BLAKE2b-256 f25b91e8de46f9d736ebce71ec9ce3ef5882e93a31f606f3a26cc955f15db91c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.3-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.3-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.3-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3d41a97af9daa6e97833eb8018b420a6b6b1508a04a86be22556778dbbd6ade1
MD5 d7f24d7e6430de836d988a0b0e88d06d
BLAKE2b-256 0505b4270d2494115a413453cf0510e861f642704839d1d42720042fb51d9940

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c567c332100e940b3fe757e7f5ce3106a0519d37ad347cb8660369fac03a25d9
MD5 7ed1b0c360bb41866d1a01b18d4c354f
BLAKE2b-256 975e864453835759e204c7db5c49bb12f1f7d1150c2cb431b1caa5dd6ae80557

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.3-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.3-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.3-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 7ee54c3697ea025b44b33854757da2e007f78ffd7822cb2a19ff27b885d24218
MD5 2f1d1f584ef1cf9b93bcc5ba8ff86eaf
BLAKE2b-256 a6d02de002d12bc1544f3b265418a01af3a6844bba4173039fd5581246a8553d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.3-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 91b0bc3b525e37f5629391543428d89fbaf4ec77338494495050a55ba0f6abd1
MD5 1ac3c6c63468bac37cf721b1e48c143e
BLAKE2b-256 82f1624ce9e4223c6f994a14337a92cd7737d141c233f460e439c92f2c416b6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.3-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.3-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.3-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 a420a0d85fc3fd3f4eb2d37986dd8fd1c46a5b52206999c054d0fae6437e57ab
MD5 044e956a1788c4d0a9d4c2c4de6fb5cd
BLAKE2b-256 9f75b6dead9d79ead68b22293795c58c448d79ac3137fad8f9f97f55be206df0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.46.3-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 69ed6a974b97b1787b320d99ecaae0db548a433efa031cc574e8df11593187f9
MD5 53f8f4feb92661aa12a701359a5ffe7b
BLAKE2b-256 8a3c01bd0bd951ff5e305b6c5a8d45983f7135a1a61b096d84b855c5e59471c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.46.3-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.3-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.3-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ca10da47f7cc592ac3f3ad875fbdc4d71305b5ef9b11610d5f9da19a9d642948
MD5 cdd1889823c2982d714048523bc51fc1
BLAKE2b-256 3bda65aeca26eb297abebe006533fcb8aaf535f6161245b0c0a6dda8ed58032c

See more details on using hashes here.

Provenance

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