SQLAlchemy 🤝 Oso Cloud
The Oso Cloud extension for SQLAlchemy enables you to filter database queries based on your application's authorization logic.
- With Local Authorization, you don’t need all your data in one place. Let your other services own things like user roles and entitlements. We’ll stitch anything relevant into queries over your SQLAlchemy data, with no need to sync.
- Pair with other extensions like
pgvectorto build powerful, secure RAG search over private data. - First-class SQLAlchemy support for unparalleled ergonomics.
Install
pip install sqlalchemy-oso-cloud
Use
Step 1: Map SQLAlchemy Data
With the utilities in sqlalchemy_oso_cloud.orm,
bind data in your SQLAlchemy models to the Oso facts you'll use
in your authorization policy.
import sqlalchemy_oso_cloud as oso
class Document(Base, oso.Resource):
...
# maps facts like `has_relation(Document{"123"}, "organization", Organization{"acme"})`
organization_id: oso.remote_relation(remote_resource_name="Organization")
# maps facts like `has_state(Document{"123"}, "published")`
state: Mapped[str] = oso.attribute()
# maps facts like `is_public(Document{"123"})`
is_public: Mapped[bool] = oso.attribute()
class DocumentChunk(Base, oso.Resource):
...
# maps facts like `has_relation(DocumentChunk{"456"}, "document", Document{"123"})`
document: Mapped["Document"] = oso.relation()
Step 2: Write a Polar policy
Unlike SQLAlchemy models which are specific to one database, Polar is agnostic of where each piece of data comes from.
actor User {}
resource Organization {
roles = ["admin", "member"];
}
resource Document {
roles = ["author"];
permissions = ["read", "write"];
relations = {
organization: Organization
};
"read" if "author";
"read" if "admin" on "organization";
"read" if
"member" on "organization" and
has_state(resource, "published");
"read" if is_public(resource);
"write" if "author";
}
resource DocumentChunk {
permissions = ["read"];
relations = {
document: Document
};
"read" if "read" on "document";
}
Step 3: Profit
Use the .authorized method to filter based on your authorization policy.
from .models import Base, DocumentChunk
import sqlalchemy_oso_cloud
from sqlalchemy_oso_cloud import select
sqlalchemy_oso_cloud.init(Base.registry)
def authorized_rag_retrieval(user, embedding):
return select(DocumentChunk)
.order_by(DocumentChunk.embedding.l2_distance(embedding))
.authorized(user, "read")
.limit(10)
Reference
Slack
Join our Slack community where Oso users and developers hang out! It's a great place to ask questions, share feedback, and get advice.
Contributing
See the Contributing Guide
License
Metadata
Release files for sqlalchemy-oso-cloud 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqlalchemy_oso_cloud-0.1.0.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlalchemy_oso_cloud-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.2 kB
Release files / sqlalchemy_oso_cloud-0.1.0.tar.gz
| Download URL | sqlalchemy_oso_cloud-0.1.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ebd0fcfb3f80aa4f3717a750f147e7dd4c35ba6d4ccec6fe8bc937f0d5f49f87
|
|
BLAKE2b-256 checksum How to use checksums |
4a40d9aaf826d4eb50fa4f4ffdd22b1fa49e45796b50f997433fde8609bffa6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Jul 8, 2025.
Transparency logRelease files / sqlalchemy_oso_cloud-0.1.0-py3-none-any.whl
| Download URL | sqlalchemy_oso_cloud-0.1.0-py3-none-any.whl |
|---|---|
| Size | 14.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
967e3d3bb769f438b9dcdbdce7479c43eda3502a99068e6a37c477a7d5ab32ae
|
|
BLAKE2b-256 checksum How to use checksums |
b2ca7758189e07047da6bdeb5b56445294c8fd9be31e81dff0161a49ae61c3b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Jul 8, 2025.
Transparency log