gRPCModel, gRPC models and schemas in Python, designed for simplicity, compatibility, and robustness.
Project description
SQLModel with Protocol Buffers & gRPC support
A fork of SQLModel adding seamless protobuf/gRPC integration
🚀 What is this?
This is a fork of SQLModel that extends the original library with Protocol Buffers (protobuf) and gRPC integration. SQLModel is a library for interacting with SQL databases from Python code, with Python objects.
Original SQLModel: https://sqlmodel.tiangolo.com | GitHub
SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust.
SQLModel is based on Python type annotations, and powered by Pydantic and SQLAlchemy.
The key features are:
- Intuitive to write: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
- Easy to use: It has sensible defaults and does a lot of work underneath to simplify the code you write.
- Compatible: It is designed to be compatible with FastAPI, Pydantic, and SQLAlchemy.
- Extensible: You have all the power of SQLAlchemy and Pydantic underneath.
- Short: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic.
- 🆕 Protocol Buffers Integration: SQLModel classes now implement the protobuf
Messageinterface, enabling direct serialization/deserialization and gRPC compatibility. - 🆕 Automatic Descriptor Generation: Protobuf descriptors are automatically synthesized from your SQLModel classes with intelligent type inference.
- 🆕 gRPC Ready: Use your SQLModel models directly in gRPC services without additional conversion code.
SQL Databases in FastAPI
SQLModel is designed to simplify interacting with SQL databases in FastAPI applications, it was created by the same author. 😁
It combines SQLAlchemy and Pydantic and tries to simplify the code you write as much as possible, allowing you to reduce the code duplication to a minimum, but while getting the best developer experience possible.
SQLModel is, in fact, a thin layer on top of Pydantic and SQLAlchemy, carefully designed to be compatible with both.
New Features: Protocol Buffers & gRPC Integration
This fork extends SQLModel with seamless Protocol Buffers (protobuf) and gRPC support:
✨ Key Additions
-
Protobuf Message Interface: All SQLModel classes now inherit from
google.protobuf.message.Message, making them fully compatible with protobuf serialization and gRPC. -
Automatic Descriptor Generation: Protobuf descriptors are automatically synthesized from your SQLModel class definitions, with intelligent type inference from Python type annotations.
-
Full Protobuf API: All standard protobuf methods are implemented:
SerializeToString()/serialize_to_string()- Serialize to binaryParseFromString()/parse_from_string()- Deserialize from binaryMergeFrom()/merge_from()- Merge from another messageClear()/clear()- Reset to defaultsHasField()/has_field()- Check field presence- And more...
-
Type Inference: Automatically maps Python types to protobuf field types:
int→TYPE_INT64float→TYPE_DOUBLEbool→TYPE_BOOLstr→TYPE_STRINGbytes→TYPE_BYTES- Lists/tuples →
LABEL_REPEATED
-
Custom Field Descriptors: Optionally provide custom protobuf field descriptors via
Field(grpc_descriptor=...)for fine-grained control.
Example: Using SQLModel with gRPC
from sqlmodel import Field, SQLModel
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
age: int | None = None
# Create a hero instance
hero = Hero(name="Deadpond", age=30)
# Serialize to protobuf binary format
serialized = hero.SerializeToString()
# Deserialize back
hero2 = Hero()
hero2.ParseFromString(serialized)
# Use directly in gRPC services!
# Your SQLModel classes work seamlessly with gRPC
Requirements
A recent and currently supported version of Python.
As SQLModel is based on Pydantic and SQLAlchemy, it requires them. They will be automatically installed when you install SQLModel.
Additional requirement for this fork: protobuf>=5.29.5 (automatically installed).
Installation
Make sure you create a virtual environment, activate it, and then install this fork:
# Install from source (this fork)
$ uv pip install -e .
# Or install directly from git
$ uv pip install git+https://github.com/YOUR_USERNAME/grpcmodel.git
Note: For the original SQLModel without protobuf support, install from PyPI:
$ uv pip install sqlmodel
Example
For an introduction to databases, SQL, and everything else, see the SQLModel documentation.
Here's a quick example. ✨
A SQL Table
Imagine you have a SQL table called hero with:
idnamesecret_nameage
And you want it to have this data:
| id | name | secret_name | age |
|---|---|---|---|
| 1 | Deadpond | Dive Wilson | null |
| 2 | Spider-Boy | Pedro Parqueador | null |
| 3 | Rusty-Man | Tommy Sharp | 48 |
Create a SQLModel Model
Then you could create a SQLModel model like this:
from sqlmodel import Field, SQLModel
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str
age: int | None = None
That class Hero is a SQLModel model, the equivalent of a SQL table in Python code.
And each of those class attributes is equivalent to each table column.
Create Rows
Then you could create each row of the table as an instance of the model:
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
This way, you can use conventional Python code with classes and instances that represent tables and rows, and that way communicate with the SQL database.
Editor Support
Everything is designed for you to get the best developer experience possible, with the best editor support.
Including autocompletion:
And inline errors:
Write to the Database
You can learn a lot more about SQLModel by quickly following the tutorial, but if you need a taste right now of how to put all that together and save to the database, you can do this:
from sqlmodel import Field, Session, SQLModel, create_engine
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str
age: int | None = None
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
session.add(hero_1)
session.add(hero_2)
session.add(hero_3)
session.commit()
That will save a SQLite database with the 3 heroes.
Select from the Database
Then you could write queries to select from that same database, for example with:
from sqlmodel import Field, Session, SQLModel, create_engine, select
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str
age: int | None = None
engine = create_engine("sqlite:///database.db")
with Session(engine) as session:
statement = select(Hero).where(Hero.name == "Spider-Boy")
hero = session.exec(statement).first()
print(hero)
Editor Support Everywhere
SQLModel was carefully designed to give you the best developer experience and editor support, even after selecting data from the database:
SQLAlchemy, Pydantic, and Protocol Buffers
That class Hero is a SQLModel model.
But at the same time, ✨ it is a SQLAlchemy model ✨. So, you can combine it and use it with other SQLAlchemy models, or you could easily migrate applications with SQLAlchemy to SQLModel.
And at the same time, ✨ it is also a Pydantic model ✨. You can use inheritance with it to define all your data models while avoiding code duplication. That makes it very easy to use with FastAPI.
And now, ✨ it is also a protobuf Message ✨. You can serialize it to binary format, use it directly in gRPC services, and interact with any protobuf-based system without additional conversion layers.
Differences from Original SQLModel
This fork maintains 100% compatibility with the original SQLModel while adding:
- ✅ All original SQLModel features work exactly as before
- ✅ New protobuf/gRPC capabilities seamlessly integrated
- ✅ Automatic descriptor generation (no extra code needed)
- ✅ Backward compatible - existing code continues to work
Contributing
This is a fork of the original SQLModel. For issues and contributions related to:
- Core SQLModel features: Please refer to the original SQLModel repository
- Protobuf/gRPC features: Please open issues in this repository
License
This project is licensed under the terms of the MIT license, same as the original SQLModel.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grpcmodel-0.0.1.tar.gz.
File metadata
- Download URL: grpcmodel-0.0.1.tar.gz
- Upload date:
- Size: 28.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc5f7e7f5ff5f088a8e117ad9340c6bc4ba61b500398acf75072bd91f4eb4223
|
|
| MD5 |
089c57e57f70e06861f84946233f1140
|
|
| BLAKE2b-256 |
97707fef57e898c8eac3a1f3440b9ded84c03c41527b83774b210505ea36625d
|
Provenance
The following attestation bundles were made for grpcmodel-0.0.1.tar.gz:
Publisher:
publish.yml on river-studio-net/grpcmodel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
grpcmodel-0.0.1.tar.gz -
Subject digest:
dc5f7e7f5ff5f088a8e117ad9340c6bc4ba61b500398acf75072bd91f4eb4223 - Sigstore transparency entry: 660703410
- Sigstore integration time:
-
Permalink:
river-studio-net/grpcmodel@a669840b305b5037d5192796fc286e662f0b8279 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/river-studio-net
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a669840b305b5037d5192796fc286e662f0b8279 -
Trigger Event:
push
-
Statement type:
File details
Details for the file grpcmodel-0.0.1-py3-none-any.whl.
File metadata
- Download URL: grpcmodel-0.0.1-py3-none-any.whl
- Upload date:
- Size: 33.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6128ef157b7f11b9dc2cc80d5b260b7bc49fa306b7c5f90f564c76a61c188d1b
|
|
| MD5 |
00a1b4c7495b75097dd63d20fe43fdee
|
|
| BLAKE2b-256 |
def10c1fec197590d7e9bcbd1ad05fa81d4655372cbc6fd732ce1938cdf8caac
|
Provenance
The following attestation bundles were made for grpcmodel-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on river-studio-net/grpcmodel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
grpcmodel-0.0.1-py3-none-any.whl -
Subject digest:
6128ef157b7f11b9dc2cc80d5b260b7bc49fa306b7c5f90f564c76a61c188d1b - Sigstore transparency entry: 660703417
- Sigstore integration time:
-
Permalink:
river-studio-net/grpcmodel@a669840b305b5037d5192796fc286e662f0b8279 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/river-studio-net
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a669840b305b5037d5192796fc286e662f0b8279 -
Trigger Event:
push
-
Statement type: