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

Uploaded Python 3

sqlspec-0.40.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.40.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.40.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.40.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.40.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.40.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.40.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.40.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.40.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.40.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.40.0.tar.gz.

File metadata

  • Download URL: sqlspec-0.40.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.40.0.tar.gz
Algorithm Hash digest
SHA256 4b08812613f1f42c255ebf5058ad8df6fd57b16a302396b06335aac5168aee3a
MD5 1d48cc541142332ac4715e3f6731cc0b
BLAKE2b-256 50ee5aa2a9a394cec07b92837d8aac992d5316f19164be01551fd25597958330

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: sqlspec-0.40.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.40.0-py3-none-any.whl
Algorithm Hash digest
SHA256 395a3596eac059c5b97a5512efd3306e7d064bb24881339cf6ee2e5109eda8d7
MD5 0225940b7953a9f75ad7b12bfe6ef13e
BLAKE2b-256 c23b4430cd9363268489e7b5da8703d8f470fff5331eebd7cffc8ddbb2bed992

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.40.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0a8a48c8fe470fe06e34413046b7c9150cc18f26240f4288291f03721fc4a247
MD5 ea17dc62d660edcfe7b2ecdd9f802c0b
BLAKE2b-256 9d6b16e5920f1ee3bd574c499c0596de70d04cbab2c227ff66a1faec9a90dba0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.40.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.40.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.40.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 32172362eca74df7527f19e918ffb7ccc6c19559b3a57e083a5c1ce123ea82c4
MD5 89797d2d7c2dd8206c76ab3ea3ad4e08
BLAKE2b-256 7ea7de4fc0f5ac4f96178769da991c20b0f727e3c43635e7373b1c8b3418086e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.40.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 23165055d0dd6385b752635ee1fe148b00a3defb0981f256fd7b59f7649d0150
MD5 dc230564b5a8bb18ad5788fbbce3110f
BLAKE2b-256 9e8e08939d1a75d07d214f45a756b6435b76bb6b77b8a1d0d1b57ca580c1348f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.40.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.40.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.40.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 93b0380530c21b5e426e56f7500252152091d5366b1a62711a673f46553e2e3c
MD5 d58216047df59d6476f0ec33ae9338cc
BLAKE2b-256 b176acb5fd8d57664d3cafba45856a1903fd847646d63698e46a70030c2fc9bd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.40.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7e3ad40427762e97ca91b8e2da0aaf62dd19afbfd4eb813f80353e80ab6389a6
MD5 51153afd686d3dcbf90c3aebf7d84c5f
BLAKE2b-256 a5e72caa2580b58642c7953019ea17631eae39cf777b730b6b096119b747f00f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.40.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.40.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.40.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 2a6c21be0319b7d81a85470dcd510ec5ddbd06abe9b6f72dbc55f698eac60ceb
MD5 6045b1ad28c57bf6ce59964dde3d2723
BLAKE2b-256 b6d3b1914ecb0669ab8cf54b42ac636ae469296ea7c6ac4de92c6d32ab47a30d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.40.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8315d54cee49f165ad27867e10acc727a629a568929fc40b12e9b12d384db25e
MD5 8978c90d702b371d956cb5335408e6ca
BLAKE2b-256 214c1c0f4d4252441dfc1b7b2b435141682287d75689a60daa85c7e10d897880

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.40.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.40.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.40.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 eea5d44396b6adb28364292c57d71ab68772b45a17308a5941bdc1a9a3087017
MD5 1f4d12ec347a127e3057dd54696de7c9
BLAKE2b-256 c2ce5a28e54277fbd39d911c3703eb450c3087eb7c1b5e9df9c2a5a6beab1de5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlspec-0.40.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f5a006b94d4aaed6f5ddf6f558216a6a4b1f357b590faa73d20a07b819084db3
MD5 616a902cd86901e9f8442c0549a987d6
BLAKE2b-256 87cd6806e9ddd7ed93cfee365a6ca8cc4c540514d818115128121912501b5446

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlspec-0.40.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.40.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.40.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 2af16b3c44529349c02fb3cffc37ef57cefe0ef3fbb030cfa747d1401ae6079a
MD5 191e468e2f0805a3b6955d0e28de1447
BLAKE2b-256 ba4c4cf0f789373a4355597116800793b76abd5c57c7e003a93a1bf18655fc74

See more details on using hashes here.

Provenance

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