gemini

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 24 Imported by: 1

Documentation

Overview

Package gemini implements a client for Google's Gemini API.

Not to be confused with Google's Vertex AI.

It is described at https://ai.google.dev/api/?lang=rest but the doc is weirdly organized.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProcessStream

func ProcessStream(chunks iter.Seq[ChatStreamChunkResponse]) (iter.Seq[genai.Reply], func() (genai.Usage, [][]genai.Logprob, error))

ProcessStream converts the raw packets from the streaming API into Reply fragments.

Types

type Blob

type Blob struct {
	MimeType string `json:"mimeType,omitzero"`
	Data     []byte `json:"data,omitzero"`
}

Blob is documented at https://ai.google.dev/api/caching?hl=en#Blob

type CachedContent

type CachedContent struct {
	Expiration
	Contents          []Content  `json:"contents,omitzero"`
	Tools             []Tool     `json:"tools,omitzero"`
	Name              string     `json:"name,omitzero"`
	DisplayName       string     `json:"displayName,omitzero"`
	Model             string     `json:"model"`
	SystemInstruction Content    `json:"systemInstruction,omitzero"`
	ToolConfig        ToolConfig `json:"toolConfig,omitzero"`

	// Not to be set on upload.
	CreateTime    string               `json:"createTime,omitzero"`
	UpdateTime    string               `json:"updateTime,omitzero"`
	UsageMetadata CachingUsageMetadata `json:"usageMetadata,omitzero"`
}

CachedContent is documented at https://ai.google.dev/api/caching?hl=en#CachedContent https://ai.google.dev/api/caching?hl=en#request-body

func (*CachedContent) GetDisplayName

func (c *CachedContent) GetDisplayName() string

GetDisplayName implements genai.CacheItem.

func (*CachedContent) GetExpiry

func (c *CachedContent) GetExpiry() time.Time

GetExpiry implements genai.CacheItem.

func (*CachedContent) GetID

func (c *CachedContent) GetID() string

GetID implements genai.Model.

func (*CachedContent) Init

func (c *CachedContent) Init(msgs genai.Messages, model, name, displayName string, ttl time.Duration, opts ...genai.GenOption) error

Init initializes the request from the given parameters.

type CachingUsageMetadata

type CachingUsageMetadata struct {
	TotalTokenCount int64 `json:"totalTokenCount"`
}

CachingUsageMetadata is documented at https://ai.google.dev/api/caching?hl=en#UsageMetadata

type ChatRequest

type ChatRequest struct {
	Contents          []Content       `json:"contents"`
	Tools             []Tool          `json:"tools,omitzero"`
	ToolConfig        ToolConfig      `json:"toolConfig,omitzero"`
	SafetySettings    []SafetySetting `json:"safetySettings,omitzero"`
	SystemInstruction Content         `json:"systemInstruction,omitzero"`
	// https://ai.google.dev/api/generate-content?hl=en#v1beta.GenerationConfig
	GenerationConfig struct {
		StopSequences              []string   `json:"stopSequences,omitzero"`
		ResponseMimeType           string     `json:"responseMimeType,omitzero"` // "text/plain", "application/json", "text/x.enum"
		ResponseSchema             Schema     `json:"responseSchema,omitzero"`   // Requires ResponseMimeType == "application/json"
		ResponseModalities         []Modality `json:"responseModalities,omitzero"`
		CandidateCount             int64      `json:"candidateCount,omitzero"` // >= 1
		MaxOutputTokens            int64      `json:"maxOutputTokens,omitzero"`
		Temperature                float64    `json:"temperature,omitzero"` // [0, 2]
		TopP                       float64    `json:"topP,omitzero"`
		TopK                       int64      `json:"topK,omitzero"`
		Seed                       int64      `json:"seed,omitzero"`
		PresencePenalty            float64    `json:"presencePenalty,omitzero"`
		FrequencyPenalty           float64    `json:"frequencyPenalty,omitzero"`
		ResponseLogprobs           bool       `json:"responseLogprobs,omitzero"`
		Logprobs                   int64      `json:"logprobs,omitzero"`
		EnableEnhancedCivicAnswers bool       `json:"enableEnhancedCivicAnswers,omitzero"`
		// https://ai.google.dev/api/generate-content?hl=en#SpeechConfig
		SpeechConfig struct {
			// https://ai.google.dev/api/generate-content?hl=en#VoiceConfig
			VoiceConfig struct {
				// https://ai.google.dev/api/generate-content?hl=en#PrebuiltVoiceConfig
				PrebuiltVoiceConfig struct {
					VoiceName string `json:"voiceName,omitzero"`
				} `json:"prebuiltVoiceConfig,omitzero"`
			} `json:"voiceConfig,omitzero"`
		} `json:"speechConfig,omitzero"`
		// See https://ai.google.dev/gemini-api/docs/thinking#rest
		// This is frustrating: it must be present for thinking models to make it possible to disable thinking. It
		// must NOT be present for non-thinking models.
		ThinkingConfig  *ThinkingConfig `json:"thinkingConfig,omitempty"`
		MediaResolution MediaResolution `json:"mediaResolution,omitzero"`
	} `json:"generationConfig,omitzero"`
	CachedContent string `json:"cachedContent,omitzero"` // Name of the cached content with "cachedContents/" prefix.
}

ChatRequest is documented at https://ai.google.dev/api/generate-content?hl=en#text_gen_text_only_prompt-SHELL

func (*ChatRequest) Init

func (c *ChatRequest) Init(msgs genai.Messages, model string, opts ...genai.GenOption) error

Init initializes the provider specific completion request with the generic completion request.

func (*ChatRequest) SetStream

func (c *ChatRequest) SetStream(stream bool)

SetStream sets the streaming mode.

type ChatResponse

type ChatResponse struct {
	Candidates     []ResponseCandidate `json:"candidates"`
	PromptFeedback struct{}            `json:"promptFeedback,omitzero"`
	UsageMetadata  UsageMetadata       `json:"usageMetadata"`
	ModelVersion   string              `json:"modelVersion"`
	ResponseID     string              `json:"responseId"`
}

ChatResponse is documented at https://ai.google.dev/api/generate-content?hl=en#v1beta.GenerateContentResponse

func (*ChatResponse) ToResult

func (c *ChatResponse) ToResult() (genai.Result, error)

ToResult converts the response to a genai.Result.

type ChatStreamChunkResponse

type ChatStreamChunkResponse struct {
	Candidates []struct {
		Content            Content            `json:"content"`
		FinishReason       FinishReason       `json:"finishReason"`
		FinishMessage      string             `json:"finishMessage"`
		Index              int64              `json:"index"`
		GroundingMetadata  GroundingMetadata  `json:"groundingMetadata"`
		UrlContextMetadata UrlContextMetadata `json:"urlContextMetadata"`
	} `json:"candidates"`
	UsageMetadata UsageMetadata `json:"usageMetadata"`
	ModelVersion  string        `json:"modelVersion"`
	ResponseID    string        `json:"responseId"`
}

ChatStreamChunkResponse is the provider-specific streaming chat chunk.

type Client

type Client struct {
	base.NotImplemented
	// contains filtered or unexported fields
}

Client implements genai.Provider.

func New

func New(ctx context.Context, opts ...genai.ProviderOption) (*Client, error)

New creates a new client to talk to Google's Gemini platform API.

If ProviderOptionAPIKey is not provided, it tries to load it from the GEMINI_API_KEY environment variable. If none is found, it will still return a client coupled with an base.ErrAPIKeyRequired error. Get your API key at https://ai.google.dev/gemini-api/docs/getting-started

To use multiple models, create multiple clients. Use one of the model from https://ai.google.dev/gemini-api/docs/models/gemini

See https://ai.google.dev/gemini-api/docs/file-prompting-strategies?hl=en for good ideas on how to prompt with images.

Using large files requires a pinned model with caching support.

Visit https://ai.google.dev/gemini-api/docs/pricing for up to date information.

As of May 2025, price on Pro model increases when more than 200k input tokens are used. Cached input tokens are 25% of the price of new tokens.

Example (HTTP_record)
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/maruel/genai"
	"github.com/maruel/genai/httprecord"
	"github.com/maruel/genai/providers/gemini"
	"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
)

func main() {
	// Example to do HTTP recording and playback for smoke testing.
	// The example recording is in testdata/example.yaml.
	var rr *recorder.Recorder
	defer func() {
		// In a smoke test, use t.Cleanup().
		if rr != nil {
			if err := rr.Stop(); err != nil {
				log.Printf("Failed saving recordings: %v", err)
			}
		}
	}()

	// Simple trick to force recording via an environment variable.
	mode := recorder.ModeRecordOnce
	if os.Getenv("RECORD") == "all" {
		mode = recorder.ModeRecordOnly
	}
	wrapper := func(h http.RoundTripper) http.RoundTripper {
		var err error
		rr, err = httprecord.New("testdata/example", h, recorder.WithMode(mode))
		if err != nil {
			log.Fatal(err)
		}
		return rr
	}
	// When playing back the smoke test, no API key is needed. Insert a fake API key.
	var opts []genai.ProviderOption
	if os.Getenv("GEMINI_API_KEY") == "" {
		opts = append(opts, genai.ProviderOptionAPIKey("<insert_api_key_here>"))
	}
	ctx := context.Background()
	c, err := gemini.New(ctx, append([]genai.ProviderOption{genai.ProviderOptionTransportWrapper(wrapper)}, opts...)...)
	if err != nil {
		log.Fatal(err)
	}
	models, err := c.ListModels(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if len(models) > 1 {
		fmt.Println("Found multiple models")
	}
}
Output:

Found multiple models

func (*Client) CacheAddRequest

func (c *Client) CacheAddRequest(ctx context.Context, msgs genai.Messages, name, displayName string, ttl time.Duration, opts ...genai.GenOption) (string, error)

CacheAddRequest caches the content for later use.

Includes tools and systemprompt.

Default time to live (ttl) is 1 hour.

The minimum cacheable object is 4096 tokens.

Visit https://ai.google.dev/gemini-api/docs/pricing for up to date information.

As of May 2025, price on Pro model cache cost is 4.50$ per 1MTok/hour and Flash is 1$ per 1Mok/hour.

At certain volumes, using cached tokens is lower cost than passing in the same corpus of tokens repeatedly. The cost for caching depends on the input token size and how long you want the tokens to persist.

func (*Client) CacheDelete

func (c *Client) CacheDelete(ctx context.Context, name string) error

CacheDelete deletes a cached file.

func (*Client) CacheExtend

func (c *Client) CacheExtend(ctx context.Context, name string, ttl time.Duration) error

CacheExtend extends the TTL of a cached content entry.

func (*Client) CacheGetRaw

func (c *Client) CacheGetRaw(ctx context.Context, name string) (CachedContent, error)

CacheGetRaw retrieves a cached content entry.

func (*Client) CacheList

func (c *Client) CacheList(ctx context.Context) ([]genai.CacheEntry, error)

CacheList lists cache entries.

func (*Client) CacheListRaw

func (c *Client) CacheListRaw(ctx context.Context) ([]CachedContent, error)

CacheListRaw retrieves the list of cached items.

func (*Client) Capabilities

func (c *Client) Capabilities() genai.ProviderCapabilities

Capabilities implements genai.Provider.

func (*Client) CountTokens added in v0.2.0

func (c *Client) CountTokens(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (*CountTokensResponse, error)

CountTokens counts the number of tokens in the given messages.

https://ai.google.dev/api/tokens#method:-models.counttokens

func (*Client) FileDelete added in v0.2.0

func (c *Client) FileDelete(ctx context.Context, name string) error

FileDelete deletes a file.

The name parameter should be in the form "files/{id}".

https://ai.google.dev/api/files#method:-files.delete

func (*Client) FileGetMetadata added in v0.2.0

func (c *Client) FileGetMetadata(ctx context.Context, name string) (*FileMetadata, error)

FileGetMetadata retrieves metadata for a single file.

The name parameter should be in the form "files/{id}".

https://ai.google.dev/api/files#method:-files.get

func (*Client) FileList added in v0.2.0

func (c *Client) FileList(ctx context.Context) ([]FileMetadata, error)

FileList returns all files.

https://ai.google.dev/api/files#method:-files.list

func (*Client) FileListRaw added in v0.2.0

func (c *Client) FileListRaw(ctx context.Context) (*FileListResponse, error)

FileListRaw returns the raw paginated response for files.

https://ai.google.dev/api/files#method:-files.list

func (*Client) FileSearchStoreCreate added in v0.2.0

func (c *Client) FileSearchStoreCreate(ctx context.Context, displayName string) (*FileSearchStore, error)

FileSearchStoreCreate creates a new file search store.

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreDelete added in v0.2.0

func (c *Client) FileSearchStoreDelete(ctx context.Context, name string, force bool) error

FileSearchStoreDelete deletes a file search store.

The name parameter should be in the form "fileSearchStores/{id}". Set force to true to delete the store even if it contains documents.

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreDocumentDelete added in v0.2.0

func (c *Client) FileSearchStoreDocumentDelete(ctx context.Context, name string, force bool) error

FileSearchStoreDocumentDelete deletes a document from a file search store.

The name parameter should be in the form "fileSearchStores/{storeId}/documents/{docId}". Set force to true to force deletion even if the document is still being processed.

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreDocumentGet added in v0.2.0

func (c *Client) FileSearchStoreDocumentGet(ctx context.Context, name string) (*FileSearchStoreDocument, error)

FileSearchStoreDocumentGet retrieves a document from a file search store.

The name parameter should be in the form "fileSearchStores/{storeId}/documents/{docId}".

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreDocumentList added in v0.2.0

func (c *Client) FileSearchStoreDocumentList(ctx context.Context, store string) ([]FileSearchStoreDocument, error)

FileSearchStoreDocumentList returns all documents in a file search store.

The store parameter should be in the form "fileSearchStores/{id}".

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreDocumentListRaw added in v0.2.0

func (c *Client) FileSearchStoreDocumentListRaw(ctx context.Context, store string) (*FileSearchStoreDocumentListResponse, error)

FileSearchStoreDocumentListRaw returns the raw paginated response for documents in a file search store.

The store parameter should be in the form "fileSearchStores/{id}".

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreGet added in v0.2.0

func (c *Client) FileSearchStoreGet(ctx context.Context, name string) (*FileSearchStore, error)

FileSearchStoreGet retrieves a file search store.

The name parameter should be in the form "fileSearchStores/{id}".

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreImportFile added in v0.2.0

func (c *Client) FileSearchStoreImportFile(ctx context.Context, store, fileName string) (*Operation, error)

FileSearchStoreImportFile imports an already-uploaded file into a file search store.

The store parameter should be in the form "fileSearchStores/{id}". The fileName parameter should be in the form "files/{id}".

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreList added in v0.2.0

func (c *Client) FileSearchStoreList(ctx context.Context) ([]FileSearchStore, error)

FileSearchStoreList returns all file search stores.

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreListRaw added in v0.2.0

func (c *Client) FileSearchStoreListRaw(ctx context.Context) (*FileSearchStoreListResponse, error)

FileSearchStoreListRaw returns the raw paginated response for file search stores.

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileSearchStoreUploadDocument added in v0.2.0

func (c *Client) FileSearchStoreUploadDocument(ctx context.Context, store, displayName, mimeType string, r io.Reader) (*Operation, error)

FileSearchStoreUploadDocument uploads a document to a file search store using the resumable upload protocol.

The store parameter should be in the form "fileSearchStores/{id}". Returns an Operation since document processing is asynchronous.

https://ai.google.dev/gemini-api/docs/file-search

func (*Client) FileUpload added in v0.2.0

func (c *Client) FileUpload(ctx context.Context, displayName, mimeType string, r io.Reader) (*FileMetadata, error)

FileUpload uploads a file using the Gemini resumable upload protocol.

The file is uploaded in two phases:

  1. Initiate a resumable upload session with metadata.
  2. Upload the file bytes to the returned upload URL.

https://ai.google.dev/api/files#method:-media.upload

func (*Client) GenAsync

func (c *Client) GenAsync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (genai.Job, error)

GenAsync implements genai.ProviderGenAsync.

It requests the providers' asynchronous API and returns the job ID.

GenAsync is only supported for models that have "predictLongRunning" reported in their Model.SupportedGenerationMethods.

The resulting file is available for 48 hours. It requires the API key in the HTTP header to be fetched, so use the client's HTTP client.

func (*Client) GenStream

func (c *Client) GenStream(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (iter.Seq[genai.Reply], func() (genai.Result, error))

GenStream implements genai.Provider.

func (*Client) GenStreamRaw

func (c *Client) GenStreamRaw(ctx context.Context, in *ChatRequest) (iter.Seq[ChatStreamChunkResponse], func() error)

GenStreamRaw provides access to the raw API.

It calls the Gemini API method streamGenerateContent?alt=sse.

func (*Client) GenSync

func (c *Client) GenSync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (genai.Result, error)

GenSync implements genai.Provider.

func (*Client) GenSyncRaw

func (c *Client) GenSyncRaw(ctx context.Context, in *ChatRequest, out *ChatResponse) error

GenSyncRaw provides access to the raw API.

It calls the Gemini API method generateContent.

func (*Client) GetModel added in v0.2.0

func (c *Client) GetModel(ctx context.Context, id string) (*Model, error)

GetModel returns the details for a single model.

https://ai.google.dev/api/models?hl=en#method:-models.get

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient returns the HTTP client to fetch results (e.g. videos) generated by the provider.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]genai.Model, error)

ListModels implements genai.Provider.

func (*Client) ModelID

func (c *Client) ModelID() string

ModelID implements genai.Provider.

It returns the selected model ID.

func (*Client) Name

func (c *Client) Name() string

Name implements genai.Provider.

It returns the name of the provider.

func (*Client) OutputModalities

func (c *Client) OutputModalities() genai.Modalities

OutputModalities implements genai.Provider.

It returns the output modalities, i.e. what kind of output the model will generate (text, audio, image, video, etc).

func (*Client) PokeResult

func (c *Client) PokeResult(ctx context.Context, id genai.Job) (genai.Result, error)

PokeResult implements genai.ProviderGenAsync.

It retrieves the result for a job ID.

func (*Client) PokeResultRaw

func (c *Client) PokeResultRaw(ctx context.Context, id genai.Job) (Operation, error)

PokeResultRaw retrieves the result for a job ID if already available.

func (*Client) PredictLongRunningRaw

func (c *Client) PredictLongRunningRaw(ctx context.Context, req *ImageRequest) (Operation, error)

PredictLongRunningRaw requests the providers' asynchronous API to generate a video.

The official documentation https://ai.google.dev/api/models?hl=en#method:-models.predictlongrunning is not really helpful.

func (*Client) PredictRaw

func (c *Client) PredictRaw(ctx context.Context, req *ImageRequest) (ImageResponse, error)

PredictRaw requests the providers' synchronous API to generate an image.

The official documentation https://ai.google.dev/api/models?hl=en#method:-models.predict is not really helpful.

func (*Client) Scoreboard

func (c *Client) Scoreboard() scoreboard.Score

Scoreboard implements genai.Provider.

type CodeExecutionResult

type CodeExecutionResult struct {
	Outcome string `json:"outcome,omitzero"` // One of OUTCOME_UNSPECIFIED, OUTCOME_OK, OUTCOME_FAILED, OUTCOME_DEADLINE_EXCEEDED
	Output  string `json:"output,omitzero"`
}

CodeExecutionResult is documented at https://ai.google.dev/api/caching?hl=en#CodeExecutionResult

type Content

type Content struct {
	Role string `json:"role,omitzero"` // "user", "model"
	// Parts can be both content and tool calls.
	Parts []Part `json:"parts"`
}

Content is the equivalent of Message for other providers. https://ai.google.dev/api/caching?hl=en#Content

func (*Content) From

func (c *Content) From(in *genai.Message) error

From converts from the genai equivalent.

func (*Content) To

func (c *Content) To(out *genai.Message) error

To converts to the genai equivalent.

type CountTokensResponse added in v0.2.0

type CountTokensResponse struct {
	TotalTokens             int64 `json:"totalTokens"`
	CachedContentTokenCount int64 `json:"cachedContentTokenCount,omitzero"`
}

CountTokensResponse is the response from the countTokens API.

https://ai.google.dev/api/tokens#v1beta.CountTokensResponse

type Duration

type Duration time.Duration

Duration is a JSON-serializable time.Duration.

func (*Duration) IsZero

func (d *Duration) IsZero() bool

IsZero reports whether the value is zero.

func (*Duration) MarshalJSON

func (d *Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type ErrorResponse

type ErrorResponse struct {
	ErrorVal ErrorResponseError `json:"error"`
}

ErrorResponse represents the response structure for Gemini errors.

It is returned as an error.

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

func (*ErrorResponse) IsAPIError

func (e *ErrorResponse) IsAPIError() bool

IsAPIError implements base.ErrorResponseI.

type ErrorResponseError

type ErrorResponseError struct {
	Code    int64  `json:"code"` // 429
	Message string `json:"message"`
	Status  string `json:"status"` // "RESOURCE_EXHAUSTED"
	Details []struct {
		Type     string `json:"@type"`
		Reason   string `json:"reason"`
		Domain   string `json:"domain"`
		Metadata struct {
			Service string `json:"service"`
		} `json:"metadata"`
		FieldViolations []struct {
			Field       string `json:"field"`
			Description string `json:"description"`
		} `json:"fieldViolations"`
		Locale  string `json:"locale"`
		Message string `json:"message"`

		// "type.googleapis.com/google.rpc.QuotaFailure"
		Violations []struct {
			// "generativelanguage.googleapis.com/generate_requests_per_model_per_day"
			// "generativelanguage.googleapis.com/generate_requests_per_model"
			// "generativelanguage.googleapis.com/generate_content_paid_tier_input_token_count"
			QuotaMetric string `json:"quotaMetric"`
			// "GenerateRequestsPerDayPerProjectPerModel"
			// "GenerateRequestsPerMinutePerProjectPerModel"
			// "GenerateContentPaidTierInputTokensPerModelPerMinute"
			QuotaID         string `json:"quotaId"`
			QuotaDimensions struct {
				Location string `json:"location"` // "global"
				Model    string `json:"model"`    // model name
			} `json:"quotaDimensions"`
		} `json:"violations"`

		// "type.googleapis.com/google.rpc.Help"
		Links []struct {
			Description string `json:"description"`
			URL         string `json:"url"`
		} `json:"links"`

		// Type == "type.googleapis.com/google.rpc.RetryInfo"
		RetryDelay string `json:"retryDelay"` // "28s"
	} `json:"details"`
}

ErrorResponseError is the nested error in an error response.

type ExecutableCode

type ExecutableCode struct {
	Language string `json:"language,omitzero"` // Only PYTHON is supported as of March 2025.
	Code     string `json:"code,omitzero"`
}

ExecutableCode is documented at https://ai.google.dev/api/caching?hl=en#ExecutableCode

type Expiration

type Expiration struct {
	ExpireTime time.Time `json:"expireTime,omitzero"` // ISO 8601
	TTL        Duration  `json:"ttl,omitzero"`        // Duration; input only
}

Expiration must be embedded. Only one of the fields can be set.

type FileData

type FileData struct {
	MimeType string `json:"mimeType,omitzero"`
	FileURI  string `json:"fileUri,omitzero"`
}

FileData is documented at https://ai.google.dev/api/caching?hl=en#FileData

type FileListResponse added in v0.2.0

type FileListResponse struct {
	Files         []FileMetadata `json:"files,omitzero"`
	NextPageToken string         `json:"nextPageToken,omitzero"`
}

FileListResponse is the response from listing files.

https://ai.google.dev/api/files#method:-files.list

type FileMetadata added in v0.2.0

type FileMetadata struct {
	Name           string    `json:"name,omitzero"`
	DisplayName    string    `json:"displayName,omitzero"`
	MimeType       string    `json:"mimeType,omitzero"`
	SizeBytes      int64     `json:"sizeBytes,omitzero,string"`
	CreateTime     time.Time `json:"createTime,omitzero"`
	UpdateTime     time.Time `json:"updateTime,omitzero"`
	ExpirationTime time.Time `json:"expirationTime,omitzero"`
	SHA256Hash     string    `json:"sha256Hash,omitzero"`
	URI            string    `json:"uri,omitzero"`
	DownloadURI    string    `json:"downloadUri,omitzero"`
	State          FileState `json:"state,omitzero"`
	Source         string    `json:"source,omitzero"`
	Error          *Status   `json:"error,omitzero"`
}

FileMetadata is the metadata for an uploaded file.

https://ai.google.dev/api/files#File

type FileSearch added in v0.2.0

type FileSearch struct {
	FileSearchStoreNames []string `json:"fileSearchStoreNames,omitzero"`
	TopK                 int32    `json:"topK,omitzero"`
	MetadataFilter       string   `json:"metadataFilter,omitzero"`
}

FileSearch is documented at https://ai.google.dev/gemini-api/docs/file-search

type FileSearchStore added in v0.2.0

type FileSearchStore struct {
	Name                 string               `json:"name,omitzero"`
	DisplayName          string               `json:"displayName,omitzero"`
	CreateTime           time.Time            `json:"createTime,omitzero"`
	UpdateTime           time.Time            `json:"updateTime,omitzero"`
	State                FileSearchStoreState `json:"state,omitzero"`
	ActiveDocumentsCount int64                `json:"activeDocumentsCount,omitzero,string"`
	SizeBytes            int64                `json:"sizeBytes,omitzero,string"`
}

FileSearchStore represents a file search store resource.

https://ai.google.dev/gemini-api/docs/file-search

type FileSearchStoreDocument added in v0.2.0

type FileSearchStoreDocument struct {
	Name        string                       `json:"name,omitzero"`
	DisplayName string                       `json:"displayName,omitzero"`
	State       FileSearchStoreDocumentState `json:"state,omitzero"`
	CreateTime  time.Time                    `json:"createTime,omitzero"`
	UpdateTime  time.Time                    `json:"updateTime,omitzero"`
	SizeBytes   int64                        `json:"sizeBytes,omitzero,string"`
	MimeType    string                       `json:"mimeType,omitzero"`
	Error       *Status                      `json:"error,omitzero"`
}

FileSearchStoreDocument represents a document within a file search store.

https://ai.google.dev/gemini-api/docs/file-search

type FileSearchStoreDocumentListResponse added in v0.2.0

type FileSearchStoreDocumentListResponse struct {
	Documents     []FileSearchStoreDocument `json:"documents,omitzero"`
	NextPageToken string                    `json:"nextPageToken,omitzero"`
}

FileSearchStoreDocumentListResponse is the response from listing documents in a file search store.

type FileSearchStoreDocumentState added in v0.2.0

type FileSearchStoreDocumentState string

FileSearchStoreDocumentState represents the state of a document in a file search store.

const (
	FileSearchStoreDocumentStateUnspecified FileSearchStoreDocumentState = "STATE_UNSPECIFIED"
	FileSearchStoreDocumentStateProcessing  FileSearchStoreDocumentState = "STATE_PROCESSING"
	FileSearchStoreDocumentStateActive      FileSearchStoreDocumentState = "STATE_ACTIVE"
	FileSearchStoreDocumentStateFailed      FileSearchStoreDocumentState = "STATE_FAILED"
)

File search store document state values.

type FileSearchStoreListResponse added in v0.2.0

type FileSearchStoreListResponse struct {
	FileSearchStores []FileSearchStore `json:"fileSearchStores,omitzero"`
	NextPageToken    string            `json:"nextPageToken,omitzero"`
}

FileSearchStoreListResponse is the response from listing file search stores.

type FileSearchStoreState added in v0.2.0

type FileSearchStoreState string

FileSearchStoreState represents the state of a file search store.

const (
	FileSearchStoreStateUnspecified FileSearchStoreState = "STATE_UNSPECIFIED"
	FileSearchStoreStateActive      FileSearchStoreState = "STATE_ACTIVE"
)

File search store state values.

type FileState added in v0.2.0

type FileState string

FileState represents the processing state of an uploaded file.

https://ai.google.dev/api/files#State

const (
	FileStateUnspecified FileState = "STATE_UNSPECIFIED"
	FileStateProcessing  FileState = "PROCESSING"
	FileStateActive      FileState = "ACTIVE"
	FileStateFailed      FileState = "FAILED"
)

File state values.

type FinishReason

type FinishReason string

FinishReason is documented at https://ai.google.dev/api/generate-content?hl=en#FinishReason

const (
	// FinishStop is the natural stop point of the model or provided stop sequence.
	FinishStop FinishReason = "STOP"
	// FinishMaxTokens means the maximum number of tokens as specified in the request was reached.
	FinishMaxTokens FinishReason = "MAX_TOKENS"
	// FinishSafety means the response candidate content was flagged for safety reasons.
	FinishSafety FinishReason = "SAFETY"
	// FinishRecitation means the response candidate content was flagged for recitation reasons.
	FinishRecitation FinishReason = "RECITATION"
	// FinishLanguage means the response candidate content was flagged for using an unsupported language.
	FinishLanguage FinishReason = "LANGUAGE"
	// FinishOther is an unknown reason.
	FinishOther FinishReason = "OTHER"
	// FinishBlocklist means token generation stopped because the content contains forbidden terms.
	FinishBlocklist FinishReason = "BLOCKLIST"
	// FinishProhibitedContent means token generation stopped for potentially containing prohibited content.
	FinishProhibitedContent FinishReason = "PROHIBITED_CONTENT"
	// FinishSPII means token generation stopped because the content potentially contains Sensitive Personally Identifiable Information.
	FinishSPII FinishReason = "SPII"
	// FinishMalformed means the function call generated by the model is invalid.
	FinishMalformed FinishReason = "MALFORMED_FUNCTION_CALL"
	// FinishImageSafety means token generation stopped because generated images contain safety violations.
	FinishImageSafety FinishReason = "IMAGE_SAFETY"
)

func (FinishReason) ToFinishReason

func (f FinishReason) ToFinishReason() genai.FinishReason

ToFinishReason converts to a genai.FinishReason.

type Format

type Format string

Format is described at https://spec.openapis.org/oas/v3.0.3#data-types

Values outside of the consts are acceptable.

const (
	// FormatFloat is for TypeNumber only.
	FormatFloat Format = "float"
	// FormatDouble is for TypeNumber only.
	FormatDouble Format = "double"

	// FormatInt32 is for TypeInteger only.
	FormatInt32 Format = "int32"
	// FormatInt64 is for TypeInteger only.
	FormatInt64 Format = "int64"

	// FormatEnum is for TypeString only.
	FormatEnum Format = "enum"
	// FormatDate is for TypeString only.
	FormatDate Format = "date"
	// FormatDateTime is for TypeString only.
	FormatDateTime Format = "date-time"
	// FormatByte is for TypeString only.
	FormatByte Format = "byte"
	// FormatPassword is for TypeString only.
	FormatPassword Format = "password"
	// FormatEmail is for TypeString only.
	FormatEmail Format = "email"
	// FormatUUID is for TypeString only.
	FormatUUID Format = "uuid"
)

type FunctionCall

type FunctionCall struct {
	ID               string      `json:"id,omitzero"`
	Name             string      `json:"name,omitzero"`
	Args             StructValue `json:"args,omitzero"`
	ThoughtSignature []byte      `json:"thoughtSignature,omitzero"` // Returned by some models with thinking
}

FunctionCall is documented at https://ai.google.dev/api/caching?hl=en#FunctionCall

func (*FunctionCall) From

func (f *FunctionCall) From(in *genai.ToolCall) error

From converts from the genai equivalent.

func (*FunctionCall) To

func (f *FunctionCall) To(out *genai.ToolCall) error

To converts to the genai equivalent.

type FunctionDeclaration

type FunctionDeclaration struct {
	Name        string `json:"name,omitzero"`
	Description string `json:"description,omitzero"`
	Parameters  Schema `json:"parameters,omitzero"`
	Response    Schema `json:"response,omitzero"`
}

FunctionDeclaration is documented at https://ai.google.dev/api/caching?hl=en#FunctionDeclaration

type FunctionResponse

type FunctionResponse struct {
	ID       string      `json:"id,omitzero"`
	Name     string      `json:"name,omitzero"`
	Response StructValue `json:"response,omitzero"`
}

FunctionResponse is documented at https://ai.google.dev/api/caching?hl=en#FunctionResponse

func (*FunctionResponse) From

func (f *FunctionResponse) From(in *genai.ToolCallResult)

From converts from the genai equivalent.

type GenOption added in v0.2.0

type GenOption struct {
	// ThinkingBudget is the maximum number of tokens the LLM can use to reason about the answer.
	//
	// From https://ai.google.dev/gemini-api/docs/thinking#set-budget
	//
	// # gemini-2.5-pro
	//
	// - Default: Dynamic thinking, model decides when and how much to think
	// - Range: 128 to 32768
	// - Cannot disable thinking
	// - Dynamic thinking: -1
	//
	// # gemini-2.5-flash
	//
	// - Default: Dynamic thinking, model decides when and how much to think
	// - Range: 0 to 24576
	// - Disable thinking with 0
	// - Dynamic thinking: -1
	//
	// # gemini-2.5-flash-lite
	//
	// - Default: Model does not think
	// - Range: 512 to 24576
	// - Disable thinking with 0
	// - Dynamic thinking: -1
	ThinkingBudget int64

	// CodeExecution enables the code execution tool, allowing the model to generate and run Python code.
	//
	// https://ai.google.dev/gemini-api/docs/code-execution
	CodeExecution bool

	// URLContext enables the URL context tool, allowing the model to fetch and process web pages.
	//
	// https://ai.google.dev/gemini-api/docs/url-context
	URLContext bool

	// FileSearch enables the file search tool with the specified configuration.
	//
	// https://ai.google.dev/gemini-api/docs/file-search
	FileSearch *FileSearch
}

GenOption defines Gemini specific options.

func (*GenOption) Validate added in v0.2.0

func (o *GenOption) Validate() error

Validate implements genai.Validatable.

type GoogleSearch

type GoogleSearch struct{}

GoogleSearch is "documented" at https://ai.google.dev/gemini-api/docs/google-search

type GroundingChunk added in v0.2.0

type GroundingChunk struct {
	Web              GroundingChunkWeb              `json:"web,omitzero"`
	RetrievedContext GroundingChunkRetrievedContext `json:"retrievedContext,omitzero"`
}

GroundingChunk is documented at https://ai.google.dev/api/generate-content?hl=en#GroundingChunk

type GroundingChunkRetrievedContext added in v0.2.0

type GroundingChunkRetrievedContext struct {
	URI          string `json:"uri,omitzero"`
	Title        string `json:"title,omitzero"`
	Text         string `json:"text,omitzero"`
	DocumentName string `json:"documentName,omitzero"`
	// FileSearchStore is the store name that sourced this chunk. Not in the official Go SDK but returned by the API.
	FileSearchStore string `json:"fileSearchStore,omitzero"`
}

GroundingChunkRetrievedContext is documented at https://ai.google.dev/api/generate-content?hl=en#RetrievedContext

type GroundingChunkWeb added in v0.2.0

type GroundingChunkWeb struct {
	URI   string `json:"uri,omitzero"`
	Title string `json:"title,omitzero"`
}

GroundingChunkWeb is documented at https://ai.google.dev/api/generate-content?hl=en#Web

type GroundingMetadata

type GroundingMetadata struct {
	GroundingChunks   []GroundingChunk   `json:"groundingChunks,omitzero"`
	GroundingSupports []GroundingSupport `json:"groundingSupports,omitzero"`
	WebSearchQueries  []string           `json:"webSearchQueries,omitzero"`
	SearchEntryPoint  SearchEntryPoint   `json:"searchEntryPoint,omitzero"`
	RetrievalMetadata RetrievalMetadata  `json:"retrievalMetadata,omitzero"`
}

GroundingMetadata is documented at https://ai.google.dev/api/generate-content?hl=en#GroundingMetadata

func (*GroundingMetadata) IsZero

func (g *GroundingMetadata) IsZero() bool

IsZero reports whether the value is zero.

func (*GroundingMetadata) To

func (g *GroundingMetadata) To() ([]genai.Reply, error)

To converts to the genai equivalent.

type GroundingSupport added in v0.2.0

type GroundingSupport struct {
	GroundingChunkIndices []int64   `json:"groundingChunkIndices,omitzero"`
	ConfidenceScores      []float64 `json:"confidenceScores,omitzero"`
	Segment               Segment   `json:"segment,omitzero"`
}

GroundingSupport is documented at https://ai.google.dev/api/generate-content?hl=en#GroundingSupport

type ImageInstance

type ImageInstance struct {
	Prompt string `json:"prompt"`
	Image  struct {
		BytesBase64Encoded []byte `json:"bytesBase64Encoded,omitzero"`
		MimeType           string `json:"mimeType,omitzero"`
	} `json:"image,omitzero"`
}

ImageInstance is not really documented, better to read the SDK code and guess, since they don't use proper structs there either and it's all hand written.

type ImageOutput

type ImageOutput struct {
	MimeType           string  `json:"mimeType,omitzero"` // "image/jpeg"
	CompressionQuality float64 `json:"compressionQuality,omitzero"`
}

ImageOutput is the provider-specific image output configuration.

type ImageParameters

type ImageParameters struct {
	// Both image and video:
	SampleCount      int64  `json:"sampleCount,omitzero"` // Number of images to generate. Default to 4.
	AspectRatio      string `json:"aspectRatio,omitzero"` // "1:1", "3:4", "4:3", "9:16", and "16:9".
	PersonGeneration string `json:"personGeneration"`     // "dont_allow", "allow_adult", "allow_all" (not valid in EU, UK, CH, MENA locations)
	EnhancePrompt    bool   `json:"enhancePrompt,omitzero"`

	// Image only.
	GuidanceScale           float64     `json:"guidanceScale,omitzero"`
	Seed                    int64       `json:"seed,omitzero"`
	SafetySetting           string      `json:"safetySetting,omitzero"`
	IncludeSafetyAttributes bool        `json:"includeSafetyAttributes,omitzero"`
	IncludeRAIReason        bool        `json:"includeRAIReason,omitzero"`
	Language                string      `json:"language,omitzero"`
	OutputOptions           ImageOutput `json:"outputOptions,omitzero"`
	AddWatermark            bool        `json:"addWatermark,omitzero"`
	SampleImageSize         string      `json:"SampleImageSize,omitzero"`

	// Video only.
	DurationSeconds int64  `json:"durationSeconds,omitzero"`
	NegativePrompt  string `json:"negativePrompt,omitzero"`
}

ImageParameters is not really documented, better to read the SDK code and guess, since they don't use proper structs there either and it's all hand written.

type ImageRequest

type ImageRequest struct {
	// There should be only one instance.
	Instances  []ImageInstance `json:"instances"`
	Parameters ImageParameters `json:"parameters"`
}

ImageRequest is not really documented. It is used for both image and video generation.

See https://ai.google.dev/gemini-api/docs/imagen#imagen

See https://pkg.go.dev/google.golang.org/genai#GenerateImagesConfig for image generation and https://ai.google.dev/gemini-api/docs/video for video generation.

See generateImagesConfigToMldev() in https://github.com/googleapis/go-genai/blob/main/models.go or GenerateImagesConfig and generateImagesInternal() in https://github.com/googleapis/js-genai/blob/main/src/models.ts and generateImagesParametersToMldev() and generateImagesConfigToMldev()

As of 2025-08, Google Vertex AI API supports way more features than the Gemini API. For example: specifying the video's last frame, whether to generate audio, compression level, resolution, randomness seed, sending updates to a pubsub topic (instead of polling), etc.

func (*ImageRequest) Init

func (i *ImageRequest) Init(msg *genai.Message, model string, mod genai.Modalities, opts ...genai.GenOption) error

Init initializes the request from the given parameters.

type ImageResponse

type ImageResponse struct {
	Predictions []struct {
		MimeType         string `json:"mimeType"`
		SafetyAttributes struct {
			Categories []string  `json:"categories"`
			Scores     []float64 `json:"scores"`
		} `json:"safetyAttributes"`
		BytesBase64Encoded []byte `json:"bytesBase64Encoded"`
		ContentType        string `json:"contentType"` // "Positive Prompt"
	} `json:"predictions"`
}

ImageResponse is the provider-specific image generation response.

type LogprobsResult

type LogprobsResult struct {
	TopCandidates    []TopCandidate   `json:"topCandidates"`
	ChosenCandidates []TokenCandidate `json:"chosenCandidates"`
}

LogprobsResult is documented at https://ai.google.dev/api/generate-content#LogprobsResult

func (*LogprobsResult) To

func (l *LogprobsResult) To() [][]genai.Logprob

To converts to the genai equivalent.

type MediaResolution

type MediaResolution string

MediaResolution is documented at https://ai.google.dev/api/generate-content#MediaResolution

const (
	MediaResolutionUnspecified MediaResolution = ""       // "MEDIA_RESOLUTION_UNSPECIFIED"
	MediaResolutionLow         MediaResolution = "LOW"    // 64 tokens
	MediaResolutionMedium      MediaResolution = "MEDIUM" // 256 tokens
	MediaResolutionHigh        MediaResolution = "HIGH"   // zoomed reframing with 256 tokens
)

Media resolution values.

type Modality

type Modality string

Modality is documented at https://ai.google.dev/api/generate-content#Modality

const (
	ModalityUnspecified Modality = "" // "MODALITY_UNSPECIFIED"
	ModalityAudio       Modality = "AUDIO"
	ModalityImage       Modality = "IMAGE"
	ModalityText        Modality = "TEXT"
)

Modality values.

type ModalityTokenCount

type ModalityTokenCount struct {
	Modality   Modality `json:"modality"`
	TokenCount int64    `json:"tokenCount"`
}

ModalityTokenCount is documented at https://ai.google.dev/api/generate-content?hl=en#v1beta.ModalityTokenCount

type Model

type Model struct {
	Name                       string   `json:"name"`
	BaseModelID                string   `json:"baseModelId"`
	Version                    string   `json:"version"`
	DisplayName                string   `json:"displayName"`
	Description                string   `json:"description"`
	InputTokenLimit            int64    `json:"inputTokenLimit"`
	OutputTokenLimit           int64    `json:"outputTokenLimit"`
	SupportedGenerationMethods []string `json:"supportedGenerationMethods"` // "batchGenerateContent", "bidiGenerateContent", "createCachedContent", "countTokens", "countTextTokens", "embedText", "generateContent", "predict", "predictLongRunning"
	Temperature                float64  `json:"temperature"`
	MaxTemperature             float64  `json:"maxTemperature"`
	TopP                       float64  `json:"topP"`
	TopK                       int64    `json:"topK"`
	Thinking                   bool     `json:"thinking"`
}

Model is documented at https://ai.google.dev/api/models#Model

func (*Model) Context

func (m *Model) Context() int64

Context implements genai.Model.

func (*Model) GetID

func (m *Model) GetID() string

GetID implements genai.Model.

func (*Model) String

func (m *Model) String() string

type ModelsResponse

type ModelsResponse struct {
	Models        []Model `json:"models"`
	NextPageToken string  `json:"nextPageToken"`
}

ModelsResponse represents the response structure for Gemini models listing.

func (*ModelsResponse) ToModels

func (r *ModelsResponse) ToModels() []genai.Model

ToModels converts Gemini models to genai.Model interfaces.

type Operation

type Operation struct {
	Name     string         `json:"name"`
	Metadata map[string]any `json:"metadata"`
	Done     bool           `json:"done"`
	// One of the following:
	Error    Status `json:"error"`
	Response struct {
		Type string `json:"@type"`
		// Video generation response.
		GenerateVideoResponse struct {
			GeneratedSamples []struct {
				Video struct {
					URI        string `json:"uri"`
					VideoBytes []byte `json:"videoBytes"` // Not set in Gemini API
					MimeType   string `json:"mimeType"`   // Not set in Gemini API
				} `json:"video"`
				RAIMediaFilteredCount   int64    `json:"raiMediaFilteredCount"`
				RAIMediaFilteredReasons []string `json:"raiMediaFilteredReasons"`
			} `json:"generatedSamples"`
		} `json:"generateVideoResponse"`
		// File search store document upload response.
		DocumentName string `json:"documentName,omitzero"`
		Parent       string `json:"parent,omitzero"`
		MimeType     string `json:"mimeType,omitzero"`
		SizeBytes    string `json:"sizeBytes,omitzero"`
	} `json:"response"`
}

Operation is documented at https://ai.google.dev/api/batch-mode#Operation

See generateVideosResponseFromMldev in js-genai for more details.

type Part

type Part struct {
	Thought          bool   `json:"thought,omitzero"`
	ThoughtSignature []byte `json:"thoughtSignature,omitzero"`

	// Union:
	Text                string              `json:"text,omitzero"`
	InlineData          Blob                `json:"inlineData,omitzero"` // Uploaded with /v1beta/cachedContents. Content is deleted after 1 hour.
	FunctionCall        FunctionCall        `json:"functionCall,omitzero"`
	FunctionResponse    FunctionResponse    `json:"functionResponse,omitzero"`
	FileData            FileData            `json:"fileData,omitzero"`            // Uploaded with /upload/v1beta/files. Files are deleted after 2 days.
	ExecutableCode      ExecutableCode      `json:"executableCode,omitzero"`      // TODO: Map to genai types.
	CodeExecutionResult CodeExecutionResult `json:"codeExecutionResult,omitzero"` // TODO: Map to genai types.

	// Union:
	VideoMetadata VideoMetadata `json:"videoMetadata,omitzero"`
}

Part is a union that only has one of the field set. Part is the equivalent of Content for other providers.

https://ai.google.dev/api/caching?hl=en#Part

func (*Part) FromReply

func (p *Part) FromReply(in *genai.Reply) error

FromReply converts from a genai reply.

func (*Part) FromRequest

func (p *Part) FromRequest(in *genai.Request) error

FromRequest converts from a genai request.

type ResponseCandidate

type ResponseCandidate struct {
	Content       Content      `json:"content"`
	FinishReason  FinishReason `json:"finishReason"`
	FinishMessage string       `json:"finishMessage,omitzero"` // Newer models with thinking return this
	// https://ai.google.dev/api/generate-content?hl=en#v1beta.SafetyRating
	SafetyRatings []struct {
		// https://ai.google.dev/api/generate-content?hl=en#v1beta.HarmCategory
		Category string `json:"category"`
		// https://ai.google.dev/api/generate-content?hl=en#HarmProbability
		Probability string `json:"probability"`
		Blocked     bool   `json:"blocked"`
	} `json:"safetyRatings"`
	// https://ai.google.dev/api/generate-content?hl=en#v1beta.CitationMetadata
	CitationMetadata struct {
		// https://ai.google.dev/api/generate-content?hl=en#CitationSource
		CitationSources []struct {
			StartIndex int64  `json:"startIndex"`
			EndIndex   int64  `json:"endIndex"`
			URI        string `json:"uri"`
			License    string `json:"license"`
		} `json:"citationSources"`
	} `json:"citationMetadata"`
	TokenCount int64 `json:"tokenCount"`
	// https://ai.google.dev/api/generate-content?hl=en#GroundingAttribution
	GroundingAttributions []struct {
		SourceID string  `json:"sourceId"`
		Countent Content `json:"countent"`
	} `json:"groundingAttributions"`
	GroundingMetadata  GroundingMetadata  `json:"groundingMetadata"`
	UrlContextMetadata UrlContextMetadata `json:"urlContextMetadata"`
	AvgLogprobs        float64            `json:"avgLogprobs"`
	LogprobsResult     LogprobsResult     `json:"logprobsResult"`
	Index              int64              `json:"index"`
}

ResponseCandidate is described at https://ai.google.dev/api/generate-content?hl=en#v1beta.Candidate

It is essentially a "Message".

func (*ResponseCandidate) To

func (r *ResponseCandidate) To(out *genai.Message) error

To converts to the genai equivalent.

type RetrievalMetadata added in v0.2.0

type RetrievalMetadata struct {
	GoogleSearchDynamicRetrievalScore float64 `json:"googleSearchDynamicRetrievalScore,omitzero"`
}

RetrievalMetadata is documented at https://ai.google.dev/api/generate-content?hl=en#RetrievalMetadata

type Schema

type Schema struct {
	// Optional. The value should be validated against any (one or more) of the subschemas
	// in the list.
	AnyOf []*Schema `json:"anyOf,omitzero"`
	// Optional. Default value of the data.
	Default any `json:"default,omitzero"`
	// Optional. The description of the data.
	Description string `json:"description,omitzero"`
	// Optional. Possible values of the element of primitive type with enum format. Examples:
	// 1. We can define direction as : {type:STRING, format:enum, enum:["EAST", NORTH",
	// "SOUTH", "WEST"]} 2. We can define apartment number as : {type:INTEGER, format:enum,
	// enum:["101", "201", "301"]}
	Enum []string `json:"enum,omitzero"`
	// Optional. Example of the object. Will only populated when the object is the root.
	Example any `json:"example,omitzero"`
	// Optional. The format of the data. Supported formats: for NUMBER type: "float", "double"
	// for INTEGER type: "int32", "int64" for STRING type: "email", "byte", etc
	Format Format `json:"format,omitzero"`
	// Optional. SCHEMA FIELDS FOR TYPE ARRAY Schema of the elements of Type.ARRAY.
	Items *Schema `json:"items,omitzero"`
	// Optional. Maximum number of the elements for Type.ARRAY.
	MaxItems int64 `json:"maxItems,omitzero,string"`
	// Optional. Maximum length of the Type.STRING
	MaxLength int64 `json:"maxLength,omitzero,string"`
	// Optional. Maximum number of the properties for Type.OBJECT.
	MaxProperties int64 `json:"maxProperties,omitzero,string"`
	// Optional. Maximum value of the Type.INTEGER and Type.NUMBER
	Maximum float64 `json:"maximum,omitzero"`
	// Optional. Minimum number of the elements for Type.ARRAY.
	MinItems int64 `json:"minItems,omitzero,string"`
	// Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING
	MinLength int64 `json:"minLength,omitzero,string"`
	// Optional. Minimum number of the properties for Type.OBJECT.
	MinProperties int64 `json:"minProperties,omitzero,string"`
	// Optional. Minimum value of the Type.INTEGER and Type.NUMBER.
	Minimum float64 `json:"minimum,omitzero"`
	// Optional. Indicates if the value may be null.
	Nullable bool `json:"nullable,omitzero"`
	// Optional. Pattern of the Type.STRING to restrict a string to a regular expression.
	Pattern string `json:"pattern,omitzero"`
	// Optional. SCHEMA FIELDS FOR TYPE OBJECT Properties of Type.OBJECT.
	Properties map[string]Schema `json:"properties,omitzero"`
	// Optional. The order of the properties. Not a standard field in open API spec. Only
	// used to support the order of the properties.
	PropertyOrdering []string `json:"propertyOrdering,omitzero"`
	// Optional. Required properties of Type.OBJECT.
	Required []string `json:"required,omitzero"`
	// Optional. The title of the Schema.
	Title string `json:"title,omitzero"`
	// Optional. The type of the data.
	Type Type `json:"type,omitzero"`
}

Schema is documented at https://ai.google.dev/api/caching#Schema

The official Go SDK documentation is at https://pkg.go.dev/google.golang.org/genai#Schema

Adapted to have less pointers and use omitzero.

func (*Schema) FromGoObj

func (s *Schema) FromGoObj(v any) error

FromGoObj generates a gemini.Schema from a Go object.

func (*Schema) FromGoType

func (s *Schema) FromGoType(t reflect.Type, tag reflect.StructTag, parent string) error

FromGoType converts a Go type to a Schema.

type SearchEntryPoint added in v0.2.0

type SearchEntryPoint struct {
	RenderedContent string `json:"renderedContent,omitzero"`
	SDKBlob         []byte `json:"sdkBlob,omitzero"` // JSON encoded list of (search term,search url) results
}

SearchEntryPoint is documented at https://ai.google.dev/api/generate-content?hl=en#SearchEntryPoint

type Segment added in v0.2.0

type Segment struct {
	PartIndex  int64  `json:"partIndex,omitzero"`
	StartIndex int64  `json:"startIndex,omitzero"`
	EndIndex   int64  `json:"endIndex,omitzero"`
	Text       string `json:"text,omitzero"`
}

Segment is documented at https://ai.google.dev/api/generate-content?hl=en#Segment

type Status

type Status struct {
	Code    int64          `json:"code"`
	Message string         `json:"message"`
	Details map[string]any `json:"details"`
}

Status is documented at https://ai.google.dev/api/files#v1beta.Status

type StructValue

type StructValue map[string]any

StructValue is documented at https://protobuf.dev/reference/protobuf/google.protobuf/#struct

type ThinkingConfig

type ThinkingConfig struct {
	// IncludeThoughts has no effect since January 2025 according to
	// https://discuss.ai.google.dev/t/thoughts-are-missing-cot-not-included-anymore/63653/13
	IncludeThoughts bool  `json:"includeThoughts"` // Must not be omitted.
	ThinkingBudget  int64 `json:"thinkingBudget"`  // Must not be omitted. [0, 24576]
}

ThinkingConfig is documented at https://ai.google.dev/api/generate-content?hl=en#ThinkingConfig See https://ai.google.dev/gemini-api/docs/thinking#rest

type TokenCandidate

type TokenCandidate struct {
	Token          string  `json:"token"`
	TokenID        int64   `json:"tokenId"`
	LogProbability float64 `json:"logProbability"`
}

TokenCandidate is documented at https://ai.google.dev/api/generate-content#TokenCandidate

type Tool

type Tool struct {
	FunctionDeclarations []FunctionDeclaration `json:"functionDeclarations,omitzero"`
	// https://ai.google.dev/api/caching?hl=en#GoogleSearchRetrieval
	GoogleSearchRetrieval struct {
		// https://ai.google.dev/api/caching?hl=en#DynamicRetrievalConfig
		DynamicRetrievalConfig struct {
			// https://ai.google.dev/api/caching?hl=en#Mode
			Mode             string  `json:"mode,omitzero"` // MODE_UNSPECIFIED, MODE_DYNAMIC
			DynamicThreshold float64 `json:"dynamicThreshold,omitzero"`
		} `json:"dynamicRetrievalConfig,omitzero"`
	} `json:"googleSearchRetrieval,omitzero"`
	CodeExecution *struct{} `json:"codeExecution,omitzero"`
	URLContext    *struct{} `json:"urlContext,omitzero"`
	// GoogleSearch presence signifies that it should be enabled,
	GoogleSearch *GoogleSearch `json:"googleSearch,omitzero"`
	// FileSearch enables file search tool.
	FileSearch *FileSearch `json:"fileSearch,omitzero"`
}

Tool is documented at https://ai.google.dev/api/caching?hl=en#Tool

type ToolConfig

type ToolConfig struct {
	// https://ai.google.dev/api/caching?hl=en#FunctionCallingConfig
	FunctionCallingConfig struct {
		Mode                 ToolMode `json:"mode,omitzero"`
		AllowedFunctionNames []string `json:"allowedFunctionNames,omitzero"`
	} `json:"functionCallingConfig,omitzero"`
}

ToolConfig is documented at https://ai.google.dev/api/caching?hl=en#ToolConfig

type ToolMode

type ToolMode string

ToolMode is documented at https://ai.google.dev/api/caching?hl=en#Mode_1

const (
	// ToolModeUnspecified is an unspecified function calling mode. This value should not be used.
	ToolModeUnspecified ToolMode = "" // "MODE_UNSPECIFIED"
	// ToolModeAuto is the default model behavior, model decides to predict either a function call or a natural language response.
	ToolModeAuto ToolMode = "AUTO"
	// ToolModeAny means the model is constrained to always predicting a function call only.
	ToolModeAny ToolMode = "ANY"
	// ToolModeNone means the model will not predict any function call.
	ToolModeNone ToolMode = "NONE"
	// ToolModeValidated means the model decides to predict either a function call or a natural language response, but will validate
	// function calls with constrained decoding.
	ToolModeValidated ToolMode = "VALIDATED"
)

type TopCandidate

type TopCandidate struct {
	Candidates []TokenCandidate `json:"candidates"`
}

TopCandidate is documented at https://ai.google.dev/api/generate-content#TopCandidates

type Type

type Type string

Type is documented at https://ai.google.dev/api/caching#Type

The official Go SDK documentation is at https://pkg.go.dev/google.golang.org/genai#Type

const (
	TypeUnspecified Type = "TYPE_UNSPECIFIED"
	TypeString      Type = "STRING"
	TypeNumber      Type = "NUMBER"
	TypeInteger     Type = "INTEGER"
	TypeBoolean     Type = "BOOLEAN"
	TypeArray       Type = "ARRAY"
	TypeObject      Type = "OBJECT"
	TypeNull        Type = "NULL"
)

Type values.

type UrlContextMetadata added in v0.2.0

type UrlContextMetadata struct {
	UrlMetadata []UrlMetadataEntry `json:"urlMetadata,omitzero"`
}

UrlContextMetadata is documented at https://ai.google.dev/gemini-api/docs/url-context

func (*UrlContextMetadata) To added in v0.2.0

func (u *UrlContextMetadata) To() []genai.Reply

To converts to the genai equivalent.

type UrlMetadataEntry added in v0.2.0

type UrlMetadataEntry struct {
	RetrievedUrl       string `json:"retrievedUrl,omitzero"`
	UrlRetrievalStatus string `json:"urlRetrievalStatus,omitzero"`
}

UrlMetadataEntry is a single URL retrieval result.

type UsageMetadata

type UsageMetadata struct {
	PromptTokenCount           int64                `json:"promptTokenCount"`
	CachedContentTokenCount    int64                `json:"cachedContentTokenCount"`
	CandidatesTokenCount       int64                `json:"candidatesTokenCount"`
	ToolUsePromptTokenCount    int64                `json:"toolUsePromptTokenCount"`
	ThoughtsTokenCount         int64                `json:"thoughtsTokenCount"`
	TotalTokenCount            int64                `json:"totalTokenCount"`
	PromptTokensDetails        []ModalityTokenCount `json:"promptTokensDetails"`
	CacheTokensDetails         []ModalityTokenCount `json:"cacheTokensDetails"`
	CandidatesTokensDetails    []ModalityTokenCount `json:"candidatesTokensDetails"`
	ToolUsePromptTokensDetails []ModalityTokenCount `json:"toolUsePromptTokensDetails"`
}

UsageMetadata is documented at https://ai.google.dev/api/generate-content?hl=en#UsageMetadata

type Value

type Value struct {
	NullValue   int64       `json:"null_value,omitzero"`
	NumberValue float64     `json:"number_value,omitzero"`
	StringValue string      `json:"string_value,omitzero"`
	BoolValue   bool        `json:"bool_value,omitzero"`
	StructValue StructValue `json:"struct_value,omitzero"`
	ListValue   []Value     `json:"list_value,omitzero"`
}

Value is documented at https://protobuf.dev/reference/protobuf/google.protobuf/#value

type VideoMetadata

type VideoMetadata struct {
	StartOffset Duration `json:"startOffset,omitzero"`
	EndOffset   Duration `json:"endOffset,omitzero"`
	FPS         int64    `json:"fps,omitzero"` // Default: 1.0, range ]0, 24]
}

VideoMetadata is documented at https://ai.google.dev/api/caching#VideoMetadata

Jump to

Keyboard shortcuts

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