Skip to main content

sqlalchemy-semoss

PyPI version Python License: MIT

SQLAlchemy dialect and Active Record ORM for SEMOSS databases.

Makes SEMOSS's ai_server.DatabaseEngine accessible through standard Python database interfaces — PEP 249 DB-API 2.0, SQLAlchemy dialect, and an Active Record ORM with save(), get(), find(), all(), and where().

Architecture

Your Application
       │
   sqlalchemy_semoss.orm        Active Record (save/delete/get/find/where)
       │
   sqlalchemy_semoss.dialect    SQLAlchemy Dialect (extends PGDialect)
       │
   sqlalchemy_semoss.dbapi      PEP 249 DB-API 2.0 (Connection, Cursor)
       │
   ai_server.DatabaseEngine     SEMOSS runtime (execQuery, insertData, ...)
       │
   PostgreSQL / RDBMS           Actual database

Installation

pip install sqlalchemy-semoss

Note: The ai_server module is provided by the SEMOSS runtime and does not need to be installed separately when running inside SEMOSS. For external development/testing, install pip install ai-server-sdk.

Quick Start

1. Raw DB-API

Use the PEP 249 interface directly for full control:

from sqlalchemy_semoss import connect

conn = connect(engine_id="your-engine-uuid")
cursor = conn.cursor()

cursor.execute("SELECT * FROM users WHERE active = %s", (True,))
for row in cursor.fetchall():
    print(row)

cursor.close()
conn.close()

2. Active Record ORM

Define models with SQLAlchemy columns, then use Rails-style methods:

from sqlalchemy_semoss import configure, SemossModel
from sqlalchemy import Column, Integer, String, DateTime

# Call once at startup
configure("your-engine-uuid")

class User(SemossModel):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    email = Column(String(255))

# Create
user = User(name="Alice", email="alice@example.com")
user.save()
print(user.id)  # auto-populated via RETURNING

# Read
user = User.get(1)                          # by primary key
users = User.find(name="Alice")             # by conditions
all_users = User.all()                      # all records
count = User.count(name="Alice")            # count

# Query builder
users = (User.where(active=True)
             .order_by("-created_at")
             .limit(10)
             .all())

first = User.where(name="Alice").first()

# Update
user.name = "Bob"
user.save()

# Delete
user.delete()

3. SQLAlchemy Engine

Use the dialect directly with SQLAlchemy's create_engine:

from sqlalchemy_semoss import create_engine

engine = create_engine("your-engine-uuid")

# Or via URL string (works after pip install due to entry-point registration):
from sqlalchemy import create_engine as sa_create_engine
engine = sa_create_engine("semoss://your-engine-uuid")

Building Framework Adapters

The DB-API layer (connect, SemossCursor) provides everything needed to build integrations with frameworks like APScheduler, LangGraph, Celery, etc.

from sqlalchemy_semoss import connect

conn = connect(engine_id="your-engine-uuid")
cursor = conn.cursor()

# Execute any SQL
cursor.execute("CREATE TABLE IF NOT EXISTS jobs (id VARCHAR PRIMARY KEY, state TEXT)")
cursor.execute("INSERT INTO jobs (id, state) VALUES (%s, %s)", ("job-1", "pending"))
cursor.execute("SELECT * FROM jobs WHERE id = %s", ("job-1",))
row = cursor.fetchone()

See the SemossDB example app for working APScheduler and LangGraph adapter implementations.

API Reference

Module-Level Functions

Function Description
connect(engine_id, insight_id=None) Create a DB-API 2.0 connection
configure(engine_id, **kwargs) Initialize the ORM (call once at startup)
create_engine(engine_id, **kwargs) Create a SQLAlchemy Engine
get_session() Get a new SQLAlchemy Session
get_engine() Get the configured SQLAlchemy Engine

Classes

Class Description
SemossConnection DB-API 2.0 Connection (wraps DatabaseEngine)
SemossCursor DB-API 2.0 Cursor (execute, fetch, iterate)
SemossDialect SQLAlchemy dialect (extends PGDialect)
SemossModel Active Record base class (extend for your models)
SemossBase SQLAlchemy DeclarativeBase (for advanced use)
QueryBuilder Chainable query builder (returned by Model.where())

Active Record Methods

Instance methods:

Method Description
save() INSERT (new) or UPDATE (existing). Returns self.
delete() DELETE this record.

Class methods:

Method Description
Model.get(pk) Find by primary key. Returns instance or None.
Model.find(**kwargs) Find all matching conditions (AND).
Model.all() Return all records.
Model.count(**kwargs) Count matching records.
Model.where(**kwargs) Start a chainable QueryBuilder.

QueryBuilder methods:

Method Description
.where(**kwargs) Add filter conditions (AND).
.order_by(*cols) Order results. Prefix with - for DESC.
.limit(n) Limit results.
.offset(n) Skip first n results.
.all() Execute and return all matches.
.first() Execute and return first match or None.
.count() Execute and return count.

How It Works

SQL Routing

The SEMOSS DatabaseEngine exposes four methods. The driver classifies each SQL statement and routes it accordingly:

SQL Type Method Called
SELECT, WITH, SHOW, EXPLAIN database.execQuery(query=sql)
INSERT database.insertData(query=sql)
UPDATE database.updateData(query=sql)
DELETE, TRUNCATE database.removeData(query=sql)
CREATE, ALTER, DROP database.execQuery(query=sql)

INSERT ... RETURNING: DML statements with a RETURNING clause are routed through execQuery (not insertData) since they return a result set. This is how save() populates auto-generated fields like primary keys.

Parameter Interpolation

Since DatabaseEngine methods accept only final SQL strings, parameters are interpolated before execution. The cursor handles escaping of strings, numbers, dates, booleans, bytes, and None (→ NULL).

Auto-Commit

SEMOSS database operations auto-commit individually. commit() and rollback() are no-ops.

Limitations

  • No transactions — each operation auto-commits; rollback is not supported
  • No SQLAlchemy session queries — session.execute(select(Model)) is not supported; use Active Record methods instead
  • No relationships — relationship() and ForeignKey joins are not supported through Active Record
  • No server-side cursors — results are fetched eagerly in one RPC call
  • Schema reflection — best-effort via information_schema; may not work on all SEMOSS backends

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub.

License

MIT

Metadata

Release files for sqlalchemy-semoss 0.1.5

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

Source distribution (sdist)

Source distribution for sqlalchemy-semoss 0.1.5
File Size Uploaded Provenance Actions
sqlalchemy_semoss-0.1.5.tar.gz 24.9 kB Without provenance Details for sqlalchemy_semoss-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-semoss 0.1.5
File Interpreter ABI Platform Actions
sqlalchemy_semoss-0.1.5-py3-none-any.whl Python 3 none any Details for sqlalchemy_semoss-0.1.5-py3-none-any.whl

Total release size: 46.5 kB

Release files / sqlalchemy_semoss-0.1.5.tar.gz

Download URL sqlalchemy_semoss-0.1.5.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6327786eae37d101cb123f34fa281e4dfbc978524a8d81586068893de702ffeb
BLAKE2b-256 checksum
How to use checksums
c1ba9e010d681822b79265cef3af84c1aff6cdc6d886713b4078ac1cf6e2cacd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / sqlalchemy_semoss-0.1.5-py3-none-any.whl

Download URL sqlalchemy_semoss-0.1.5-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
922d332b81998c933f2e41f48460640bb99304e5f38cd5017785f58f146a27e5
BLAKE2b-256 checksum
How to use checksums
0c3e74769694aa74b90c44df36c98d24f8fe978a79da0aaa85eaf441aa1ab868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for sqlalchemy-semoss 0.1.5

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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