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.

referencestools.md

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

Tools


DefineTool

Define a tool the model can call during generation.

type WeatherInput struct {
	Location string `json:"location" jsonschema:"description=City name"`
}

type WeatherOutput struct {
	Temperature float64 `json:"temperature"`
	Conditions  string  `json:"conditions"`
}

weatherTool := genkit.DefineTool(g, "getWeather",
	"Gets the current weather for a location.",
	func(ctx *ai.ToolContext, input WeatherInput) (WeatherOutput, error) {
		// Call your weather API
		return WeatherOutput{Temperature: 72, Conditions: "sunny"}, nil
	},
)

Using Tools in Generation

Pass tools to Generate, GenerateText, or prompts:

resp, err := genkit.Generate(ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt("What's the weather in San Francisco?"),
	ai.WithTools(weatherTool),
)
// The model calls the tool automatically and incorporates the result
fmt.Println(resp.Text())

Tool Choice

ai.WithToolChoice(ai.ToolChoiceAuto)     // model decides (default)
ai.WithToolChoice(ai.ToolChoiceRequired) // model must use a tool
ai.WithToolChoice(ai.ToolChoiceNone)     // model cannot use tools

Max Turns

Limit how many tool-call round trips the model can make:

ai.WithMaxTurns(3) // default is 5

Tool runtime helpers (ai/tool)

Requires Genkit Go v1.14.0+.

Package github.com/firebase/genkit/go/ai/tool holds the verbs a tool (or a WrapTool hook) calls. They take a context.Context, so they work from DefineTool too (*ai.ToolContext embeds the context).

Function Use
tool.Interrupt(ctx, data) Pause the call for the caller (see below).
tool.Fail(ctx, err) Hand this error to the model as the tool's output so it can fix its input, instead of stopping the loop.
tool.AttachParts(ctx, parts...) Attach media (images, etc.) to the result while keeping the tool's own output type. Replaces multipart tools.
tool.SendChunk(ctx, chunk) Stream progress from a tool (set Role: ai.RoleTool).
tool.OriginalInput[In](ctx) On a restart with revised input, the input the tool was first called with.
tool.ResumeData[T](ctx) Read a resume payload untyped (for WrapTool hooks).
import "github.com/firebase/genkit/go/ai/tool"

screenshot := genkit.DefineTool(g, "screenshot", "Takes a screenshot of a page.",
	func(ctx *ai.ToolContext, in PageInput) (*PageInfo, error) {
		info, png, err := capture(ctx, in.URL)
		if err != nil {
			return nil, err
		}
		tool.AttachParts(ctx, ai.NewMediaPart("image/png", png)) // a data: URL
		return info, nil // still *PageInfo, so the model sees the real output schema
	})

genkit.DefineMultipartTool / ai.MultipartToolResponse are deprecated; use tool.AttachParts.

Errors the model can fix

By default a tool error stops the loop with ai.ErrToolFailed. Return tool.Fail for errors the model can correct:

query := genkit.DefineTool(g, "query", "Runs a read-only SQL query.",
	func(ctx *ai.ToolContext, in QueryInput) ([]string, error) {
		rows, err := runQuery(ctx, in.SQL)
		if errors.Is(err, errSyntax) {
			return nil, tool.Fail(ctx, err) // the model reads the error and fixes the query
		}
		return rows, err // a lost connection still stops the loop
	})

For tools you don't own (e.g. MCP tools), &middleware.SoftToolErrors{} (optionally Tools: []string{"search"}) returns every tool error to the model. See middleware. A tool that panics fails with the message tool panicked instead of crashing the process.

Tool interrupts (DefineResumableTool)

Pause a tool call to get human input, then resume. Use genkit.DefineResumableTool, whose third type parameter is the resume type: what the caller answers with. The tool receives it as a pointer that is nil on the first call.

Do not use the deprecated ai.InterruptWith, ToolContext.Interrupt, RestartWith/RespondWith, WithToolRestarts/WithToolResponses, ai.ResumedValue, IsResumed, or ai.OriginalInputAs. The experimental genkitx.DefineInterruptibleTool and ai/exp/tool package were removed in v1.14.0.

Define a resumable tool

import (
	"context"

	"github.com/firebase/genkit/go/ai/tool"
	"github.com/firebase/genkit/go/genkit"
)

type TransferInput struct {
	ToAccount string  `json:"toAccount"`
	Amount    float64 `json:"amount"`
}

type TransferOutput struct {
	Status string `json:"status"`
}

// TransferInterrupt is extra data the tool reports when it pauses.
type TransferInterrupt struct {
	Reason string `json:"reason"`
}

// Confirmation is the resume type. Must be a struct or map (a JSON object).
type Confirmation struct {
	Approved bool `json:"approved"`
}

transferMoney := genkit.DefineResumableTool(g, "transferMoney",
	"Transfers money to another account.",
	func(ctx context.Context, in TransferInput, confirm *Confirmation) (*TransferOutput, error) {
		if confirm == nil && in.Amount > approvalLimit {
			// First call: pause. Data must serialize to a JSON object (or be nil).
			return nil, tool.Interrupt(ctx, TransferInterrupt{Reason: "over_limit"})
		}
		if confirm != nil && !confirm.Approved {
			return &TransferOutput{Status: "declined"}, nil
		}
		// ... perform the transfer
		return &TransferOutput{Status: "completed"}, nil
	},
)

DefineResumableTool returns *ai.ResumableToolAction[In, Out, Res].

Handle interrupts

A paused generation returns (resp, nil) with resp.FinishReason == ai.FinishReasonInterrupted. Claim each part in resp.Interrupts() with the tool's Interrupted method. The claim matches the part to the tool and decodes its input, so the verbs after it cannot fail:

Verb Effect
call.Restart(resume) Run the tool again with the typed resume value.
call.RestartWithInput(input, resume) Run it again with revised input (history records the revised request).
call.Respond(output) Answer with the tool's output; the tool does not run again.
const maxRounds = 5

resp, err := genkit.Generate(ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt(userRequest),
	ai.WithTools(transferMoney),
)
if err != nil {
	return err
}

for round := 0; round < maxRounds && resp.FinishReason == ai.FinishReasonInterrupted; round++ {
	var parts []*ai.Part
	for _, part := range resp.Interrupts() {
		call, ok := transferMoney.Interrupted(part)
		if !ok {
			return fmt.Errorf("no handler for interrupt from tool %q", part.ToolRequest.Name)
		}
		meta, _ := ai.InterruptAs[TransferInterrupt](part) // the data the tool paused with
		log.Println("paused:", meta.Reason)
		approved := askUser(call.Input.Amount, call.Input.ToAccount) // call.Input is TransferInput
		parts = append(parts, call.Restart(Confirmation{Approved: approved}))
	}

	// The full history, the same tools, and every interrupt answered.
	resp, err = genkit.Generate(ctx, g,
		ai.WithMessages(resp.History()...),
		ai.WithTools(transferMoney),
		ai.WithResume(parts...),
	)
	if err != nil {
		return err
	}
}
fmt.Println(resp.Text())

Rules:

  • Answer every interrupt in the turn with exactly one part. Missing one fails with ai.ErrUnresolvedToolRequest; passing resp.Interrupts() back as is (still paused) fails with INVALID_ARGUMENT. The whole resume is checked before any tool runs.
  • Several resumable tools? Try each tool's Interrupted on the part; it reports false for other tools' interrupts.
  • A resume can pause again. It returns (resp, nil) with FinishReason interrupted, so loop as above.
  • Middleware holds (e.g. ToolApproval) are not the tool's interrupt: claim them with middleware.ToolApprovalInterrupted(part). See middleware.
  • Generic handlers that only hold the part can build the same parts with part.ToToolRestart(resume), part.ToToolRestartWithInput(input, resume), and part.ToToolResponse(output). These return an error, since nothing was type-checked by a claim.

Read the original input after a revised restart

if orig, ok := tool.OriginalInput[TransferInput](ctx); ok {
	log.Printf("adjusted from $%.2f to $%.2f", orig.Amount, in.Amount)
}

tool.OriginalInput reports false on a first call and on a restart that kept the input.

Agents use the same tool and claim; only the delivery differs (aix.ToolResume). See agents-human-in-the-loop.md.

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