Django tutorial
In this tutorial, you’ll build Bargain Chef, a standalone Genkit backend on Django that exposes a recipe-generating flow over HTTP. It uses two AI patterns Genkit simplifies: streaming structured output and tool calling.
The genkit-plugin-django package wires the flow into Django’s async (ASGI) stack with a single decorator, exposing both a JSON endpoint and a Server-Sent Events stream so any client can consume the partial recipe as it arrives, no manual request parsing or streaming code required.
What you’ll build
Section titled “What you’ll build”For each request, your server prompts Gemini to draft a recipe, and the model calls a tool to look up mock grocery sale prices so it can prefer on-sale ingredients. The server streams the recipe back field-by-field as it’s generated, so clients see progress before the full recipe is ready.
You can find the finished code on GitHub.
Prerequisites
Section titled “Prerequisites”- Python 3.11 or later
- uv package manager
This tutorial assumes you’re already familiar with building Django applications.
Set up the application
Section titled “Set up the application”Create the project
Section titled “Create the project”Create a new project with uv and bootstrap Django:
mkdir my-genkit-djangocd my-genkit-djangouv init --no-readme --python 3.11uv run django-admin startproject myproject .uv run python manage.py startapp recipesInstall packages
Section titled “Install packages”uv add django django-cors-headers uvicorn genkit genkit-plugin-django genkit-plugin-google-genaiThese packages include:
django: The Django web framework.django-cors-headers: CORS middleware. Lets browser frontends served from a different origin call the Genkit endpoint.uvicorn: ASGI server that runs Django with async support.genkit: Core Genkit SDK.genkit-plugin-django: Helper that exposes Genkit flows as Django views, including server-sent events for streaming.genkit-plugin-google-genai: Plugin that connects Genkit to Google’s Gemini models.
Install the Genkit CLI, which enables Genkit testing and observability:
curl -sL cli.genkit.dev | bashConfigure a model API key
Section titled “Configure a model API key”This tutorial uses the Gemini API from Google AI Studio:
Get a Gemini API Key
Set the GEMINI_API_KEY environment variable to your key:
export GEMINI_API_KEY=<your API key>Configure Django
Section titled “Configure Django”Update myproject/settings.py so Django loads the recipes app and runs without the default database and admin middleware that Bargain Chef doesn’t need:
INSTALLED_APPS = [ 'django.contrib.contenttypes', 'django.contrib.auth', 'corsheaders', 'recipes',]
MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware',]
# Allows all origins so any browser frontend can call the endpoint during# development. Before deploying, set CORS_ALLOWED_ORIGINS to the origins you serve instead.CORS_ALLOW_ALL_ORIGINS = True
ROOT_URLCONF = 'myproject.urls'
DATABASES = {}Create the backend
Section titled “Create the backend”The backend handles requests from clients. For each request, it prompts Gemini to draft a recipe, lets the model call a tool to look up today’s grocery sale prices, and streams the partial recipe back as it’s generated.
The whole pipeline is a single Genkit flow. A flow is a special Genkit function with built-in observability, type safety, and tooling integration.
You’ll build the backend in four parts:
- Initialize Genkit and register Gemini as the model provider.
- Define a tool the model can call to fetch sale prices.
- Describe the recipe shape with Pydantic so Genkit can validate the final output and stream partial recipe chunks.
- Define the flow that ties everything together.
Create recipes/views.py:
from datetime import datetimefrom typing import Literal
from pydantic import BaseModel, Field
from genkit import ActionRunContext, Genkitfrom genkit.plugins.django import genkit_django_handlerfrom genkit.plugins.google_genai import GoogleAI
ai = Genkit( plugins=[GoogleAI()], model='googleai/gemini-flash-latest',)
class SaleItem(BaseModel): name: str price: str
class GetIngredientsInput(BaseModel): day_type: Literal['weekday', 'weekend'] = Field( description='Whether to fetch weekday or weekend sale prices.' )
@ai.tool( name='get_ingredients_on_sale', description=( 'Returns the ingredients on sale at the local grocery store, with prices. ' 'The sale set differs between weekdays and weekends.' ),)async def get_ingredients_on_sale( input: GetIngredientsInput,) -> list[SaleItem]: # Mock data: in a real app, query a pricing database. if input.day_type == 'weekend': return [ SaleItem(name='chicken breast', price='$2.99/lb'), SaleItem(name='pasta', price='$0.79'), SaleItem(name='canned tomatoes', price='$0.99'), SaleItem(name='garlic', price='$0.50 / head'), SaleItem(name='olive oil', price='$6.99'), ] return [ SaleItem(name='eggs', price='$3.49 / dozen'), SaleItem(name='spinach', price='$1.99'), SaleItem(name='parmesan', price='$4.99'), SaleItem(name='lemons', price='$0.50 each'), SaleItem(name='rice', price='$2.49'), SaleItem(name='butter', price='$3.99'), ]
class RecipeIngredient(BaseModel): name: str quantity: str on_sale: bool
class Recipe(BaseModel): title: str description: str servings: int ingredients: list[RecipeIngredient] steps: list[str]
class BargainChefInput(BaseModel): craving: str = Field(description='What the user feels like eating right now.')
@genkit_django_handler(ai)@ai.flow(name='bargainChefFlow', chunk_type=Recipe)async def bargain_chef_flow( input: BargainChefInput, ctx: ActionRunContext,) -> Recipe: today = datetime.now().strftime('%A')
stream_response = ai.generate_stream( prompt=( f'Today is {today}. The user is craving: {input.craving}.\n\n' 'Call the get_ingredients_on_sale tool with the day_type that matches ' 'today. Saturday and Sunday are weekends; all other days are weekdays. ' 'Then propose ONE recipe that takes advantage of those deals. For each ' "ingredient, set on_sale=true if it appears in the tool's response, " 'false otherwise.' ), tools=[get_ingredients_on_sale], output_schema=Recipe, config={'temperature': 0.7, 'thinkingConfig': {'thinkingLevel': 'MINIMAL'}}, )
async for chunk in stream_response.stream: if chunk.output: ctx.send_chunk(chunk.output)
response = await stream_response.response if not response.output: raise ValueError('Failed to generate recipe') return response.outputA few details are worth noting before you run the backend:
- Final output and streamed chunks:
output_schemais the complete recipe the flow returns at the end.chunk_type=Recipetypes the streamed chunks as partial recipes, because early chunks might only include the title or description. - Shared Pydantic types:
Recipe,RecipeIngredient, andBargainChefInputare defined as Pydantic models, ready for the Django app to import and for Genkit to validate against. - The
get_ingredients_on_saletool: The model decides when to call it based on the prompt, and the typedGetIngredientsInputforces the model to passday_type='weekday'or'weekend'. In a real app, the tool would query a pricing database, inventory system, or third-party API. ctx.send_chunk: Each call pushes the latest partial recipe to the client, giving the UI a typed view of the generated JSON as it grows. After the stream completes, the flow awaitsresponseso the HTTP request still resolves with a validated recipe.- The
@genkit_django_handler(ai)decorator: Stacked on top of@ai.flow, it adapts the flow into an async Django view that parses JSON requests, runs the flow, and emits server-sent events when the client sendsAccept: text/event-stream. It handles both the streaming and non-streaming responses for you, so there’s no request parsing orStreamingHttpResponseplumbing to write.
Wire up the URL
Section titled “Wire up the URL”from django.urls import pathfrom recipes.views import bargain_chef_flow
urlpatterns = [ path('bargainChefFlow', bargain_chef_flow),]The @genkit_django_handler decorator turns bargain_chef_flow into a Django view, so you can point a URL straight at it.
Check the project layout
Section titled “Check the project layout”Verify that your project layout matches the structure below:
- pyproject.toml
- manage.py
Directorymyproject
- settings.py
- urls.py
- asgi.py
Directoryrecipes
- views.py
Optional: install the Genkit agent skills
Section titled “Optional: install the Genkit agent skills”If you’re coding with an AI assistant, install the Genkit Agent Skills so it has structured guidance on Genkit APIs, patterns, and common errors:
npx skills add genkit-ai/skillsSee Develop with AI for tool-specific installation instructions.
Run the app
Section titled “Run the app”Start the Django app with Uvicorn so it can serve async requests and streaming responses:
uv run uvicorn myproject.asgi:application --reloadThe server listens on http://localhost:8000.
Test and inspect the app
Section titled “Test and inspect the app”You can test the flow directly with curl, and you can use the Developer UI to inspect both manual runs and requests from clients.
Send a request with curl
Section titled “Send a request with curl”Once the server is running, use the -N flag and an Accept: text/event-stream header to consume the streamed response:
curl -N -X POST http://localhost:8000/bargainChefFlow \ -H "Content-Type: application/json" \ -H "Accept: text/event-stream" \ -d '{"data":{"craving":"something warm with chicken"}}'The { "data": ... } wrapper is required: Genkit’s HTTP handler reads the flow input from the request body’s data field.
The response arrives as a series of data: events. Each event contains the partial recipe accumulated so far, with fields such as title, ingredients, and steps filling in as the model generates them.
To get a single non-streamed JSON response instead, omit the Accept header:
curl -X POST http://localhost:8000/bargainChefFlow \ -H "Content-Type: application/json" \ -d '{"data":{"craving":"something warm with chicken"}}'Use the Developer UI
Section titled “Use the Developer UI”The Developer UI is Genkit’s local console for testing flows and inspecting execution traces. It runs alongside your backend code, gives you a visual runner for any flow in your project, and records every tool call and model invocation so you can iterate on prompts and debug tool behavior.
-
Start the Developer UI from your project root:
Terminal window genkit start -- uv run uvicorn myproject.asgi:application --reloadThis launches the Developer UI at
http://localhost:4000by default. -
Select
bargainChefFlowfrom the list of flows. -
Enter sample input:
{ "craving": "something warm with chicken" } -
Click Run.
You’ll see the generated recipe, with a trace that builds in real time so you can follow the flow’s progress through each tool call and model invocation.
What you built
Section titled “What you built”You now have a standalone Genkit backend on Django that streams structured output from Gemini over HTTP, calls a tool during generation to ground the model’s response in mock sale-price data, validates input and output against schemas, and surfaces every step in a local trace UI.