Adds gRPC support to FastAPI apps using decorators and Pydantic auto-generation
Project description
FastAPI gRPC Bridge
A Python package that seamlessly adds gRPC support to FastAPI applications using decorators. It automatically generates Protocol Buffer definitions from Pydantic models and creates gRPC services that mirror your FastAPI routes.
Features
- 🚀 Easy Integration: Add gRPC support to existing FastAPI apps with a simple decorator
- 🔄 Auto Proto Generation: Automatically generates
.protofiles from Pydantic models - 📡 Dual Protocol Support: Serve both HTTP and gRPC from the same codebase
- ⚡ Auto-Start gRPC: Automatically starts gRPC server when FastAPI starts (configurable)
- 🎯 Type Safety: Full type safety with Pydantic models
- 🔧 FastAPI Router Support: Works with FastAPI routers and sub-applications
- 🌐 Async Support: Full support for async/await functions
Installation
pip install fastapi-grpc-bridge
Quick Start
1. Basic Usage with Auto-Start (Recommended)
from fastapi import FastAPI
from fastapi_grpc_bridge import grpc_route, add_grpc_support
from pydantic import BaseModel
app = FastAPI()
class HelloResponse(BaseModel):
message: str
@app.get("/hello")
@grpc_route("/hello")
async def say_hello(name: str) -> HelloResponse:
return HelloResponse(message=f"Hello, {name}!")
# Add gRPC support - this will auto-start the gRPC server when FastAPI starts
add_grpc_support(app)
if __name__ == "__main__":
import uvicorn
# Both HTTP (port 8000) and gRPC (port 50051) will start automatically
uvicorn.run(app, host="0.0.0.0", port=8000)
2. Manual gRPC Server Control
from fastapi import FastAPI
from fastapi_grpc_bridge import grpc_route, add_grpc_support, start_grpc_server_standalone
from pydantic import BaseModel
app = FastAPI()
class HelloResponse(BaseModel):
message: str
@app.get("/hello")
@grpc_route("/hello")
async def say_hello(name: str) -> HelloResponse:
return HelloResponse(message=f"Hello, {name}!")
# Disable auto-start
add_grpc_support(app, auto_start_grpc=False)
# Start gRPC server manually in a separate script
if __name__ == "__main__":
start_grpc_server_standalone() # Or use this for standalone gRPC server
3. Start Both Services
Option A: Auto-Start (Single Command)
python app.py
# This starts both FastAPI (HTTP) on port 8000 and gRPC on port 50051
Option B: Manual Start (Separate Commands)
# Terminal 1: Start FastAPI
uvicorn app:app --host 0.0.0.0 --port 8000
# Terminal 2: Start gRPC server
python -c "from app import app; from fastapi_grpc_bridge import start_grpc_server_standalone; start_grpc_server_standalone()"
Working with FastAPI Routers
from fastapi import FastAPI, APIRouter
from fastapi_grpc_bridge import grpc_route, add_grpc_support
from pydantic import BaseModel
app = FastAPI()
api_router = APIRouter(prefix="/api")
class UserResponse(BaseModel):
id: int
name: str
email: str
@api_router.get("/user")
@grpc_route("/user")
async def get_user(user_id: int) -> UserResponse:
return UserResponse(
id=user_id,
name=f"User {user_id}",
email=f"user{user_id}@example.com"
)
app.include_router(api_router)
add_grpc_support(app) # Auto-starts gRPC server
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Type Support
The package automatically maps Python types to Protocol Buffer types:
| Python Type | Protocol Buffer Type |
|---|---|
str |
string |
int |
int32 |
float |
float |
bool |
bool |
bytes |
bytes |
Optional[T] |
T (optional) |
API Reference
@grpc_route(path: str, response_model: Optional[Type[BaseModel]] = None)
Decorator to add gRPC support to FastAPI routes.
Parameters:
path: Route path (used for naming the gRPC service)response_model: Pydantic model for the response (optional if return annotation is provided)
add_grpc_support(app_or_router, auto_start_grpc=True)
Add gRPC support to a FastAPI app or router.
Parameters:
app_or_router: FastAPI app or APIRouter instanceauto_start_grpc: Whether to automatically start gRPC server when FastAPI starts (default: True)
start_grpc_server_standalone()
Start the gRPC server in standalone mode (for manual control).
Examples
See the examples/ directory for complete working examples:
examples/app.py: Complete example with auto-start functionalityexamples/start_grpc_server.py: Standalone gRPC server launcherexamples/test_auto_start.py: Test script to verify both services
Testing Your Services
Test HTTP Service
curl "http://localhost:8000/hello?name=World"
curl "http://localhost:8000/api/user?user_id=123"
curl "http://localhost:8000/health" # Shows gRPC routes status
Test gRPC Service
import grpc
import sys
sys.path.append('generated_protos')
import say_hello_pb2
import say_hello_pb2_grpc
def test_grpc_service():
channel = grpc.insecure_channel('localhost:50051')
stub = say_hello_pb2_grpc.Say_helloServiceStub(channel)
request = say_hello_pb2.say_helloRequest(name="World")
response = stub.Call(request)
print(f"Response: {response.message}")
channel.close()
if __name__ == "__main__":
test_grpc_service()
Automated Testing
# Start the app
python examples/app.py &
# Run tests
python examples/test_auto_start.py
Generated Files
The package automatically generates:
.protofiles in thegenerated_protos/directory- Python gRPC client and server stubs (
*_pb2.pyand*_pb2_grpc.py)
Configuration
Ports
- HTTP: 8000 (configurable via uvicorn)
- gRPC: 50051 (fixed, can be modified in
grpc_server.py)
Auto-Start Behavior
- Default: gRPC server auto-starts when FastAPI starts
- Disable: Set
auto_start_grpc=Falseinadd_grpc_support() - Manual: Use
start_grpc_server_standalone()for manual control
Requirements
- Python 3.7+
- FastAPI
- Pydantic
- grpcio
- grpcio-tools
- requests (for testing)
License
MIT License
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 fastapi_grpc_bridge-0.1.2.tar.gz.
File metadata
- Download URL: fastapi_grpc_bridge-0.1.2.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bec7bef26789e8f7ad9e0af8e70f093933e87c9d0085bc615e52daa35527bfd8
|
|
| MD5 |
cff2f026196081635766b9e73db46ba4
|
|
| BLAKE2b-256 |
998948dcb63538cc7b3ed709911d2f59c8ef22c44bca63d223e3ca9d0b901e6b
|
File details
Details for the file fastapi_grpc_bridge-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_grpc_bridge-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9389988706b2b825042028e04519ec3ec8c6ce4281bd1c0a655770d1ac787c29
|
|
| MD5 |
c0a169046a3e63d29a230af166c35f2a
|
|
| BLAKE2b-256 |
db5adad50caa3ae5cc51d1c8be96c0fcdbb6a1181c5062d59ea0266114ab982d
|