Skip to main content

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-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-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-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.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for genkit-fastapi 0.10.0
File Size Uploaded
genkit_fastapi-0.10.0.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for genkit-fastapi 0.10.0
File Interpreter ABI Platform
genkit_fastapi-0.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / genkit_fastapi-0.10.0.tar.gz

Download URL genkit_fastapi-0.10.0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
db7b9c1702b41593367af449cb90ac39e424b492b6d75580e9736cd5dfbc4e25
BLAKE2b-256 checksum
How to use checksums
2a8531bc216af938a96184805104c1a6a82f7a33123895563d8cb8802f65c397
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 Aug 20, 2026.

Transparency log

Release files / genkit_fastapi-0.10.0-py3-none-any.whl

Download URL genkit_fastapi-0.10.0-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5702501d0abe335540afb1da93ed051caa2996c308a0e52af79f53d71c2ff9fb
BLAKE2b-256 checksum
How to use checksums
a3fd93426798440d3e7c8147e02d8c1831999e75194c7775d6f9fa7ee695f36b
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 Aug 20, 2026.

Transparency log

Release history Release notifications | RSS feed

0.12.0

2 release files

This release

0.10.0 This release

2 release files

0.9.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page