Help us shape the future of Go! Take the 2026 Go Developer Survey

gemini

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package gemini adapts Google's Gemini generateContent API to agent.Model. It translates the provider-neutral []agent.Message/Part model to Gemini "contents" (text, inlineData/fileData images, functionCall, functionResponse), declares tools as functionDeclarations, opens the SSE stream (streamGenerateContent?alt=sse), and normalizes Gemini's response chunks into agent's Event taxonomy. Zero external deps: net/http + stdlib, exactly like the anthropic and openai adapters (the repo enforces a lean core via architecture_test.go, so google.golang.org/genai is deliberately not imported).

Gemini sends COMPLETE functionCall objects (name + args object), not the streamed argument fragments Anthropic/OpenAI send. So one functionCall part becomes a single ToolCallDelta carrying the whole args object as ArgsFragment; the agent core's finalize() json.Valid gate still holds because a complete args object is valid JSON.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is a Google Gemini generateContent API adapter implementing agent.Model.

func New

func New(apiKey string, opts ...Option) *Model

New constructs a Gemini model adapter. apiKey is your Google AI Studio API key. For Vertex AI or a proxy, override the host with WithBaseURL (the request path /v1beta/models/{model}:streamGenerateContent is appended to it).

func (*Model) Describe added in v0.8.0

func (m *Model) Describe() agent.ModelInfo

Describe reports the adapter's identity: provider "gemini", the configured model ID, and support for a JSON-schema response format.

func (Model) Format added in v0.7.0

func (m Model) Format(f fmt.State, _ rune)

Format writes String for every verb, so no fmt verb (%v, %+v, %#v, %s, %x, ...) prints the key.

func (*Model) RequiresToolsForRequired added in v0.10.0

func (m *Model) RequiresToolsForRequired() bool

RequiresToolsForRequired implements agent.ToolRules: Gemini refuses tool choice "required" with no tools declared.

func (*Model) Stream

func (m *Model) Stream(ctx context.Context, req agent.Request) (*agent.Stream, error)

Stream implements agent.Model.

func (Model) String added in v0.7.0

func (m Model) String() string

String describes the adapter by its model id. It never includes the API key (or the base URL, which can carry credentials of its own), so a config that holds a *Model can be logged.

func (*Model) ToolNameRule added in v0.10.0

func (m *Model) ToolNameRule() *regexp.Regexp

ToolNameRule implements agent.ToolRules: the tool names Gemini accepts, `^[a-zA-Z_][a-zA-Z0-9_.:-]{0,63}$`, so agent.New refuses any other name when the agent is built.

type Option

type Option func(*Model)

Option configures a Model in New.

func WithBaseURL

func WithBaseURL(u string) Option

WithBaseURL sets the API host, for a proxy or a compatible endpoint; /v1beta/models/{model}:streamGenerateContent is appended to it. The default is "https://generativelanguage.googleapis.com".

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets the HTTP client requests are sent with. The default is http.DefaultClient.

func WithMaxResponseBytes added in v0.8.0

func WithMaxResponseBytes(n int64) Option

WithMaxResponseBytes caps how many bytes of one streamed reply the adapter reads: a reply that runs longer fails with agent.ErrResponseTooLarge, which middleware.Retryable does not retry. The default is provider.DefaultMaxResponseBytes (32 MiB); n <= 0 keeps it. Raise it for replies that legitimately run longer, such as large inline images.

func WithMaxTokens

func WithMaxTokens(n int) Option

WithMaxTokens sets the default limit on tokens in a reply; a request's Sampling.MaxTokens overrides it. The default is 0, which leaves the limit to the provider.

func WithModel

func WithModel(id string) Option

WithModel sets the model ID sent with every request. The default is "gemini-2.0-flash".

func WithToolResultCodec added in v0.2.0

func WithToolResultCodec(c provider.ToolResultCodec) Option

WithToolResultCodec encodes tool results sent to the model with c instead of raw JSON (for example GCF, to cut tokens on structured output). The journal keeps the JSON form; only what the model reads changes. Default is JSON. Gemini normally passes a JSON tool result through as a structured response; with a codec set, the encoded string is sent as {"result": <encoded>}.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL