Genkit FastAPI Plugin
Expose Genkit flows and agents as HTTP endpoints in a FastAPI application.
Installation
uv add genkit-fastapi genkit-google-genai
Usage
Serving Flows (serve_flow)
Use serve_flow to register a Genkit flow as a FastAPI endpoint:
from fastapi import FastAPI
from genkit import Genkit
from genkit_fastapi import serve_flow
from genkit_google_genai import GoogleAI
ai = Genkit(plugins=[GoogleAI()], model=GoogleAI.gemini_model('gemini-flash-latest'))
app = FastAPI()
@ai.tool(description='Get current weather for a location')
async def get_weather(location: str) -> str:
return f'Sunny in {location}'
@ai.flow()
async def chat_flow(prompt: str) -> str:
response = await ai.generate(
prompt=prompt,
tools=[get_weather],
)
return response.text
# Mount flow endpoint at POST /api/chat_flow
app.include_router(serve_flow(chat_flow), prefix='/api')
Serving Agents (serve_agent)
Use serve_agent to expose an agent as FastAPI routes (including /getSnapshot and /abort endpoints when session state storage is enabled):
from fastapi import FastAPI
from genkit import Genkit
from genkit_fastapi import serve_agent
from genkit_google_genai import GoogleAI
ai = Genkit(plugins=[GoogleAI()], model=GoogleAI.gemini_model('gemini-flash-latest'))
app = FastAPI()
@ai.tool(description='Get current weather for a location')
async def get_weather(location: str) -> str:
return f'Sunny in {location}'
weather_agent = ai.define_agent(
name='weatherAgent',
model=GoogleAI.gemini_model('gemini-flash-latest'),
system='You are a helpful weather assistant.',
tools=[get_weather],
)
# Mount agent turn route at POST /api/weatherAgent (plus /getSnapshot and /abort companion endpoints)
app.include_router(serve_agent(weather_agent), prefix='/api')
Custom Base Path & Dependency Injection
Customize the base path or resolve request context using FastAPI's dependency injection system:
from fastapi import Depends, Header
from genkit_fastapi import serve_flow
async def user_context(authorization: str = Header(...)):
return {'uid': parse_token(authorization)}
app.include_router(
serve_flow(
chat_flow,
base_path='/chat',
context_dependency=user_context,
),
prefix='/api',
)
Decorator Handler (genkit_fastapi_handler)
For custom route definitions, you can also use @genkit_fastapi_handler:
from genkit_fastapi import genkit_fastapi_handler
@app.post('/custom-chat', response_model=None)
@genkit_fastapi_handler(ai)
@ai.flow()
async def custom_chat(prompt: str) -> str:
response = await ai.generate(
prompt=prompt,
tools=[get_weather],
)
return response.text
Running
# With Genkit Dev UI
genkit start -- uvicorn main:app --reload
# Production (no Dev UI)
uvicorn main:app
Streaming
Endpoints automatically support streaming when the client sends Accept: text/event-stream or specifies ?stream=true:
curl -X POST http://localhost:8000/api/chat_flow \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{"data": "Tell me a joke"}'
Release files for genkit-fastapi 0.11.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 | |
|---|---|---|---|
| genkit_fastapi-0.11.0.tar.gz | 14.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| genkit_fastapi-0.11.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.2 kB
Release files / genkit_fastapi-0.11.0.tar.gz
| Download URL | genkit_fastapi-0.11.0.tar.gz |
|---|---|
| Size | 14.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc00045c9745c51aca63a96a6b5391c9c40c6994967022c77ba56258a10d99f9
|
|
BLAKE2b-256 checksum How to use checksums |
9e12e4b26af4b0d2e1991b67c48e4ce0d90981dbf62b293aa6fda08a203b0510
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 3, 2026.
Transparency logRelease files / genkit_fastapi-0.11.0-py3-none-any.whl
| Download URL | genkit_fastapi-0.11.0-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8961592b849831139a4622c360124e8ec1c48fe5d5deda421fa89d0de1b639cd
|
|
BLAKE2b-256 checksum How to use checksums |
a9b7ce6ceaa9a4ca0482e25348e93fb5dbdf7c4e21a177c3045109af4c872160
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 3, 2026.
Transparency log