# SQLAlchemy — card for AI agents

Python SQL toolkit and object-relational mapper for database abstraction.

Category: Databases, ORMs and clients · PyPI package `sqlalchemy` · latest version 2.1.6 (checked 2026-11-01)
Install: `pip install sqlalchemy`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## SQLAlchemy
- Check which line the project pins. The site lists both 2.1.4 and 2.0.54 under "Current Releases", each with its own migration notes and docs. The "Current Documentation" link is for 2.1.
- Never put literal values into SQL statements. Pass them as bound parameters, which the maintainers say lets query plans be cached and makes SQL injection "a non-issue".
- With the ORM, call `commit()` to persist changes. Nothing reaches permanent storage before that.
- If the task does not need an ORM, use the Core expression language instead. The README says: "Don't use an ORM if the problem doesn't need one."
- Express ORM queries with joins, subqueries and correlation in terms of the object model. Literal SQL is available, but the README says it is "virtually never needed".
- On the 2.1 line, use 2.1.4 or later. It fixes `Result` reference cycles introduced by the 2.1 Cython refactor and a connection leak with asyncio drivers.
- On a mapped Declarative class, assign only a `MetaData` object to the `metadata` attribute. Other values trigger a warning that the name should be left reserved.

## Agent Skills for it
- sqlalchemy-alembic-expert-best-practices-code-review from midudev/autoskills: `npx skills add midudev/autoskills --skill sqlalchemy-alembic-expert-best-practices-code-review`

## What it is
- SQLAlchemy is "the Python SQL toolkit and Object Relational Mapper". The GitHub repository calls it "The Database Toolkit for Python" and lists it as MIT licensed. The website's current documentation covers version 2.1. Its "Current Releases" list shows two lines side by side: 2.1.4 and 2.0.54.
- It has two parts. The Core is a database abstraction layer with a Python SQL expression language, schema metadata, connection pooling, type coercion and custom types. The ORM sits above it and is built on the identity map, unit of work and data mapper patterns. With its declarative configuration, changes to domain objects are synchronized with the current transaction automatically.
- The maintainers write that it gives application developers "the full power and flexibility of SQL". The README's philosophy section is blunt: "Don't use an ORM if the problem doesn't need one." It also says the developer makes all decisions about the object model and the relational schema, and SQLAlchemy only automates carrying them out.

## Caveats
- It will not design the model for you: By the maintainers' own description, the developer makes every decision about the design, structure and naming of both the object model and the schema. SQLAlchemy only provides the means to automate those decisions.
- ORM changes persist only on commit(): The README says that with the ORM, nothing goes to permanent storage until `commit()` is called. It presents this as part of making transactions the norm.
- Result iteration held cursors in early 2.1: The 2.1.4 notes fix a regression where iterating a `Result` directly, or using methods such as `Result.scalars()`, created reference cycles. These kept the `Result` and its DBAPI cursor alive until cyclic GC ran. The notes say this came from the 2.1 refactoring of `Result` into Cython code.
- Asyncio connection leak on a failed close(): The 2.1.4 release notes fix an issue in `Connection.close()`. If the rollback inside it raised an exception that was not classified as a disconnect, the connection stayed open and its pooled DBAPI connection stayed checked out. The notes say that with an asyncio driver, the connection was leaked.
- No-row statements left cursors to GC: The 2.1.4 notes describe a regression from the 1.4 series. Cursors for statements that return no rows, including `CreateTable`, `DropTable` and SAVEPOINT statements, were not closed after execution. The fix is backported to 2.0.55, while the site lists 2.0.54 as the current 2.0 release.

## Numbers (AgentGid data)
- Gid Score: 79.6 (#107 of all tools, 4 of 120 in its category)
- Used in 181 public projects set up for AI coding agents (2.9%)
- Downloads last week: 105,478,465 (as of 2026-11-24), +13.2% over 4 weeks
- GitHub stars: 12,306 (as of 2026-11-24)
- Last push: 2026-11-19

## Often used with
FastAPI 76.2%, Uvicorn 76.2%, Pydantic 71.3%, python-dotenv 63.5%, Alembic 48.6%, python-multipart 47.0%

## Checked alternatives (same job)
- DuckDB: https://agentgid.com/tools/duckdb/agent.md
- Neo4j: https://agentgid.com/tools/neo4j/agent.md
- SQLModel: https://agentgid.com/tools/sqlmodel/agent.md
- PyMongo: https://agentgid.com/tools/pymongo/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/sqlalchemy/
- Homepage: https://www.sqlalchemy.org
- Repository: https://github.com/sqlalchemy/sqlalchemy
- Source: https://github.com/sqlalchemy/sqlalchemy#readme
- Source: https://github.com/sqlalchemy/sqlalchemy
- Source: https://github.com/sqlalchemy/sqlalchemy/releases
- Source: https://www.sqlalchemy.org

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
