A collection of middleware implementations for Genkit.
Project description
Genkit Middleware Plugin
A collection of middleware implementations for Firebase Genkit Python.
Overview
This plugin provides five 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
Quick start
Import the middleware classes you need and pass instances directly into use=[]:
from genkit import Genkit
from genkit.plugins.middleware import Retry, Fallback, Middleware
ai = Genkit(plugins=[Middleware()])
response = await ai.generate(
model='googleai/gemini-flash-latest',
prompt='Hello!',
use=[
Retry(max_retries=5),
Fallback(models=['googleai/gemini-2.5-pro']),
],
)
These pre-packaged middlewares will be available to play with in the Dev UI by default.
Installation
pip install genkit-plugin-middleware
Usage
Retry
Automatically retries model calls on transient failures with configurable exponential backoff:
from genkit.plugins.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-flash-latest',
prompt='Hello!',
use=[retry],
)
Fallback
Falls back to alternative models on retryable errors:
from genkit.plugins.middleware import Fallback
fallback = Fallback(
models=['googleai/gemini-2.5-pro', 'googleai/gemini-flash-latest'],
statuses=['UNAVAILABLE', 'DEADLINE_EXCEEDED'],
)
response = await ai.generate(
model='googleai/gemini-2.5-ultra',
prompt='Hello!',
use=[fallback],
)
ToolApproval
Requires approval before executing tools (useful for sensitive operations):
from genkit.plugins.middleware import ToolApproval
approval = ToolApproval(
allowed_tools=['get_weather', 'search'], # These tools run without approval
)
response = await ai.generate(
model='googleai/gemini-flash-latest',
prompt='Delete the database',
tools=[delete_database_tool],
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 toolApproved
(the middleware only treats explicit dict metadata as approval):
first = await ai.generate(
model='googleai/gemini-flash-latest',
prompt='Delete the database',
tools=[delete_database_tool],
use=[approval],
)
from genkit import restart_tool
response = await ai.generate(
model='googleai/gemini-flash-latest',
prompt='Delete the database',
messages=list(first.messages),
tools=[delete_database_tool],
use=[approval],
resume_restart=restart_tool(
interrupt=first.interrupts[0],
resumed_metadata={'toolApproved': True},
),
)
Skills
Scans directories for SKILL.md files and exposes them as loadable instructions:
from genkit.plugins.middleware import Skills
skills = Skills(
skill_paths=['skills', 'prompts/skills'],
)
response = await ai.generate(
model='googleai/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.plugins.middleware import Filesystem
fs = Filesystem(
root_dir='./workspace',
allow_write_access=True,
tool_name_prefix='',
)
response = await ai.generate(
model='googleai/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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file genkit_plugin_middleware-0.7.0.tar.gz.
File metadata
- Download URL: genkit_plugin_middleware-0.7.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
821482927ca96b9c736ab0d1dc1989da29def3a4a13ea186a90f7156f073a962
|
|
| MD5 |
50b07b9434718f64869879e276a1f74e
|
|
| BLAKE2b-256 |
fdd5de3cf7d9290c26c63a119fdc517c9ff87ca8a8d562a7f3141ecfe7a11628
|
Provenance
The following attestation bundles were made for genkit_plugin_middleware-0.7.0.tar.gz:
Publisher:
publish_python.yml on genkit-ai/genkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
genkit_plugin_middleware-0.7.0.tar.gz -
Subject digest:
821482927ca96b9c736ab0d1dc1989da29def3a4a13ea186a90f7156f073a962 - Sigstore transparency entry: 1780844371
- Sigstore integration time:
-
Permalink:
genkit-ai/genkit@546090f09745c7f9b24a2fbc02ddca2d474c92d6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/genkit-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_python.yml@546090f09745c7f9b24a2fbc02ddca2d474c92d6 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file genkit_plugin_middleware-0.7.0-py3-none-any.whl.
File metadata
- Download URL: genkit_plugin_middleware-0.7.0-py3-none-any.whl
- Upload date:
- Size: 20.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fee53c68869bf8931258692c3ba4b178ca5209fedaf163883384c7f5535660d4
|
|
| MD5 |
27483cb04311db11aef814c475f9f7a1
|
|
| BLAKE2b-256 |
c8b1aacd0e43db84d9f75c8ac8a1abd42606aa990bfd70e859dac18fb0f2be56
|
Provenance
The following attestation bundles were made for genkit_plugin_middleware-0.7.0-py3-none-any.whl:
Publisher:
publish_python.yml on genkit-ai/genkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
genkit_plugin_middleware-0.7.0-py3-none-any.whl -
Subject digest:
fee53c68869bf8931258692c3ba4b178ca5209fedaf163883384c7f5535660d4 - Sigstore transparency entry: 1780844661
- Sigstore integration time:
-
Permalink:
genkit-ai/genkit@546090f09745c7f9b24a2fbc02ddca2d474c92d6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/genkit-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_python.yml@546090f09745c7f9b24a2fbc02ddca2d474c92d6 -
Trigger Event:
workflow_dispatch
-
Statement type: