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

/developing-genkit-go

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

Provides guidance on developing AI-powered applications using Genkit in Go. Use when the user asks to build AI features, agents, flows, or tools in Go using Genkit, or when working with Genkit Go code involving generation, prompts, streaming, tool calling, or model providers. Don't use for Genkit in JavaScript/TypeScript, Python, or Dart.

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

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencesprompts.md

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

Prompts


DefinePrompt

Define a reusable prompt in code with a default model and template.

jokePrompt := genkit.DefinePrompt(g, "joke",
	ai.WithModel(googlegenai.ModelRef("googleai/gemini-flash-latest", nil)),
	ai.WithInputType(JokeRequest{Topic: "example"}),
	ai.WithPrompt("Tell me a joke about {{topic}}."),
)

Execute

resp, err := jokePrompt.Execute(ctx,
	ai.WithInput(map[string]any{"topic": "cats"}),
)
fmt.Println(resp.Text())

ExecuteStream

stream := jokePrompt.ExecuteStream(ctx,
	ai.WithInput(map[string]any{"topic": "cats"}),
)
for result, err := range stream {
	if err != nil { return err }
	if result.Done { break }
	fmt.Print(result.Chunk.Text())
}

Override Options at Execution

resp, err := jokePrompt.Execute(ctx,
	ai.WithInput(map[string]any{"topic": "cats"}),
	ai.WithModelName("googleai/gemini-pro-latest"),  // override model
	ai.WithConfig(map[string]any{"temperature": 0.9}),
	ai.WithTools(myTool),
)

DefineDataPrompt (Typed Input/Output)

Strongly-typed prompts with Go generics.

type JokeRequest struct {
	Topic string `json:"topic"`
}

type Joke struct {
	Setup     string `json:"setup" jsonschema:"description=The setup"`
	Punchline string `json:"punchline" jsonschema:"description=The punchline"`
}

jokePrompt := genkit.DefineDataPrompt[JokeRequest, *Joke](g, "structured-joke",
	ai.WithModel(googlegenai.ModelRef("googleai/gemini-flash-latest", nil)),
	ai.WithPrompt("Tell me a joke about {{topic}}."),
)

Execute (typed)

joke, resp, err := jokePrompt.Execute(ctx, JokeRequest{Topic: "cats"})
// joke is *Joke, resp is *ModelResponse

ExecuteStream (typed)

stream := jokePrompt.ExecuteStream(ctx, JokeRequest{Topic: "cats"})
for result, err := range stream {
	if err != nil { return err }
	if result.Done {
		finalJoke := result.Output // *Joke
		break
	}
	fmt.Print(result.Chunk) // partial *Joke
}

.prompt Files (Dotprompt)

Define prompts in separate files with YAML frontmatter and Handlebars templates.

Basic .prompt File

prompts/joke.prompt:

---
model: googleai/gemini-flash-latest
input:
  schema:
    topic: string
---
Tell me a joke about {{topic}}.

Load and Use

// LookupPrompt returns Prompt (untyped: map[string]any input, string output)
jokePrompt := genkit.LookupPrompt(g, "joke")
resp, err := jokePrompt.Execute(ctx,
	ai.WithInput(map[string]any{"topic": "cats"}),
)

Typed .prompt File

prompts/structured-joke.prompt:

---
model: googleai/gemini-flash-latest
config:
  thinkingConfig:
    thinkingBudget: 0
input:
  schema: JokeRequest
output:
  format: json
  schema: Joke
---
Tell me a joke about {{topic}}.

Register Go types so the .prompt file can reference them by name:

genkit.DefineSchemaFor[JokeRequest](g)
genkit.DefineSchemaFor[Joke](g)

jokePrompt := genkit.LookupDataPrompt[JokeRequest, *Joke](g, "structured-joke")
joke, resp, err := jokePrompt.Execute(ctx, JokeRequest{Topic: "cats"})

LoadPrompt (Explicit Path)

prompt := genkit.LoadPrompt(g, "./prompts/countries.prompt", "countries")
resp, err := prompt.Execute(ctx)

.prompt File Features

Multi-message prompts with roles:

---
model: googleai/gemini-flash-latest
input:
  schema:
    question: string
---
{{ role "system" }}
You are a helpful assistant.

{{ role "user" }}
{{question}}

Media in prompts:

---
model: googleai/gemini-flash-latest
input:
  schema:
    videoUrl: string
    contentType: string
---
{{ role "user" }}
Summarize this video:
{{media url=videoUrl contentType=contentType}}

Conditionals and loops:

---
input:
  schema:
    topic: string
    dietaryRestrictions?(array): string
---
Write a recipe about {{topic}}.
{{#if dietaryRestrictions}}
Dietary restrictions: {{#each dietaryRestrictions}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}.
{{/if}}

Inline schema in .prompt file:

---
model: googleai/gemini-flash-latest
input:
  schema:
    topic: string
    style?: string
output:
  format: json
  schema:
    title: string
    body: string
    tags(array): string
---
Write an article about {{topic}}.
{{#if style}}Write in a {{style}} style.{{/if}}

Tools, tool-loop control, and middleware:

.prompt frontmatter can also configure tool calling and attach middleware, so an agent-style prompt can be fully described in the file:

---
model: googleai/gemini-flash-latest
input:
  schema:
    tone: string
tools:
  - getAttractions
  - getFlightInfo
toolChoice: auto          # auto | required | none
maxTurns: 20              # max tool-call loop iterations (WithMaxTurns)
returnToolRequests: false # return tool requests instead of running them (WithReturnToolRequests)
use:
  - name: genkit-middleware/retry
    config:
      maxRetries: 2
  - genkit-middleware/fallback   # bare string = middleware name, no config
---
{{role "system"}}
You are a friendly trip planning assistant. Keep your tone {{tone}}.

{{history}}

Field mapping:

Frontmatter Option Notes
tools WithTools list of registered tool names (strings)
toolChoice WithToolChoice auto, required, or none
maxTurns WithMaxTurns integer
returnToolRequests WithReturnToolRequests boolean
use WithUse list of middleware refs

use entries are resolved by name against middleware registered on the *Genkit instance. Each entry is either a bare string (the middleware name) or a map with name and optional config. Register the built-in middleware plugin so names like genkit-middleware/retry resolve:

import "github.com/firebase/genkit/go/plugins/middleware"

g := genkit.Init(ctx, genkit.WithPlugins(
	&googlegenai.GoogleAI{},
	&middleware.Middleware{}, // registers genkit-middleware/{retry,fallback,filesystem,skills,toolApproval}
))

Available named middleware: genkit-middleware/retry, genkit-middleware/fallback, genkit-middleware/filesystem, genkit-middleware/skills, genkit-middleware/toolApproval. See references/middleware.md.

Schemas

DefineSchemaFor (from Go type)

Registers a Go struct as a named schema for use in .prompt files.

genkit.DefineSchemaFor[JokeRequest](g)
genkit.DefineSchemaFor[Joke](g)

The schema name matches the Go type name. Use jsonschema struct tags for metadata:

type Recipe struct {
	Title       string       `json:"title" jsonschema:"description=The recipe title"`
	Difficulty  string       `json:"difficulty" jsonschema:"enum=easy,enum=medium,enum=hard"`
	Ingredients []Ingredient `json:"ingredients"`
	Steps       []string     `json:"steps"`
}

type Ingredient struct {
	Name   string  `json:"name"`
	Amount float64 `json:"amount"`
	Unit   string  `json:"unit"`
}

DefineSchema (manual JSON Schema)

genkit.DefineSchema(g, "Recipe", map[string]any{
	"type": "object",
	"properties": map[string]any{
		"title": map[string]any{"type": "string"},
		"ingredients": map[string]any{
			"type":  "array",
			"items": map[string]any{"type": "object"},
		},
	},
	"required": []string{"title", "ingredients"},
})

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.

1 warning2d3 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    The skill provides comprehensive documentation and code samples for developing AI applications with Genkit in Go. It includes instructions for installing the Genkit CLI from an official source and documents security features like tool approval and filesystem sandboxing. No malicious patterns were detected.

  • Socket2d

    No alerts

  • Snyk3d

    Risk: MEDIUM · 2 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 4 days ago.

Activeupdated 5 days ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}

README badge

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