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

Uploaded Python 3

sqlspec-0.43.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.7 MB view details)

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

sqlspec-0.43.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.0 MB view details)

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

sqlspec-0.43.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.7 MB view details)

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

sqlspec-0.43.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.0 MB view details)

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

sqlspec-0.43.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.7 MB view details)

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

sqlspec-0.43.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.0 MB view details)

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

sqlspec-0.43.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.6 MB view details)

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

sqlspec-0.43.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.9 MB view details)

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

sqlspec-0.43.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.6 MB view details)

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

sqlspec-0.43.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (5.0 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.43.0.tar.gz.

File metadata

  • Download URL: sqlspec-0.43.0.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.43.0.tar.gz
Algorithm Hash digest
SHA256 7462233d61e11af6d9b3d541d90e9c71e16f2c0453d7e368bd7eaaab402dc161
MD5 3d7d21977cc1ca55b12ebe655461c152
BLAKE2b-256 8239387cde405146178a49f964c88cc00be717e789e26f1ce9d1a32538735272

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: sqlspec-0.43.0-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.43.0-py3-none-any.whl
Algorithm Hash digest
SHA256 201bbd40ecd406ad8f270ae897183f4a5e785f84ee658f8beeeefae89ef5df5a
MD5 a7aac119e23b6f8239d7e99914a50325
BLAKE2b-256 ebfae3887168f2a7376c8057f90561d79f36acfa8ab26e13002f23dc6039e9cb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.43.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fdc06759be16baddcd853a67625df6c1f1d307d2f36ebe1b0d46f8ce1c794ef5
MD5 fdd0cdba11fbe438c72c43054c081e94
BLAKE2b-256 e974e446b39a31887789d81b82792f1313e38614431ba0d3a2dae834f366a881

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.43.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.43.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.43.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 dc7f0a5aebb3a3e074fcae0b6d4d38647dc43d2c026f53b56f8dd745876dae5d
MD5 075a92d1b181e5e7478a9db098cd0a38
BLAKE2b-256 30f25e31f599d414a8b85fa413c3e2e4521e40540d4296db0a38b74b3b3628a7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.43.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 269b741f83030fe0b7a6d054683fd53c53c7f1c672f01d4175ac712c0caf9688
MD5 1c38bc2942d338a5bbb07137464b67f6
BLAKE2b-256 0ea17762e934d16c334ae804beb9d679e18f77dd0608faf89899c0b8c18e5806

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.43.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.43.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.43.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 bb8422d9d6d14051416cc834ff116d03d17d10bdd1b57b27d2405aef2bfae6a0
MD5 acc3c0f07b85ca78ec5ea6a2e57c4da7
BLAKE2b-256 d0a8da2e80791f1ea6c97f459acad032f48a150f157695d50fe7a4c0f8236b71

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.43.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3512438d02da21dd8f775e52d61b7008feb7c95eec7894a8d729b548528a09db
MD5 6c8ee90395fcd40210a1b55cc5e25fbe
BLAKE2b-256 f6ea947d0c8bf745d33862656bf0aa9a59648a7d74253ab4c1b3b71a6f48eef1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.43.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.43.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.43.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 b71fe477a080174be9e8024fdf82b2ed131d53dc7249315c4031c020584bbec7
MD5 49969ab5051d4dcef47293aa304c0f9b
BLAKE2b-256 653478658e663acbfb174000a39a0ddc0098ec28e66e56eb452a61d650d62599

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.43.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c7a3137f9bd06d4c5ff1e3c4d09c549fe5a053b9e412f21e314e718ba7f9085f
MD5 8e5c6d97e435f253de48d641d69a04a4
BLAKE2b-256 d8cf5bb246922f1a0132ea59dcd7b389fd2142076ad48073886b3578c63780fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.43.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.43.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.43.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 b9da15620ea0ef1473c9cfeafe963c0eefdf32bce5b0093a7db2aba5b8512a93
MD5 7370b45ce1737735c5141b35f7f3428a
BLAKE2b-256 53092c38a8e5997b26434d9b735924e987f1db74deed037caa09febd3af1464d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.43.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 764682d006db07af95f3b787d891e003452a35311d3ca2e462bdce3966274774
MD5 e1fd37d868590c968249d0152204f95e
BLAKE2b-256 4f814de3dcdfb75dcc42a6e400e53608fcff3dfb39eb64a7a46e2903fd37ca62

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.43.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.43.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.43.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0b688a8d45a69f72bf5574eec69c129ccaa7a57c3449fcd1751ed27edc576c7c
MD5 4ad6989100a09d050057c437d3c0ae99
BLAKE2b-256 8b3ae60ece2a34a47950d157fcc550f57da831b2802ff859a9879aed8e7dd599

See more details on using hashes here.

Provenance

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