# Genkit Python SDK
Genkit is a framework for building AI-powered applications with type-safe flows, structured outputs, and integrated observability. This is the Python implementation that maintains feature parity with the JavaScript/TypeScript SDK.
## Architecture Overview
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Your Application β
β β
β from genkit import Genkit β
β ai = Genkit(plugins=[GoogleGenAI()], model=gemini_2_0_flash) β
β β
β βββββββββββ βββββββββββ βββββββββββ ββββββββββββ βββββββββββββ β
β β Flows β β Tools β β Prompts β β Embeddersβ β Retrieversβ β
β β@ai.flow β β@ai.tool β β.prompt β βai.embed()β βai.retrieveβ β
β ββββββ¬βββββ ββββββ¬βββββ ββββββ¬βββββ ββββββ¬ββββββ βββββββ¬ββββββ β
β β β β β β β
β ββββββββββββββ΄βββββββββββββ΄ββββββββββββββ΄ββββββββββββββ β
β β β
β βββββββββΌβββββββββ β
β β Registry β β
β β (all actions) β β
β βββββββββ¬βββββββββ β
β β β
β ββββββββββββββββββββββΌβββββββββββββββββββββ β
β βΌ βΌ βΌ β
β ββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Plugins β β Tracing β β Reflection β β
β β (providers) β β (OpenTelemetryβ β API (DevUI) β β
β ββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## Plugin Architecture
Plugins are the primary extension mechanism in Genkit. They add model
providers, telemetry backends, vector stores, and other capabilities.
Every plugin implements the same abstract interface (`Plugin` base class)
and is loaded lazily by the `Registry`.
### Plugin Class Hierarchy
All plugins inherit from `genkit.core.plugin.Plugin` and implement three
abstract methods:
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Plugin (Abstract Base Class) β
β genkit.core.plugin.Plugin β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β name: str β
β βββββββββββββββββββββββββββββββββββββ β
β Plugin namespace (e.g., 'googleai', 'anthropic', 'ollama') β
β β
β async init() β list[Action] β called once per plugin β
β βββββββββββββββββββββββββββββββββββββ β
β One-time initialization; returns actions to pre-register. β
β Called lazily on first action resolution, NOT at registration. β
β β
β async resolve(kind, name) β Action? β called per lookup β
β βββββββββββββββββββββββββββββββββββββ β
β Resolve a single action by kind and name. Returns None if β
β this plugin doesn't handle the requested action. β
β β
β async list_actions() β list[ActionMetadata] β for Dev UI β
β βββββββββββββββββββββββββββββββββββββ β
β Advertise available actions without heavy initialization. β
β Called by the Reflection API for DevUI action discovery. β
β β
β model(name) β ModelReference β helper method β
β embedder(name) β EmbedderRef β helper method β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β GoogleAI β β Anthropic β β Ollama β ... etc.
β name = β β name = β β name = β
β 'googleai' β β 'anthropic' β β 'ollama' β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
```
### Plugin Lifecycle
Plugins go through four phases: registration, lazy initialization,
action resolution, and action discovery.
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Plugin Lifecycle β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Phase 1: REGISTRATION (at Genkit startup) β
β βββββββββ β
β ai = Genkit(plugins=[GoogleAI(), Anthropic()]) β
β β β
β βββΊ registry.register_plugin(GoogleAI()) β
β βββΊ registry.register_plugin(Anthropic()) β
β β β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββ β
β β Registry._plugins β β
β β ββββββββββββββββββ¬ββββββββββββββββββββ β β
β β β "googleai" β GoogleAI instance β β β
β β β "anthropic" β Anthropic instanceβ β β
β β ββββββββββββββββββ΄ββββββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββββββββββ β
β β
β Phase 2: LAZY INIT (on first action resolution) β
β ββββββββββ β
β await ai.generate(model='googleai/gemini-2.0-flash', ...) β
β β β
β βΌ β
β registry._ensure_plugin_initialized('googleai') β
β β β
β βΌ β
β actions = await plugin.init() β called exactly once β
β β (subsequent calls are no-ops) β
β βΌ β
β for action in actions: β
β registry.register_action_instance(action) β
β β
β Phase 3: ACTION RESOLUTION (on each usage) β
β βββββββββββββββββββββ β
β await plugin.resolve(ActionKind.MODEL, 'googleai/gemini-2.0-flash')β
β β β
β βΌ β
β Action instance returned and cached in registry β
β β
β Phase 4: ACTION DISCOVERY (for Dev UI) β
β ββββββββββββββββββ β
β await plugin.list_actions() β
β β β
β βΌ β
β ActionMetadata[] returned to Reflection API β
β (does NOT trigger init β must be fast and safe) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
### How the Registry Resolves Actions
When you call `ai.generate(model='googleai/gemini-2.0-flash')`, the
registry uses a multi-step resolution algorithm:
```
ai.generate(model="googleai/gemini-2.0-flash")
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Step 1: CACHE HIT? β
β Is "googleai/gemini-2.0-flash" already in registry._entries? β
β βββ YES β return cached Action (fast path) β
β βββ NO β continue to Step 2 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Step 2: NAMESPACED or UNPREFIXED? β
β Does the name contain "/"? β
β β β
β βββ YES ("googleai/gemini-2.0-flash") β
β β βββ Find plugin "googleai" β
β β βββ await _ensure_plugin_initialized("googleai") β
β β βββ Check cache again (init may have registered it) β
β β βββ await plugin.resolve(MODEL, "googleai/gemini-2.0") β
β β β
β βββ NO ("gemini-2.0-flash") β
β βββ Try ALL plugins β
β βββ If 1 match β use it β
β βββ If 2+ match β ValueError (ambiguous) β
β βββ If 0 match β continue to Step 3 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Step 3: DYNAMIC ACTION PROVIDERS (fallback) β
β Try registered Dynamic Action Providers (e.g., MCP servers) β
β βββ Found β register and return β
β βββ Not found β return None β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
### Writing a Custom Plugin
```python
from genkit.core.plugin import Plugin
from genkit.core.action import Action, ActionMetadata
from genkit.core.action.types import ActionKind
class MyPlugin(Plugin):
name = 'myplugin'
def __init__(self, api_key: str) -> None:
self.api_key = api_key
async def init(self) -> list[Action]:
# Return actions to pre-register (optional)
return []
async def resolve(self, kind: ActionKind, name: str) -> Action | None:
if kind == ActionKind.MODEL:
return self._create_model(name)
return None
async def list_actions(self) -> list[ActionMetadata]:
return [
ActionMetadata(kind=ActionKind.MODEL, name='myplugin/my-model'),
]
# Usage:
ai = Genkit(plugins=[MyPlugin(api_key='...')])
response = await ai.generate(model='myplugin/my-model', prompt='Hello!')
```
## How a Flow Executes
```
ai.generate(prompt="Tell me a joke")
β
βΌ
ββββββββββββ βββββββββββββ ββββββββββββ ββββββββββββ
β 1. Flow βββββΊβ 2. Model βββββΊβ 3. Tool? βββββΊβ 4. Model β
β starts β β called β β (if the β β responds β
β tracing β β (Gemini, β β model β β with β
β β β Claude, β β decides β β final β
β β β etc.) β β to use β β answer β
β β β β β one) β β β
ββββββββββββ βββββββββββββ ββββββββββββ ββββββββββββ
β β β β
ββββββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββ
β
βββββββββββββΌββββββββββββ
β Trace (every step β
β recorded for DevUI, β
β Cloud Trace, etc.) β
ββββββββββββββββββββββββ
```
## Deploying Flows
Flows are just functions. You choose **how** to expose them over HTTP:
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Two Ways to Serve Flows β
β β
β Option A: Built-in Flow Server Option B: Framework Adapter β
β (zero config, all flows exposed) (full control, per-route) β
β β
β app = create_flows_asgi_app( from flask import Flask β
β registry=ai.registry, app = Flask(__name__) β
β context_providers=[ β
β api_key('my-secret'), @app.route('/joke') β
β ], @genkit_flask_handler(ai) β
β ) @ai.flow() β
β async def joke(topic): β
β # Exposes ALL flows as: return ai.generate(...) β
β # POST /tell_joke β
β # POST /summarize β
β # POST /translate β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Deploy Anywhere β β
β β Cloud Run Β· Firebase Β· Fly.io Β· AWS Β· Azure Β· K8s Β· Bare Metal β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## Context Providers and Authentication
Context providers run **before** your flow, reading the HTTP request
and deciding whether to allow or reject it:
```
curl POST /tell_joke -H "Authorization: my-secret-key"
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Flow Server β
β β
β 1. Parse request body β
β β β
β βΌ β
β 2. Context providers (one by one): β
β β β
β βββ api_key('my-secret') β
β β βββ β
Key matches β {auth: {api_key: β¦}} β
β β βββ β Wrong key β 403 Permission Deniedβ
β β βββ β No key β 401 Unauthenticated β
β β β
β βββ (more providers, if any β results merge) β
β β β
β βΌ β
β 3. Run flow with merged context β
β tell_joke("banana", context={auth: {api_key:β¦}})β
β β β
β βΌ β
β 4. Return response β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## Directory Structure
```
py/
βββ packages/genkit/ # Core Genkit framework package
βββ plugins/ # Official plugins
β βββ amazon-bedrock/ # Amazon Bedrock models + X-Ray telemetry (community)
β βββ anthropic/ # Claude models
β βββ azure/ # Azure AI telemetry (community)
β βββ cloudflare-workers-ai/# Cloudflare Workers AI + OTLP telemetry (community)
β βββ checks/ # Safety guardrails
β βββ cohere/ # Cohere models (community)
β βββ compat-oai/ # OpenAI-compatible APIs
β βββ deepseek/ # DeepSeek models
β βββ dev-local-vectorstore/# Local development vector store
β βββ evaluators/ # RAGAS and custom evaluators
β βββ firebase/ # Firebase integration + telemetry
β βββ flask/ # Flask HTTP endpoints
β βββ google-cloud/ # GCP telemetry (Cloud Trace, Logging)
β βββ google-genai/ # Gemini, Imagen, Veo, Lyria, TTS
β βββ huggingface/ # HuggingFace Inference API
β βββ mcp/ # Model Context Protocol
β βββ microsoft-foundry/ # Azure AI Foundry (11,000+ models) (community)
β βββ mistral/ # Mistral models
β βββ observability/ # 3rd party telemetry (Sentry, Datadog, etc.)
β βββ ollama/ # Local Ollama models
β βββ vertex-ai/ # Model Garden + Vector Search
β βββ xai/ # Grok models
βββ samples/ # Sample applications
βββ testapps/ # Test applications
```
## Setup Instructions
1. Install `uv` from https://docs.astral.sh/uv/getting-started/installation/
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```
2. Install required tools using `uv`
```bash
uv tool install httpie
uv tool install mypy
uv tool install ruff
```
3. If you are using VSCode, install the `Ruff` extension from the marketplace to add linter support.
## Quick Start
```python
from genkit import Genkit
from genkit.plugins.google_genai import GoogleGenAI, gemini_2_0_flash
ai = Genkit(
plugins=[GoogleGenAI()],
model=gemini_2_0_flash,
)
response = await ai.generate(prompt="Tell me a joke")
print(response.text)
```
## Genkit Concepts
| Concept | What It Is (ELI5) | Where to Learn More |
|---------|-------------------|---------------------|
| **Genkit** | The main "control center" β you create one and use it to define flows, tools, and prompts. Think of it as the brain of your AI app. | `Genkit()` constructor |
| **Flow** | A function that does AI work. It calls models, uses tools, and its execution is fully traced so you can debug it. Flows are the unit you deploy. | `@ai.flow()` |
| **Model** | An AI model (Gemini, Claude, GPT, Llama, etc.) that generates text, images, or other content. You call it with `ai.generate()`. | `ai.generate()` |
| **Tool** | A function that a model can *choose* to call. For example, "look up the weather" or "search the database." You define it, and the model decides when to use it. | `@ai.tool()` |
| **Prompt** | A template for what to say to a model. Can include variables, system instructions, and output schemas. `.prompt` files use Handlebars syntax. | `ai.prompt()`, `.prompt` files |
| **Plugin** | A package that adds capabilities β model providers (Google, Anthropic), telemetry (Cloud Trace), vector stores, etc. You plug them in when creating `Genkit`. | `Genkit(plugins=[...])` |
| **Context Provider** | Middleware that runs *before* a flow is called via HTTP. It reads the request (headers, body) and either provides auth info to the flow or rejects the request. | `api_key()`, `create_flows_asgi_app()` |
| **Flow Server** | A built-in HTTP server that wraps your flows as API endpoints so `curl` (or any client) can call them. It's Genkit's simple way to deploy flows without a web framework. | `create_flows_asgi_app()` |
| **Registry** | The internal directory of all defined flows, tools, models, and prompts. The Dev UI and CLI read it to discover what's available. | `ai.registry` |
| **Action** | The low-level building block behind flows, tools, models, and prompts. Everything you define becomes an "action" in the registry with input/output schemas and tracing. | `genkit.core.action` |
| **Middleware** | Functions that wrap around model calls to add behavior β logging, caching, safety checks, or modifying requests/responses. Runs at the model level, not HTTP level. | `ai.define_model(use=[...])` |
| **Embedder** | A model that turns text into numbers (vectors) for similarity search. Used with vector stores for RAG (Retrieval-Augmented Generation). | `ai.embed()` |
| **Retriever** | A component that searches a vector store and returns relevant documents for a query. Used in RAG pipelines. | `ai.retrieve()` |
| **Indexer** | A component that stores documents into a vector store. The opposite of a retriever β it writes, while the retriever reads. | `ai.index()` |
| **Evaluator** | A tool that scores AI outputs for quality, safety, or correctness. Used for automated testing of AI behavior. | `ai.evaluate()` |
| **Dotprompt** | Genkit's prompt file format (`.prompt` files). Combines Handlebars templates + YAML frontmatter for model config, schemas, and few-shot examples. | `prompts/` directory |
| **Tracing** | Automatic recording of every step in a flow β model calls, tool invocations, timings. Visible in the Dev UI and exportable to Cloud Trace, Jaeger, etc. | OpenTelemetry integration |
| **Reflection API** | An internal HTTP/WebSocket API that lets the Dev UI inspect the registry, trigger actions, and stream results. Only active in development mode. | Dev UI (`genkit start`) |
| **Dynamic Action Provider** | A plugin that can register actions at runtime (not just at startup). Example: MCP servers that expose tools on-demand. | `ai.define_dynamic_action_provider()` |
| **Structured Output** | Asking a model to return data in a specific format (JSON matching a Pydantic model or schema). Genkit validates the output for you. | `ai.generate(output_schema=...)` |
## Plugin Categories
| Category | Plugins | Purpose |
|----------|---------|---------|
| **Model Providers** | google-genai, anthropic, amazon-bedrock, ollama, compat-oai, deepseek, xai, mistral, huggingface, microsoft-foundry, cloudflare-workers-ai, cohere | AI model access |
| **Telemetry** | google-cloud, amazon-bedrock, azure, firebase, cloudflare-workers-ai, observability | Distributed tracing & logging |
| **Vector Stores** | firebase, vertex-ai, dev-local-vectorstore | Embeddings storage & retrieval |
| **Safety** | checks, evaluators | Guardrails & evaluation |
| **Integrations** | flask, mcp | HTTP endpoints, tool protocols |
## Community Plugins
Some plugins are community-maintained and supported on a best-effort basis:
- **amazon-bedrock** - Amazon Bedrock models + AWS X-Ray telemetry
- **azure** - Azure Monitor / Application Insights telemetry
- **cloudflare-workers-ai** - Cloudflare Workers AI models + OTLP telemetry
- **cohere** - Cohere command models + reranking
- **microsoft-foundry** - Azure AI Foundry (11,000+ models)
- **observability** - Third-party backends (Sentry, Honeycomb, Datadog, etc.)
## Sample Catalog
Each sample demonstrates specific Genkit concepts. Use this table to find
examples of any feature:
| Sample | Key Concepts | Description |
|--------|-------------|-------------|
| **Model Providers** | | |
| `provider-google-genai-hello` | Model, Flow | Basic Gemini model usage |
| `provider-anthropic-hello` | Model, Flow | Claude model usage |
| `provider-ollama-hello` | Model, Flow | Local Ollama models |
| `provider-cohere-hello` | Model, Flow | Cohere models |
| `provider-compat-oai-hello` | Model, Flow | OpenAI-compatible APIs |
| `provider-deepseek-hello` | Model, Flow | DeepSeek models |
| `provider-xai-hello` | Model, Flow | Grok models |
| `provider-mistral-hello` | Model, Flow | Mistral models |
| `provider-huggingface-hello` | Model, Flow | HuggingFace Inference API |
| `provider-amazon-bedrock-hello` | Model, Flow, Telemetry | AWS Bedrock + X-Ray tracing |
| `provider-cloudflare-workers-ai-hello` | Model, Flow, Telemetry | Cloudflare Workers AI |
| `provider-microsoft-foundry-hello` | Model, Flow | Azure AI Foundry |
| **Google Cloud** | | |
| `provider-google-genai-vertexai-hello` | Model, Flow | Vertex AI models |
| `provider-google-genai-vertexai-image` | Model, Flow | Imagen image generation |
| `provider-google-genai-media-models-demo` | Model, Flow | TTS, STT, Veo, Lyria |
| `provider-google-genai-code-execution` | Model, Tool | Code execution sandbox |
| `provider-google-genai-context-caching` | Model, Flow | Context caching for long prompts |
| `provider-vertex-ai-model-garden` | Model, Flow | Model Garden access |
| `provider-vertex-ai-rerank-eval` | Retriever, Evaluator | Reranking + evaluation |
| `provider-vertex-ai-vector-search-*` | Embedder, Indexer, Retriever | Vector search |
| `provider-firestore-retriever` | Retriever | Firestore document retrieval |
| `provider-observability-hello` | Telemetry | Multi-backend tracing |
| **Framework Patterns** | | |
| `framework-prompt-demo` | Prompt, Dotprompt | Advanced prompt templates |
| `framework-format-demo` | Structured Output | JSON/enum output formatting |
| `framework-context-demo` | Context Provider, Flow | Auth context in flows |
| `framework-middleware-demo` | Middleware | Model-level request/response hooks |
| `framework-evaluator-demo` | Evaluator | Custom evaluation metrics |
| `framework-restaurant-demo` | Flow, Tool, Prompt | Multi-step agent with tools |
| `framework-dynamic-tools-demo` | Tool, Dynamic Action Provider | Runtime tool registration |
| `framework-tool-interrupts` | Tool | Human-in-the-loop tool approval |
| `framework-realtime-tracing-demo` | Tracing | Live trace streaming |
| `dev-local-vectorstore-hello` | Embedder, Retriever, Indexer | Local dev vector store |
| **Deployment** | | |
| `web-short-n-long` | Flow Server, Context Provider | Built-in flows server + `api_key()` auth |
| `web-flask-hello` | Flow, Plugin (Flask) | Flask framework integration |
| `web-endpoints-hello` | Flow Server, Security | Production ASGI + gRPC deployment |
| `web-multi-server` | Flow Server, Reflection API | Multiple Genkit instances |
See the [samples/README.md](samples/README.md) for running instructions.
## Running Tests
Run all unit tests:
```bash
uv run pytest .
```
Run tests for a specific plugin:
```bash
uv run pytest plugins/amazon-bedrock/
```
## Development
See [GEMINI.md](GEMINI.md) for detailed development guidelines, including:
- Code quality and linting requirements
- Type checking configuration
- Testing conventions
- Documentation standards
## License
Apache 2.0