Genkit Middleware Plugin
A collection of middleware implementations for Genkit Python.
Building with a coding agent? Install the Genkit Python skill first.
npx skills add genkit-ai/skills --skill developing-genkit-pythonIt teaches your agent the current Genkit Python APIs and common gotchas. Source, manual install and skills for other languages: genkit-ai/skills.
Overview
This plugin provides concrete middleware implementations for common use cases:
- Retry: Retries model API calls on transient errors with exponential backoff
- Fallback: Falls back to alternative models when the primary model fails
- ToolApproval: Requires explicit approval before executing tool calls
- Skills: Exposes a library of skills as system prompts and tools
- Filesystem: Provides sandboxed filesystem operations
- Artifacts (experimental,
genkit_middleware.exp): Session artifact listing plus read/write artifact tools
Quick start
Import the middleware classes you need and pass instances directly into use=[]:
from genkit import Genkit
from genkit_google_genai import GoogleAI
from genkit_middleware import Retry, Fallback, Middleware
ai = Genkit(plugins=[GoogleAI(), Middleware()])
response = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='Hello!',
use=[
Retry(max_retries=5),
Fallback(models=['googleai/gemini-3.1-pro-preview']),
],
)
Every middleware works when you pass an instance in use=[]; no plugin
registration is needed. Registering Middleware() lists the stable middleware
in the Dev UI and lets a .prompt file name them in use:.
Installation
uv add genkit-middleware genkit-google-genai
Usage
Retry
Automatically retries model calls on transient failures with configurable exponential backoff:
from genkit_google_genai import GoogleAI
from genkit_middleware import Retry
retry = Retry(
max_retries=3,
statuses=['UNAVAILABLE', 'DEADLINE_EXCEEDED', 'RESOURCE_EXHAUSTED'],
initial_delay_ms=1000,
max_delay_ms=60000,
backoff_factor=2.0,
no_jitter=False, # set True for deterministic backoff (tests)
)
response = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='Hello!',
use=[retry],
)
Fallback
Falls back to alternative models on retryable errors:
from genkit_google_genai import GoogleAI
from genkit_middleware import Fallback
fallback = Fallback(
models=[
'googleai/gemini-3.1-pro-preview',
'googleai/gemini-flash-latest',
],
statuses=['UNAVAILABLE', 'DEADLINE_EXCEEDED'],
)
response = await ai.generate(
model=GoogleAI.gemini('gemini-pro-latest'),
prompt='Hello!',
use=[fallback],
)
ToolApproval
Requires approval before executing tools (useful for sensitive operations):
from pydantic import BaseModel, Field
from genkit_google_genai import GoogleAI
from genkit_middleware import ToolApproval
class DeleteInput(BaseModel):
name: str = Field(description='Database name to delete')
@ai.tool()
async def delete_database(input: DeleteInput) -> str:
return f'Deleted {input.name}'
approval = ToolApproval(
allowed_tools=['get_weather', 'search'], # These tools run without approval
)
first = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='Delete the database',
tools=['delete_database'],
use=[approval],
)
When a non-allowed tool is called, execution is interrupted. Approve and re-run the
tool by restarting it with resumed_metadata that includes tool_approved:
response = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='Delete the database',
messages=list(first.messages),
tools=['delete_database'],
use=[approval],
resume_restart=first.interrupts[0].restart(resumed_metadata={'tool_approved': True}),
)
Skills
Scans directories for SKILL.md files and exposes them as loadable instructions:
from genkit_google_genai import GoogleAI
from genkit_middleware import Skills
skills = Skills(
skill_paths=['skills', 'prompts/skills'],
)
response = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='Help me with Python',
use=[skills],
)
Skills are discovered by scanning for directories containing SKILL.md files. Each SKILL.md can have optional YAML frontmatter:
---
name: python-expert
description: Expert Python programming assistance
---
You are an expert Python programmer...
Filesystem
Provides sandboxed file operations confined to a root directory:
from genkit_google_genai import GoogleAI
from genkit_middleware import Filesystem
fs = Filesystem(
root_dir='./workspace',
allow_write_access=True,
tool_name_prefix='',
)
response = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='List files in the current directory',
use=[fs],
)
Provides four tools:
list_files: List files in a directoryread_file: Read file contentwrite_file: Write to a file (requiresallow_write_access=True)edit_file: Edit file with string replacements (requiresallow_write_access=True)
Artifacts
Exposes read_artifact / write_artifact tools and lists session artifacts in the
system prompt. It only works inside an agent, so it's experimental and lives in
genkit_middleware.exp. Pass it straight to the agent; Middleware() doesn't
register it, so it isn't listed in the Dev UI and a .prompt file can't name it:
from genkit_middleware import Middleware
from genkit_middleware.exp import Artifacts
from genkit.exp import Genkit
from genkit.exp.agent import InMemorySessionStore
from genkit_google_genai import GoogleAI
ai = Genkit(plugins=[GoogleAI(), Middleware()])
agent = ai.define_agent(
name='workspaceAgent',
model=GoogleAI.gemini('gemini-flash-latest'),
use=[Artifacts()],
store=InMemorySessionStore(),
)
chat = agent.chat()
await chat.send('Write poem.txt with a short poem about Python agents.')
# chat.artifacts now includes poem.txt
Metadata
Release files for genkit-middleware 0.13.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 | Provenance | Actions |
|---|---|---|---|---|
| genkit_middleware-0.13.0.tar.gz | 33.3 kB | With provenance | Details for genkit_middleware-0.13.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| genkit_middleware-0.13.0-py3-none-any.whl | Python 3 | none | any | Details for genkit_middleware-0.13.0-py3-none-any.whl |
Total release size: 60.0 kB
Release files / genkit_middleware-0.13.0.tar.gz
| Download URL | genkit_middleware-0.13.0.tar.gz |
|---|---|
| Size | 33.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
348160a73648b8dbf04b188f0c532122f8fd6fcfcb222552825cec7ba8ead604
|
|
BLAKE2b-256 checksum How to use checksums |
a8f2a157a7457eac40f7f966887f7a39ea1e2fa0e9e7f890f72a04a9fd736c32
|
| 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 Nov 5, 2026.
Transparency logRelease files / genkit_middleware-0.13.0-py3-none-any.whl
| Download URL | genkit_middleware-0.13.0-py3-none-any.whl |
|---|---|
| Size | 26.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6382a3945d9957660130ffec854041e0c413fc85e2c48fd280a5a50334c5fd4d
|
|
BLAKE2b-256 checksum How to use checksums |
5b656fdbdb833981f585fcd22999a938c8b20a8431ee10b715d9a370f8a037f8
|
| 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 Nov 5, 2026.
Transparency logSecurity for genkit-middleware 0.13.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for genkit_middleware-0.13.0.tar.gz
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 Nov 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.