Index of every page on this site: llms.txt
All skills
genkit-ai avatar

/developing-genkit-python

@d12c988
by Genkitgenkit-ai/skills40 stars
13
Run it yourself

Provides guidance on developing AI-powered applications using Genkit Python (0.13) and migrating Genkit Python 0.12 projects to 0.13. Use when building AI agents, flows, tools, structured outputs, or FastAPI routes in Python, upgrading from Genkit 0.12, or debugging Genkit imports, config errors, or traces. Don't use for Genkit in JavaScript/TypeScript, Go, or Dart.

Use this Skill: https://skilld.dev/gh/genkit-ai/skills/developing-genkit-python

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesdotprompt.md

≈1.7k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Dotprompt — Genkit Python


What It Is

.prompt files pair YAML frontmatter (model, config, input/output schemas, tools, middleware) with Handlebars templates. Handlebars templates only render inside .prompt files and ai.define_prompt(...) — strings passed to ai.generate(prompt=..., system=...) are sent to the model verbatim.

File Format

Place .prompt files inside prompts/ and pass prompt_dir=... to Genkit(...):

---
model: googleai/gemini-flash-latest
config:
  temperature: 0.4
input:
  default:
    tone: practical
  schema:
    food: string
    tone?: string                 # optional scalar - `field?: type`
    ingredients?(array): string   # optional array/object uses parenthetical syntax
output:
  schema: Recipe                  # references a schema registered via ai.define_schema()
  format: json
---
{{role "system"}}
You are a chef. Keep recipes {{tone}}.

{{role "user"}}
Generate a recipe for {{food}}.
{{#if ingredients}}
Prefer these ingredients:
{{list ingredients}}
{{/if}}

Use {{role "system"}} and {{role "user"}} to emit distinct message roles; a template without {{role ...}} becomes a single user message.

Input & output validation:

  • Prompt inputs are validated against input.schema (or input_schema= on define_prompt) before calling the model, and input.default values from .prompt frontmatter are filled in automatically.
  • When output.schema names a Pydantic class registered with ai.define_schema('Recipe', Recipe), response.output is validated and returned directly as Recipe | None (None when the model's JSON is truncated or malformed; check response.finish_reason).

Python Setup

from pathlib import Path
from pydantic import BaseModel
from genkit import Genkit, Prompt
from genkit_google_genai import GoogleAI

ai = Genkit(
    plugins=[GoogleAI()],
    model=GoogleAI.gemini('gemini-flash-latest'),
    prompt_dir=Path(__file__).resolve().parent.parent / 'prompts',
)


class Recipe(BaseModel):
    title: str
    steps: list[str]


ai.define_schema('Recipe', Recipe)

Calling a Prompt (Prompt)

ai.prompt(...) and ai.define_prompt(...) return a Prompt. There is no .execute() method — call and await the Prompt directly:

recipe_prompt: Prompt = ai.prompt('recipe')

# Call options use the same keyword names as `ai.generate`.
# `config` merges key-by-key over the prompt's frontmatter config.
# `output_schema` is fixed by the prompt and cannot be overridden per call.
response = await recipe_prompt(
    {'food': 'banana bread', 'ingredients': ['walnuts', 'cinnamon']},
    config={'temperature': 0.2},
)
if response.output is not None:
    recipe: Recipe = response.output

# Variant (`prompts/recipe.quick.prompt`)
quick_res = await ai.prompt('recipe', variant='quick')({'food': 'banana bread'})

Code-Defined Prompts (ai.define_prompt)

class GreetingInput(BaseModel):
    name: str


greet = ai.define_prompt(
    name='greet',
    input_schema=GreetingInput,
    prompt='Say hello to {{name}}.',
)
res = await greet(GreetingInput(name='Ana'))

Streaming from a Prompt

prompt.stream(...) is synchronous (do not await it); iterate .stream and await .response:

from genkit import FlowContext

@ai.flow()
async def stream_recipe(food: str, ctx: FlowContext[str]) -> str:
    sr = ai.prompt('recipe').stream({'food': food})
    chunks: list[str] = []
    async for chunk in sr.stream:
        if chunk.text:
            ctx.send_chunk(chunk.text)
            chunks.append(chunk.text)
    final = await sr.response
    return final.text or ''.join(chunks)

Render Without Generating

render(...) takes the same arguments as a call and returns the GenerateActionOptions (model, config, messages, output schema) without calling a model. Use it to debug templates and input defaults:

rendered = await ai.prompt('recipe').render({'food': 'focaccia'})
print(rendered.model, rendered.config)
# => googleai/gemini-flash-latest {'temperature': 0.4}
for message in rendered.messages:
    print(message.role, message.text.strip())
# => system You are a chef. Keep recipes practical.
# => user Generate a recipe for focaccia.

Helpers

Register Handlebars helpers with ai.define_helper(name, fn). Dotprompt calls every helper as fn(params, options): params is the list of positional template arguments, options carries hash arguments. A one-argument function raises TypeError at render time.

from typing import Any


def list_helper(params: list[Any], options: Any) -> str:
    items = params[0] if params else None
    if not isinstance(items, list):
        return ''
    return '\n'.join(f'- {item}' for item in items)


ai.define_helper('list', list_helper)

Built-in helpers: role, json, media, history, section, ifEquals, unlessEquals.

Use {{list ingredients}} in the .prompt body.

Variants and Partials

  • Variants: save <name>.<variant>.prompt (e.g. recipe.vegan.prompt) and load with ai.prompt('recipe', variant='vegan').
  • Partials: prefix partial files with _ (e.g. prompts/_disclaimer.prompt) or register with ai.define_partial('disclaimer', '...'), then include via {{>disclaimer}} or {{>disclaimer region=region}}.

Prompts with Tools and Middleware

  • Tools: list registered tool names under tools: in YAML frontmatter, or pass tools=[lookup_rate] when calling await prompt(input, tools=[lookup_rate]). The tool loop stops after maxTurns (frontmatter) or max_turns= (call), default 50.
  • Middleware: pass instances at call time with use=[Retry(max_retries=2)]. To name middleware under use: in frontmatter, register it first: add Middleware() from genkit_middleware to Genkit(plugins=[...]) (registers retry, fallback, tool_approval, skills, filesystem) or define your own with @ai.middleware. An unregistered name fails with NOT_FOUND.
---
model: googleai/gemini-flash-latest
tools: [lookup_rate]
use:
  - name: retry
    config:
      max_retries: 2
---
Convert {{amount}} USD to EUR.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts14d3 checks · Risk SAFE
  • Gen Agent Trust Hub14d

    The skill provides comprehensive documentation and code examples for developing AI applications with Genkit Python. It includes instructions for setting up environment variables and installing packages from trusted registries (NPM, PyPI). While it describes building agents that process untrusted user data, which creates a potential surface for indirect prompt injection, this is inherent to the technology described and is documented alongside safety features like human-in-the-loop approval.

  • Socket14d

    No alerts

  • Snyk14d

    Risk: LOW · No issues

Signed by skilld at d12c988. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 days ago.

Activeupdated 2 weeks ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}

README badge

README badge for genkit-ai/skills/developing-genkit-python