Genkit Python SDK v0.7.0 Release Notes
Genkit Python SDK v0.7.0 is here! In this release, we've focused on giving you more control over the generation pipeline, adding pre-built resilience tools, and making it easier to integrate Genkit into your production web applications.
What's New
Intercept the Generation Loop with Middleware (#5253)
We've added a pluggable middleware architecture to the core generate pipeline. By subclassing BaseMiddleware and using the @ai.middleware decorator, you can run custom code at key lifecycle points in the generation process.
Configure your middleware with a custom configuration model using Pydantic:
from pydantic import BaseModel
from genkit import Genkit, BaseMiddleware
ai = Genkit()
# Configure your middleware with a custom config using Pydantic
class LoggingConfig(BaseModel):
prefix: str = "[AI]"
@ai.middleware(name="logger")
class Logger(BaseMiddleware[LoggingConfig]):
# Wrap the outer loop of the generate call (includes tools)
async def wrap_generate(self, params, ctx, next_fn):
return await next_fn(params, ctx)
# Wrap the raw model API call (inspect/mutate inputs/outputs)
async def wrap_model(self, params, ctx, next_fn):
print(f"{self.config.prefix} Running: {params.prompt}")
res = await next_fn(params, ctx)
print(f"{self.config.prefix} Response: {res.text}")
return res
# Wrap individual tool execution (inspect/mutate inputs/outputs or Interrupt)
async def wrap_tool(self, params, ctx, next_fn):
print(f"Tool {params.name} called with: {params.input}")
return await next_fn(params, ctx)
# Apply it on generate calls with full IDE autocomplete:
await ai.generate(
model='gemini-2.5-flash',
prompt='Hello!',
use=[Logger(prefix="[Veneer]")]
)- wrap_generate: Hooks into the outer loop of the generate call, wrapping both the model execution and subsequent tool calls.
- wrap_model: Wraps the raw model API call, allowing you to inspect or mutate model input parameters and the raw generated output.
- wrap_tool: Wraps individual tool executions. You can inspect or modify inputs/outputs of tool calls, or raise an
Interruptto pause execution (e.g., waiting for user approval).
Pre-built, Production-Ready Middleware (#5253)
We also released an official genkit-plugin-middleware package so you don't have to write common resilience patterns from scratch:
- Smart Retries: Handle transient API failures with exponential backoff and randomized jitter (
Retry). - Model Fallbacks: Automatically swap to backup models if a primary provider fails or hits rate limits (
Fallback). - Tool Approval: Intercept sensitive tool calls to require user confirmation or automated validation (
ToolApproval, supporting snake_case config options from #5479). - Filesystem Sandbox: Restrict agent actions to a sandboxed directory with secure file operations like read, write, edit, and list (
Filesystem). - File System Skills: Bundle and execute groups of tools backed by simple file storage (
Skills).
Expose AI Logic as HTTP Endpoints with Django (#5408)
To make it easier to deploy AI workloads, we built genkit-plugin-django to let you expose Genkit flows as standard Django endpoints. You can find a complete example showing how to wire this up in the new django-hello sample folder.