Files
turso/serverless/python
Glauber Costa db451100e8 differential: specify and exercise the batch API parity contract
State the parity requirement in the serverless differential conformance
spec — each serverless driver exposes the same public API as its
language's embedded driver, and non-JavaScript serverless drivers track
the JavaScript driver's capabilities — and reference it from the
AGENTS.md core principles.

Add the param_batch operation to the shared vocabulary (spec/ops.json,
documented in operations.md) and implement it in the JavaScript,
Python, and Rust harnesses: 1-4 statements through the batch() API —
parameterized INSERTs drawing from the adversarial value pool, SELECTs,
and statements from error_sqls — optionally atomic via a mode of
deferred or immediate. The embedded and serverless drivers must agree
on every statement's result; on failure they must agree on the failing
statement's index and on the per-statement results carried by the
error, entry by entry. An agreed batch failure leaves well-defined
state, so the harnesses keep comparing later operations — that is what
catches an atomic batch that reports the right index but fails to roll
back. The server-side execution statistics are excluded from the
comparison: the serverless driver reports them and the embedded driver
does not, by design.

The embedded drivers execute a batch as a sequential loop that stops at
the first error, so the comparison is the oracle for the serverless
batch: one pipeline request must behave exactly like sequential
execution that stops at the first failure.

The Rust generator fails the suite deterministically, with an
actionable message, when ops.json contains an operation it does not
implement, instead of panicking mid-case on an unknown id.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-28 10:40:26 -07:00
..

Turso Serverless Driver for Python

A pure Python driver for Turso Cloud that speaks the SQL over HTTP protocol. Designed for serverless and edge environments: no persistent connections, no native extensions, just HTTP requests from the standard library.

The API implements DB-API 2.0 and mirrors the embedded turso driver, so the same application code can run against a local database or Turso Cloud.

Usage

import turso_serverless

conn = turso_serverless.connect(
    "libsql://my-db.turso.io",
    auth_token="...",
)

conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
conn.execute("INSERT INTO users (name) VALUES (?)", ("Alice",))
conn.commit()

for row in conn.execute("SELECT id, name FROM users"):
    print(row)

conn.close()

For a database encrypted with a customer-managed key, pass the base64-encoded key as remote_encryption_key:

conn = turso_serverless.connect(
    "libsql://my-db.turso.io",
    auth_token="...",
    remote_encryption_key="...",
)

Interactive transactions span multiple HTTP requests; the server keeps the connection state alive between them:

conn.execute("BEGIN")
conn.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")
conn.execute("UPDATE accounts SET balance = balance + 100 WHERE id = 2")
conn.commit()

Conformance tests

The test suite runs against a live database. Point it at a Turso Cloud instance:

$ export TURSO_DATABASE_URL=libsql://<your-db>.turso.io
$ export TURSO_AUTH_TOKEN=<your-token>
$ uv run --extra dev pytest

The tests skip themselves when the environment variables are not set.

Differential tests

differential holds property-based parity tests: random operation sequences generated from the shared spec (serverless/conformance/differential/spec/ops.json) run against both the embedded pyturso driver and this driver talking to Turso Cloud, and any divergence in results, result shapes, value types, or error outcomes fails the property. With the same environment variables set:

$ uv run --extra differential pytest differential

The embedded driver is built from bindings/python, so a Rust toolchain is required. HEGEL_NUM_RUNS tunes iterations per property (default 10, sized for a remote database over the network).

The encryption header property test (differential/test_encryption_header.py) is the exception: it runs against a local stub server and needs no database, no environment variables, and no embedded driver:

$ uv run --extra dev pytest differential/test_encryption_header.py