smartgrpc Agent Guide
Project Goal
smartgrpc is a small Python framework that lets developers declare gRPC APIs with:
pydantic.BaseModelfor messagesenum.IntEnumfor enums- Python classes and methods for services
The framework then:
- registers decorated elements in
SmartGrpcApplication - renders
.protofiles from Python declarations - compiles protobuf stubs with
grpcio-tools - adapts protobuf requests/responses to Pydantic models on server and client sides
The current repo now has a productionized synchronous v1 core: schema generation, runtime adapters, compatibility checks, and regression tests are in place. Remaining gaps are mainly async/platform concerns and deeper socket-level integration coverage.
Repository Layout
src/smartgrpc/application.pyMain entry point. Holds the registry, decorator helpers, proto generation, compilation, and server startup.src/smartgrpc/decorators.pyDecorators for@message,@enum,@service, and@interface.src/smartgrpc/registry.pyCollects declared elements and groups them by source file for rendering.src/smartgrpc/render.pyTurns registered Python types into proto snippets.src/smartgrpc/templates.pyJinja2 templates used by the renderer.src/smartgrpc/protos.pyWrites.protofiles and invokes protobuf compilation.src/smartgrpc/server.pyBuilds runtime gRPC servicer adapters around user-defined service classes.src/smartgrpc/client.pyBuilds runtime client wrappers around generated gRPC stubs.src/smartgrpc/convert.pyConverts between protobuf messages and Pydantic models, including oneof and well-known types.src/smartgrpc/types.pyScalar type aliases and runtime type tagging helpers.examples/bookstoreOfficial example coveringinclude(), cross-file references, and all 4 synchronous RPC shapes.testsRegression suite covering render, convert, runtime adapters, schema generation, compatibility, streaming, and well-known types.
Intended Usage Flow
Typical usage in this repo is:
- create
sg = SmartGrpcApplication() - decorate models with
@sg.message - decorate enums with
@sg.enum - decorate service classes with
@sg.service - decorate RPC methods with
@sg.interface - call
sg.generate() - call
sg.compile() - run
sg.run() - create a client with
create_client(service_class=..., app=sg, target=...)
Current Gaps And Risks
- async APIs, interceptors, TLS/auth helpers, and DI are not implemented.
- Arbitrary
Union[T1, T2, ...]mapping is still intentionally unsupported beyond explicitoneof_field(...). - Socket-level real-network integration coverage is still thinner than adapter-level coverage.
- Example runs generate protobuf artifacts in-place; regenerate them from source rather than editing generated files manually.
Working Rules For Future Changes
- Prefer changing library code under
src/smartgrpcbefore editing generated_pb2.pyfiles. - If protobuf outputs need to change, regenerate them from source declarations rather than patching generated files manually.
- When changing message conversion behavior, verify both directions:
- Pydantic -> protobuf
- protobuf -> Pydantic
- When changing rendering behavior, validate with at least one example app and inspect the generated
.proto. - Preserve Python 3.12 syntax already used by the project.
- Prefer
rgfor code search.
Local Dev Commands
- Install deps:
uv sync - Lint/format/type-check:
make format - Run example server:
uv run python examples/bookstore/server.py - Run example client:
uv run python examples/bookstore/client.py
Recommended Near-Term Milestones
- Add socket-level integration tests around the bookstore example.
- Design router-style composition on top of
include(). - Add async/interceptor support.
- Add service factory / DI support.
- Expand production docs around deployment and compatibility workflows.