feat(py/samples): add web-endpoints-hello — REST + gRPC kitchen-sink sample#4498
feat(py/samples): add web-endpoints-hello — REST + gRPC kitchen-sink sample#4498
Conversation
Summary of ChangesHello @yesudeep, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Python sample application that demonstrates the powerful integration of Genkit AI flows with the FastAPI web framework. The sample highlights FastAPI's native asynchronous capabilities, allowing Genkit flows to be called directly from API endpoints, and showcases features like real-time AI response streaming via SSE, context propagation from HTTP requests, and the reusability of Pydantic models across both frameworks. This addition provides a clear example for developers looking to build high-performance, AI-powered web services using Genkit and FastAPI. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new sample, web-fastapi-hello, which is a great addition to demonstrate Genkit's integration with FastAPI. The implementation correctly showcases non-streaming flow invocation, context passing, and dual server setup.
My review includes two main points of feedback:
- A suggestion to refactor the streaming endpoint (
/chat/stream) insrc/main.pyto use Genkit's native streaming API (flow.stream()). This simplifies the code, improves performance by removing polling, and avoids re-registering a flow on every request. - A minor correction in
py/samples/web-fastapi-hello/pyproject.tomlregarding an incorrect package path in the build configuration.
Overall, this is a valuable sample, and with these changes, it will be an even better example for users.
80fa459 to
9fa510a
Compare
3ea1c85 to
d25b0b2
Compare
d566fb2 to
2c235db
Compare
|
/gemini review |
2c235db to
f8ba104
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces web-endpoints-hello, a comprehensive and well-structured sample demonstrating REST and gRPC endpoints for Genkit flows. The sample is of high quality, featuring production-ready patterns for security, resilience, and observability, along with extensive documentation and tests. The changes to the wider repository, such as updates to CI, linting, and dependency configurations, are well-integrated.
My review has identified a few issues that should be addressed:
- A potential security risk in the root
.gitignorefile due to the removal of a global rule for.envfiles. - Portability issues in the shared shell script
_common.shfor installing CLI tools on different Linux distributions.
Overall, this is an excellent addition to the repository. Addressing the identified issues will further improve the security and robustness of the codebase.
e096bde to
bc0f401
Compare
Adds a new sample demonstrating FastAPI + Genkit integration, exercising several core Genkit capabilities: - Flows — @ai.flow() for tell_joke and translate_text - Tools — @ai.tool() for get_current_time (model-callable) - Structured output — Output(schema=TranslationResult) for typed JSON - Streaming — generate_stream() with SSE via StreamingResponse - Traced steps — ai.run() for auditable sub-spans in DevUI - Context injection — Authorization header forwarded to flows - uvloop — 2-4x faster async event loop Endpoints: POST /tell-joke non-streaming joke POST /tell-joke/stream SSE streaming joke POST /translate structured translation with tool use GET /health health check GET /docs auto-generated Swagger UI (auto-opened) FastAPI is ASGI-native, so Genkit flows work with plain await — no adapter layer needed unlike Flask which requires genkit_flask_handler. feat(py/samples): multi-cloud deploy, auto-telemetry, and endpoint tests Enhance the web-fastapi-hello sample with smooth multi-cloud deployment, auto-detection of cloud telemetry, and comprehensive endpoint test coverage. Rationale: Users deploying Genkit apps need zero-friction deployment to their preferred cloud with appropriate observability built in. This commit adds AWS and Azure as first-class deployment targets alongside GCP and Fly.io, with the app automatically detecting the cloud platform and enabling the right telemetry plugin (Cloud Trace, X-Ray, App Insights, or generic OTLP). Deployment: - Add deploy_aws.sh for AWS App Runner (ECR + auto-scaling) - Add deploy_azure.sh for Azure Container Apps (ACR Build + scale-to-zero) - Add CLI auto-detection/install helpers for gcloud, aws, az, flyctl in _common.sh with interactive platform-specific install prompts - Add auth check helpers (check_aws_auth, check_az_auth) alongside existing check_gcloud_auth - All deploy scripts now source _common.sh for consistent UX Telemetry: - Auto-detect GCP, AWS, Azure at startup via env vars (K_SERVICE, AWS_EXECUTION_ENV, CONTAINER_APP_NAME, etc.) - Enable platform-specific telemetry plugin automatically - Fall back to generic OTLP if OTEL_EXPORTER_OTLP_ENDPOINT is set - Add GENKIT_TELEMETRY_DISABLED env var to disable telemetry - Add --no-telemetry CLI flag - Add optional dependency groups: pip install .[gcp|aws|azure|observability] Tests: - Add pytest endpoint tests (tests/endpoints_test.py) covering all HTTP endpoints with mocked Genkit flows - Add test optional deps group in pyproject.toml Documentation: - Update README.md with AWS/Azure deploy sections, telemetry docs, and updated feasibility/secrets tables - Update justfile with deploy-aws, deploy-azure targets - Update py/samples/README.md with web-fastapi-hello entry fix(py): resolve CI lint/test failures and fix granian adapter Fix pre-existing type errors in GranianAdapter by switching from the sync granian.Granian API (whose serve() is not awaitable) to the async granian.server.embed.Server API designed for embedded use within an existing event loop. Changes: Granian adapter (packages/genkit/src/genkit/web/manager/_adapters.py): - Use granian.server.embed.Server instead of granian.Granian - Remove workers/loop params (embed API uses runtime_threads) - Import Interfaces and LogLevels correctly Type checker config (py/pyproject.toml): - Add granian, pydantic-settings to lint dependency group - Add web-fastapi-hello paths to ty environment root and pyrefly search - Add samples/*/tests/**/*.py to pyrefly project_includes - Ignore web-fastapi-hello tests in workspace pytest (run in isolation) CI (python.yml): - Add sample-tests job that installs and tests samples in isolation - Build job now depends on sample-tests Sample fixes (web-fastapi-hello): - Add conftest.py for sys.path setup (workspace + sample root) - Add pytest config to sample pyproject.toml for isolated test runs - Fix async fixture to use pytest_asyncio.fixture (strict mode) - Fix streaming test mock (use real coroutine, not AsyncMock) - Adopt .<name>.env convention (auto-gitignored by **/*.env) - Add local.env.example template for local development Env file convention: - Use .<name>.env instead of .env.<name> so all env files share the .env extension, are auto-gitignored by **/*.env, and get syntax highlighting in editors refactor(py/samples): rename web-fastapi-hello to web-asgi-hello Renames the sample to web-asgi-hello and adds multi-framework support (FastAPI + Litestar) via a framework adapter pattern. Changes: - Rename sample directory and package from web-fastapi-hello to web-asgi-hello - Add --framework=fastapi|litestar CLI flag for framework selection - Add src/frameworks/ adapter package with create_app() factory pattern - fastapi_app.py: FastAPI adapter with all routes - litestar_app.py: Litestar adapter with matching routes - Make telemetry.py framework-aware (auto-detects FastAPI vs Litestar) - Update config.py with framework setting and --framework CLI arg - Make server.py accept any ASGI app (not FastAPI-specific) - Update run.sh to pass all CLI args through ($@) - Update justfile with dev-litestar command - Update deploy scripts and Dockerfile with new service names - Update pyproject.toml with litestar and OTel ASGI dependencies - Update tests to use create_app() factory pattern - Update workspace pyproject.toml references - Update README with multi-framework docs and template usage guide - Remove old src/routes.py (replaced by framework adapters) fix(py/samples): resolve ty type checker errors in web-asgi-hello - Use Callable[..., Any] type alias for ASGI apps in server.py - Use fastapi.FastAPI concrete type and isinstance check in telemetry.py - Fix span.attributes narrowing in telemetry_test.py - Move gRPC import fallback to avoid unresolved-import error feat(py/samples): port ruff, ty, pyright, pyrefly configs to web-asgi-hello Adds standalone type checker and linter configurations to pyproject.toml so the sample works as a self-contained template when copied out of the monorepo. Includes ruff, ty, pyright, and pyrefly with the same strictness as the monorepo root. feat(py/samples): add gRPC support and rename to web-endpoints-hello Rename web-asgi-hello → web-endpoints-hello to reflect that it's now a kitchen-sink sample showing all endpoint types: REST (FastAPI, Litestar, Quart) and gRPC. gRPC additions: - src/grpc_server.py: async gRPC servicer that delegates every RPC to the same Genkit flows used by the REST endpoints - protos/genkit_sample.proto: service definition with 9 RPCs including server-side streaming (TellStory) - src/generated/: auto-generated protobuf + gRPC stubs - scripts/generate_proto.sh: regenerate stubs from proto definition - gRPC reflection enabled for grpcui/grpcurl discovery - Both servers run in parallel via asyncio.gather in ai.run_main() New CLI flags: --grpc-port (default 50051), --no-grpc New scripts: test_grpc_endpoints.sh (grpcurl-based integration tests) New justfile targets: proto, grpcui, grpc-list, test-grpc-endpoints, test-all Dependencies added: grpcio, grpcio-tools, grpcio-reflection fix(py/samples): make setup scripts cross-platform (macOS, Debian, Fedora) Rewrite setup.sh to auto-detect the OS and package manager: - macOS: Homebrew - Debian/Ubuntu: apt (with fallbacks for older versions) - Fedora: dnf - Arch: pacman Platform detection sources /etc/os-release on Linux and prints the detected OS/distro/pkg-manager at startup for debugging. Tool-specific install strategies: - uv: cross-platform curl installer (unchanged) - just: brew → apt/dnf (if available) → official install script fallback - podman: brew / apt / dnf - shellcheck: brew / apt / dnf - grpcurl: brew → go install → prebuilt binary from GitHub releases - grpcui: brew → go install (with helpful error message if Go missing) - genkit CLI: npm (cross-platform, unchanged) Also update test_grpc_endpoints.sh error messages with Linux install instructions, and add a Prerequisites section to the README with a per-platform install matrix. docs(py/samples): add instructions to pin genkit version when using as template Inside the monorepo, genkit and genkit-plugin-* dependencies resolve to local workspace packages via [tool.uv.sources]. When copying the sample out as a standalone project, users must pin these to a released PyPI version (e.g. genkit>=0.5.0) or uv sync / pip install will fail. Add a "Pin Genkit to a release version" section to README.md showing the before/after for both main dependencies and optional telemetry plugins, with a tip for finding the latest version. Update GEMINI.md with the same guidance.
bc0f401 to
b32db40
Compare
The granian.Granian() constructor's 'loop' parameter no longer accepts the string literal 'auto' (now requires the Loops enum), and 'log_level' requires a LogLevels enum instead of a raw string. These type mismatches cause pyrefly and pyright errors. Migrate to granian.server.embed.Server, which provides a proper async serve() method designed for running granian inside an existing event loop (exactly our use case in the ServersManager). This also replaces the deprecated 'workers' parameter with 'runtime_threads'. Also removes the temporary pyrefly/pyright suppressions that were added in #4498 to work around these pre-existing type errors.
Summary
Adds web-endpoints-hello — a self-contained, template-ready sample that demonstrates all the ways to expose Genkit flows as network endpoints: REST (ASGI) and gRPC.
Designed to be copied out of the monorepo as a standalone project starter via
./scripts/eject.sh.Highlights
--frameworkgrpc.aioserver with reflection, running in parallel on:50051--serverTellStoryRPC streams chunks via gRPCjust devauto-starts Jaeger./setup.sh+just devgets everything runningjust lintpipeline with ruff, ty, pyrefly, pyright, shellcheck, liccheck, pysentryChanges outside the sample
.github/workflows/python.yml): Addedsample-testsjob that installs and tests samples in isolationbin/check_license,bin/add_license): Ignore generated protobuf stubs (*_pb2.py,*_pb2_grpc.py)py/pyproject.toml): Excludeweb-endpoints-hellofrom workspace-level ty/pyrefly/pyright — the sample has its own lint pipeline with the correct deps. Addedsentry-sdkto workspace lint deps for type resolution.py/samples/README.md, all samplepyproject.tomls): Standardized naming taxonomy and addedrun.shscripts with shared_common.shutilitiespackages/genkit/src/genkit/web/manager/_adapters.py): Switched from syncgranian.Granianto asyncgranian.server.embed.ServerAPIpy/typos.toml): Addedcafto allowlist (substring ofcaféin Latin-1 encoding tests)Architecture
Test plan
just test— 361 unit tests passjust lint— all type checkers and linters pass./run.shstarts both REST (:8080) and gRPC (:50051)./test_endpoints.sh/./test_grpc_endpoints.sh— integration testsbin/lintand tests pass with the sample excluded from workspace-level type checking