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.

referencesgeneration.md

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

Generation


GenerateText

Simplest form. Returns a string.

text, err := genkit.GenerateText(ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt("Tell me a joke about %s", topic),
)

Generate

Returns a full *ModelResponse with metadata, usage stats, and history.

resp, err := genkit.Generate(ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithSystem("You are a helpful assistant."),
	ai.WithPrompt("Explain %s", topic),
)
fmt.Println(resp.Text())         // concatenated text
fmt.Println(resp.FinishReason)   // ai.FinishReasonStop, etc.
fmt.Println(resp.Usage)          // token counts

GenerateData (Structured Output)

Returns a typed Go value parsed from the model's JSON output.

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

joke, resp, err := genkit.GenerateData[Joke](ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt("Tell me a joke about %s", topic),
)
// joke is *Joke, resp is *ModelResponse

Streaming

GenerateStream

Returns an iterator. Each value has .Done, .Chunk, and .Response.

stream := genkit.GenerateStream(ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt("Tell me a long story about %s", topic),
)
for result, err := range stream {
	if err != nil {
		return err
	}
	if result.Done {
		finalText := result.Response.Text()
		break
	}
	fmt.Print(result.Chunk.Text()) // incremental text
}

GenerateDataStream (Structured Streaming)

Streams typed partial objects as they arrive.

stream := genkit.GenerateDataStream[Joke](ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt("Tell me a joke about %s", topic),
)
for result, err := range stream {
	if err != nil {
		return err
	}
	if result.Done {
		finalJoke := result.Output // *Joke
		break
	}
	partialJoke := result.Chunk // *Joke (partial)
}

Callback-Based Streaming

Use ai.WithStreaming with Generate for callback-style streaming. The callback receives *ai.ModelResponseChunk:

resp, err := genkit.Generate(ctx, g,
	ai.WithModelName("googleai/gemini-flash-latest"),
	ai.WithPrompt("Tell me a story"),
	ai.WithStreaming(func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
		fmt.Print(chunk.Text()) // extract text from chunk
		return nil
	}),
)
// resp contains the final complete response

Common Options

// Model selection
ai.WithModel(googlegenai.ModelRef("googleai/gemini-flash-latest", nil))  // model reference
ai.WithModelName("googleai/gemini-flash-latest")                         // by name string

// Content
ai.WithPrompt("Tell me about %s", topic)    // user message (supports fmt verbs)
ai.WithSystem("You are a pirate.")          // system instructions
ai.WithMessages(msg1, msg2)                 // conversation history
ai.WithDocs(doc1, doc2)                     // context documents
ai.WithTextDocs("context 1", "context 2")   // context as strings

// Model config (provider-specific)
ai.WithConfig(map[string]any{"temperature": 0.7})

Output Formats

Control how the model structures its output.

By Go Type

// Automatically uses JSON format and instructs model to match the type
ai.WithOutputType(MyStruct{})

By Format String

ai.WithOutputFormat(ai.OutputFormatJSON)   // single JSON object
ai.WithOutputFormat(ai.OutputFormatJSONL)  // JSON Lines (one object per line)
ai.WithOutputFormat(ai.OutputFormatArray)  // JSON array
ai.WithOutputFormat(ai.OutputFormatEnum)   // constrained enum value
ai.WithOutputFormat(ai.OutputFormatText)   // plain text (default)

Enum Output

type Color string
const (
	Red   Color = "red"
	Green Color = "green"
	Blue  Color = "blue"
)

text, err := genkit.GenerateText(ctx, g,
	ai.WithPrompt("What color is the sky?"),
	ai.WithOutputEnums(Red, Green, Blue),
)

Custom Output Instructions

ai.WithOutputInstructions("Return a JSON object with fields: name (string), age (number)")

Combining Format + Schema

// JSONL with a typed schema (useful for streaming lists)
genkit.DefinePrompt(g, "characters",
	ai.WithPrompt("Generate 5 story characters"),
	ai.WithOutputType([]StoryCharacter{}),
	ai.WithOutputFormat(ai.OutputFormatJSONL),
)

Embeddings

res, err := genkit.Embed(ctx, g,
	ai.WithEmbedderName("googleai/gemini-embedding-001"),
	ai.WithTextDocs("The sky is blue.", "Grass is green."), // one embedding per doc
)
if err != nil {
	return err
}
vector := res.Embeddings[0].Embedding // []float32

// Optional tuning (default is 3072 dimensions)
res, err = genkit.Embed(ctx, g,
	ai.WithEmbedderName("googleai/gemini-embedding-001"),
	ai.WithTextDocs("The sky is blue."),
	ai.WithConfig(&genai.EmbedContentConfig{
		OutputDimensionality: genai.Ptr[int32](768),
		TaskType:             "RETRIEVAL_DOCUMENT",
	}),
)

Common embedders: googleai/gemini-embedding-001 (text), googleai/gemini-embedding-2 (multimodal: text, image, video).

Multimodal input goes in a document's media parts:

res, err := genkit.Embed(ctx, g,
	ai.WithEmbedderName("googleai/gemini-embedding-2"),
	ai.WithDocs(&ai.Document{Content: []*ai.Part{
		ai.NewMediaPart("image/png", "data:image/png;base64,..."),
	}}),
)

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