Skip to main content
The genkit package provides the core functionality for building AI applications in Go.

Init()

Creates and initializes a new Genkit instance.

Parameters

context.Context
required
Context for initialization (should handle shutdown signals)
...GenkitOption
Configuration optionsAvailable options:
  • WithPlugins(...Plugin) - Initialize plugins
  • WithDefaultModel(string) - Set default model
  • WithPromptDir(string) - Set prompts directory
  • WithPromptFS(fs.FS) - Use embedded prompt filesystem

Returns

*Genkit
Initialized Genkit instance

Generate()

Performs a model generation request.

Parameters

context.Context
required
Context for the request
*Genkit
required
Genkit instance
...GenerateOption
Generation options (see ai package)Common options:
  • ai.WithModelName(string)
  • ai.WithModel(Model)
  • ai.WithPrompt(string)
  • ai.WithSystem(string)
  • ai.WithMessages([]*Message)
  • ai.WithTools(...Tool)
  • ai.WithConfig(any)
  • ai.WithOutputType(T)
  • ai.WithOutputSchema(map[string]any)

Returns

*ModelResponse
The model’s response
error
Error if generation failed

GenerateText()

Generates text and returns it as a string.

Parameters

Same as Generate().

Returns

string
Generated text
error
Error if generation failed

GenerateData()

Generates structured data matching a Go type.

Type Parameters

any
required
Output type (struct with JSON tags)

Parameters

Same as Generate().

Returns

*Out
Typed output data
*ModelResponse
Full model response
error
Error if generation failed

GenerateStream()

Generates a response with streaming.

Returns

iter.Seq2[*ModelStreamValue, error]
Iterator yielding stream values and errorsModelStreamValue fields:
  • Done bool - True when streaming is complete
  • Response *ModelResponse - Final response (when Done)
  • Chunk *ModelResponseChunk - Streaming chunk (when !Done)

GenerateDataStream()

Generates structured data with streaming.

DefineFlow()

Defines and registers a flow.

Type Parameters

any
required
Input type
any
required
Output type

Parameters

*Genkit
required
Genkit instance
string
required
Flow name
func(context.Context, In) (Out, error)
required
Flow implementation

Returns

*Flow[In, Out, struct{}]
Flow instance with Run() method

DefineStreamingFlow()

Defines a streaming flow.

Type Parameters

any
required
Input type
any
required
Final output type
any
required
Streaming chunk type

Parameters

*Genkit
required
Genkit instance
string
required
Flow name
StreamingFunc[In, Out, Stream]
required
Implementation functionSignature:

Returns

*Flow[In, Out, Stream]
Streaming flow instance

Run()

Runs a step within a flow (for tracing).

Type Parameters

any
required
Return type

Parameters

context.Context
required
Context (must be from within a flow)
string
required
Step name (for tracing)
func() (Out, error)
required
Step implementation

Returns

Out
Step output
error
Error if step failed

DefineTool()

Defines and registers a tool.
See ai.DefineTool for full documentation.

DefinePrompt()

Defines a prompt programmatically.
See Prompts API for full documentation.

LookupPrompt()

Retrieves a prompt by name (from .prompt files or DefinePrompt).

DefineModel()

Defines a custom model.

LookupModel()

Retrieves a registered model by name.

Example: Complete Application