Skip to main content

swan-aisql

SWAN AI-SQL as a pip package: DuckDB with semantic SQL functions (ai_filter, ai_classify, ai_score, ai_complete, ai_agg, ai_embed, ai_image) and the SWAN optimizer, which folds duplicate prompts, orders predicates by learned selectivity, pushes LIMIT into the model calls and keeps a fixed number of requests in flight. Engine, design and benchmarks: https://github.com/ZhaoFuheng/SWAN-AISQL.

pip install "swan-aisql[embed]"      # the extension, the pinned duckdb, and the local embeddings server
pip install swan-aisql               # without the embeddings server (sentence-transformers + torch)
import os, swan_aisql

con = swan_aisql.connect()        # OPENAI_API_KEY from the environment -> OpenAI directly, model gpt-5.6-luna
con.sql("CREATE TABLE reviews AS SELECT * FROM 'reviews.parquet'")
con.sql("""
    SELECT critic, review
    FROM reviews
    WHERE ai_filter('Is this movie review clearly positive? Review: ' || review)
    LIMIT 10
""").show()
con.sql("SELECT llm_calls, cache_hits, cost_usd FROM ai_usage()").show()

examples/swan2_formula_1.ipynb walks through this on a SWAN 2.0 benchmark database, with the model calls each question costs. connect() returns a plain duckdb.DuckDBPyConnection; swan_aisql.load(con) adds the extension to a connection you opened yourself (with allow_unsigned_extensions).

Endpoint. Any OpenAI-compatible chat endpoint: connect(endpoint="https://api.openai.com"), a litellm proxy, or the repository's caching proxy (endpoint="http://localhost:4001"). With no endpoint, the extension uses AI_PROXY_URL from the environment and, failing that, OpenAI when OPENAI_API_KEY is set. https:// endpoints need the TLS build of the extension, which the published wheels are. Requests carry temperature: 0; a model that rejects the parameter (the gpt-5 reasoning models answer HTTP 400 to it) gets the request again without it, and the process leaves it out from then on.

Settings. model, api_key, reasoning_effort and price_input / price_output / price_cached (USD per 1M tokens, so ai_usage().cost_usd prices a direct endpoint's calls) are keywords; every other SET ai_* option passes through, e.g. connect(ai_concurrency=50, ai_limit=False). The full list is SELECT * FROM duckdb_settings() WHERE name LIKE 'ai_%' and the repository README.

Embeddings. The learned predicate ordering embeds prompts and inputs through a local server: MiniLM for text and CLIP for ai_image predicates, the setup every published number used. With the embed extra installed, connect() starts it when nothing answers on the embeddings endpoint (localhost:4002 unless ai_embed_endpoint or AI_EMBED_URL says otherwise) and waits for it to listen; the first start downloads the two models (about 1.2 GB into ~/.cache/huggingface), later starts take a few seconds. The server outlives the Python process so the next one finds it running; swan_aisql.stop_embed_server() ends it, swan-aisql-embed [port] runs it by hand, and connect(embed_server=False) never starts one. Any OpenAI-compatible embeddings endpoint works instead, text only: connect(ai_embed_endpoint="https://api.openai.com", ai_embed_model="text-embedding-3-small"). With no server at all the engine notices on the first request, orders predicates by prompt cost alone from then on, and every query still runs.

Version coupling. A DuckDB extension loads only into the exact DuckDB build it was compiled for, so each swan-aisql release pins one duckdb version (this release: 2.0.0, the DuckDB 2.0 LTS). connect() reports a mismatch with the pip install 'duckdb==...' line that fixes it.

Building a wheel yourself (from the repository checkout, after make release):

pip install build wheel "duckdb==$(sed -n 's/^dependencies = \["duckdb==\([^"]*\)"\]/\1/p' python/pyproject.toml)"
python/build_wheel.sh                        # packs build/release/extension/aisql/aisql.duckdb_extension
pip install python/dist/swan_aisql-*.whl

The binary must come from the DuckDB commit the pinned duckdb wheel was built from (the duckdb/ submodule pin); the script checks the binary's footer against the installed engine and refuses any other build.

Releasing (maintainers). .github/workflows/PythonWheels.yml builds the extension for every platform through the DuckDB distribution pipeline, packs one wheel per platform, smoke-tests each against the pinned duckdb wheel on its own runner, and on a v* tag publishes them: a tag ending in -test goes to TestPyPI, any other to PyPI. Publishing uses PyPI trusted publishing (no stored token): register the project swan-aisql with owner ZhaoFuheng, repository SWAN-AISQL, workflow PythonWheels.yml and environment pypi on pypi.org (testpypi on test.pypi.org), and create those two environments in the repository settings. Bumping DuckDB: pin the duckdb/ submodule to the commit the target duckdb wheel reports as source_id, set the same commit in both workflows and the matching duckdb== version in pyproject.toml.

Metadata

Release files for swan-aisql 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for swan-aisql 0.3.2
File Actions
swan_aisql-0.3.2-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for swan_aisql-0.3.2-py3-none-win_amd64.whl
swan_aisql-0.3.2-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details for swan_aisql-0.3.2-py3-none-manylinux_2_28_x86_64.whl
swan_aisql-0.3.2-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details for swan_aisql-0.3.2-py3-none-manylinux_2_28_aarch64.whl
swan_aisql-0.3.2-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for swan_aisql-0.3.2-py3-none-macosx_11_0_arm64.whl
swan_aisql-0.3.2-py3-none-macosx_10_15_x86_64.whl Python 3 none macOS 10.15+ x86-64 Details for swan_aisql-0.3.2-py3-none-macosx_10_15_x86_64.whl

Total release size: 86.6 MB

Release files / swan_aisql-0.3.2-py3-none-win_amd64.whl

Download URL swan_aisql-0.3.2-py3-none-win_amd64.whl
Size 17.2 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
20cf92b9c5b59792851a1086c62a51fc4b3144c99ecd32025b2513e96a02ff4a
BLAKE2b-256 checksum
How to use checksums
5a7d896e947a6894fc7b988932051a0328da549269d16c01b0a113b5c747220f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / swan_aisql-0.3.2-py3-none-manylinux_2_28_x86_64.whl

Download URL swan_aisql-0.3.2-py3-none-manylinux_2_28_x86_64.whl
Size 20.2 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
5a5b6fa87fae58a4556aa8d6c85e44681b793a5d7b3ee936846849a0251a4707
BLAKE2b-256 checksum
How to use checksums
db404c955b79ce67ca3c57270ce4b790ccb72845add9fa5a3821c7d63fcb688e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / swan_aisql-0.3.2-py3-none-manylinux_2_28_aarch64.whl

Download URL swan_aisql-0.3.2-py3-none-manylinux_2_28_aarch64.whl
Size 18.9 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
b5adb0b4663c8a06a7bbad6575401f381e6705c56051f938158483b12a7d57b6
BLAKE2b-256 checksum
How to use checksums
f9cf13b95db5dc1761e0c2aa8e4e0874a8098b6c2695bb44aca9e8cae1595a7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / swan_aisql-0.3.2-py3-none-macosx_11_0_arm64.whl

Download URL swan_aisql-0.3.2-py3-none-macosx_11_0_arm64.whl
Size 14.8 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5b9e083a4a711a79e42444c6d51a94fb42106f5483bf99ee6eaadffcba6ae5b5
BLAKE2b-256 checksum
How to use checksums
7cd796b63fb241ddce8a1754813d57bfbffa4831a4396ad3070ed6cccfb2dfa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / swan_aisql-0.3.2-py3-none-macosx_10_15_x86_64.whl

Download URL swan_aisql-0.3.2-py3-none-macosx_10_15_x86_64.whl
Size 15.4 MB
Tags Python 3 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
8277657dbf2da456310f7072d9b386395a4fd650c7600cd1191007d6d4994ac5
BLAKE2b-256 checksum
How to use checksums
de824cd29b10abf54e0858c1b4a807a8279f980ef02f46f0af2ddd14b116d7b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for swan-aisql 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for swan_aisql-0.3.2-py3-none-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page