Skip to main content

SQL Experiments in Python

Project description

SQLSpec

Type-safe SQL execution layer for Python.

SQLSpec handles database connectivity and result mapping so you can focus on SQL. Write raw queries when you need precision, use the builder API when you need composability, or load SQL from files when you need organization. Every statement passes through a sqlglot-powered AST pipeline for validation, dialect conversion, and optimization before execution. Export results as Python objects, Arrow tables, Polars or pandas DataFrames.

It's not an ORM. It's the connectivity and processing layer between your application and your database that provides the right abstraction for each situation without dictating how you write SQL.

Status

SQLSpec is currently in active development. The public API may change. Follow the docs and changelog for updates.

What You Get

Connection Management

  • Connection pooling with configurable size, timeout, and lifecycle hooks
  • Sync and async support with a unified API surface
  • Adapters for PostgreSQL (psycopg, asyncpg, psqlpy), SQLite (sqlite3, aiosqlite), DuckDB, MySQL (asyncmy, mysql-connector, pymysql), Oracle, BigQuery, and ADBC-compatible databases

Query Execution

  • Raw SQL strings with automatic parameter binding and dialect translation
  • SQL AST parsing via sqlglot for validation, optimization, and dialect conversion
  • Builder API for programmatic query construction without string concatenation
  • SQL file loading to keep queries organized alongside your code (named SQL queries)
  • Statement stacks for batching multiple operations with transaction control

Result Handling

  • Type-safe result mapping to Pydantic, msgspec, attrs, or dataclasses
  • Apache Arrow export for zero-copy integration with pandas, Polars, and analytical tools
  • Result iteration, single-row fetch, or bulk retrieval based on your use case

Framework Integration

  • Litestar plugin with dependency injection for connections, sessions, and pools
  • Starlette/FastAPI middleware for automatic transaction management
  • Flask extension with sync/async portal support

Production Features

  • SQL validation and caching via sqlglot AST parsing
  • OpenTelemetry and Prometheus instrumentation hooks
  • Database event channels with native LISTEN/NOTIFY, Oracle AQ, and a portable queue fallback
  • Structured logging with correlation ID support
  • Migration CLI for schema versioning

Quick Start

Install

pip install "sqlspec"

Run your first query

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!

That's it. Write SQL, define a schema, get typed objects back. Connection pooling, parameter binding, and result mapping are handled automatically.

See the Getting Started guide for installation variants, adapter selection, and advanced result mapping options.

Documentation

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 (CLOB) handling, and real-time performance metrics.

See the usage docs for detailed guides on adapters, configuration patterns, and features like the SQL file loader.

Built With

  • sqlglot - SQL parser, transpiler, and optimizer powering SQLSpec's AST pipeline

Contributing

Contributions, issue reports, and adapter ideas are welcome. Review the contributor guide and follow the project coding standards before opening a pull request.

License

SQLSpec is distributed under the MIT License.

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.39.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.39.0-py3-none-any.whl (1.0 MB view details)

Uploaded Python 3

sqlspec-0.39.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.2 MB view details)

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

sqlspec-0.39.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.5 MB view details)

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

sqlspec-0.39.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.2 MB view details)

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

sqlspec-0.39.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.5 MB view details)

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

sqlspec-0.39.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.2 MB view details)

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

sqlspec-0.39.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.5 MB view details)

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

sqlspec-0.39.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.1 MB view details)

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

sqlspec-0.39.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.4 MB view details)

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

sqlspec-0.39.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (4.2 MB view details)

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

sqlspec-0.39.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (4.4 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.39.0.tar.gz.

File metadata

  • Download URL: sqlspec-0.39.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.39.0.tar.gz
Algorithm Hash digest
SHA256 78caf31a4699d02c9ae09c991d8f138cba96b9122136297da3b3a732bb5e3e75
MD5 edba6213124884b7db1fab0d4489c82c
BLAKE2b-256 2e1cf80aba62d013760a942a87c49880296e93b6cef76978a1df4c689b99cee3

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: sqlspec-0.39.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.39.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fe80412a76d14b1af677e1ffcfe5b6f7d31c428cf6650dd6f01576b1a562b34
MD5 fb9b765744477177bd6cc92a5c7b6b4c
BLAKE2b-256 fd4023a67046760bd3a682dfa99c8129713a95c9afa2af76608a25e10fef4c9f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.39.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 385f467c31c27ea61cfc7ddac8e14549702b6c0bd7d4b7093475e204f8c62347
MD5 94854c34b763c78c0101ec5f93fdd844
BLAKE2b-256 b5cc727d17ed679463e64289192f0d169442006b4897dba5e42ad4728febbc28

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.39.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.39.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.39.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0950864b7be48e8928627fad3582bf12123b2defc387b1dba3642ce71c5936aa
MD5 62740ebca633453ecb2c22bdde9c0404
BLAKE2b-256 7fec3bedb7b3ba3e897dbbeb7a6081ece7f116318105bc52f79611c25a68520f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.39.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3f9ef47e6745d57f671ce41521145453843f4fca99a770b9837ab7fb77b73da4
MD5 efc59f89249ecd6d6028e8925243fe27
BLAKE2b-256 4942244689d0b15340c5e3c7be19644a0889545c104f131e042aaaad6decd4a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.39.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.39.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.39.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e891f9a6b20fba410c66341d3bd07ccc80592739a337cb1108ca0aee37fd83f4
MD5 f1e30366fb18b9597d8628cc706c3412
BLAKE2b-256 500909146f25070b323bffba82ebe5e13be174ee3512a4b5f04ab074f80553b4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.39.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7f0f6988f3592ec28ff448ba3fe569dbe07bd1858ca274aebd4472a88a47bdd3
MD5 3973c692efd8cb828773a5ec5cf8fed4
BLAKE2b-256 73bb45c1387ed1283798051346b24aedc2b43257a1c2c565a1d2c17ae05a2199

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.39.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.39.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.39.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 308e6b4c64f9e32f783d18092c3bc782a3d0856ed42a96f52c0a7c1275b2796d
MD5 bdb2e2b5815e5d558d909c9ba9ca7e9a
BLAKE2b-256 6d4708a00fd6268f53fac905f75b88a96944eecc9a39dd1e32420859e8e1858d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.39.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 059def818c7426642a66b15ec4e484dd70294735b46d36171c5484bb547c2c5b
MD5 a4e48d9a8d0cefd005ea77b42933c514
BLAKE2b-256 dd931dd3c93f6ac23d0c81f7c814583b3d9500037043bba03b004720c8132998

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.39.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.39.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.39.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 00d80efc1abdb33bbf294cb28180703add9c8dbc91145518a7c1a30faf0c3c2e
MD5 2fa0a96274722c9d224a42736f6e0224
BLAKE2b-256 2b7562de838c5e8714263e2a6bbafe750c177b0d56e4f30c7ad281d50018a117

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.39.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d6198c4c65312ba85c82b89f06e3b61c6efa0b309481c1b83c7479a740e8b5c0
MD5 0fd1e444492e77c13c3d39bde8ee4150
BLAKE2b-256 5b32be9b70ff2dc40d51f78b00a55f154a6fd74cad7e860de77ea79e05b77e5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.39.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.39.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.39.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 f02368c6d4f986cb63419b19e00ed20bf2733219fa1471f27e2e661a35049b18
MD5 ac7cb3fb36210ea7bace41797a5ed847
BLAKE2b-256 725276c55c534f8cc7c877e5aabad2ecf8b22d96e1c506ad8d466f3b5236e54a

See more details on using hashes here.

Provenance

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