Alloy.Provider.Gemini (alloy v0.16.0)

Copy Markdown View Source

Provider for Google's Gemini GenerateContent API.

Normalizes Gemini's contents / parts wire format to Alloy messages, including function calling and thinking-part round-tripping.

Config

Required:

  • :api_key - Gemini API key string, or a 0-arity callback returning String.t() | {:ok, String.t()} | {:error, term()} invoked per HTTP attempt (inside a bounded worker when :timeout_ms is finite or :cancel_token is set). When passing a callback via Alloy.run/2, also set :origin_key to a stable non-empty string for account isolation.
  • :model - Model name, for example "gemini-3.8-flash" or "gemini-3.5-flash-lite". Gemini 2.5 models are open only to projects that already used them.

Optional:

  • :auth_scheme - :api_key (default, sends x-goog-api-key) or :bearer (sends Authorization: Bearer for Google OAuth / ADC access tokens; pair with a stable :origin_key when passing via Alloy.run/2)
  • :origin_key - Account identifier used for cross-origin provenance hashing (required for function :api_key and auth_scheme: :bearer; recommended for static keys when persisting transcripts with file attachments across key rotation)
  • :cached_content - Gemini cached content resource name (e.g. "cachedContents/..."). Because Gemini requires systemInstruction, tools, and toolConfig to be baked into the cached content resource, setting :cached_content omits systemInstruction and functionDeclarations from the wire body (while keeping Alloy.run/2 local tool handlers active) and rejects explicit provider-level :tool_config, :google_search, :url_context, or :code_execution conflicts.
  • :google_search - true or config map to enable Google Search grounding
  • :url_context - true or config map to enable URL context retrieval
  • :code_execution - true or config map to enable Gemini code execution
  • :max_tokens - Max output tokens, sent as maxOutputTokens. Thinking tokens count against it. When unset, the model's own output limit applies (Alloy 0.12.4 and earlier sent 4096)
  • :system_prompt - System prompt string
  • :api_url - Base URL (default: "https://generativelanguage.googleapis.com")
  • :api_version - API version path (default: "v1beta")
  • :generation_config - Raw Gemini generationConfig fields (see Thinking)
  • :reasoning - Portable reasoning config (nil | :off | :low | :medium | :high | :xhigh | %{effort: ..., display: :summarized | :omitted}). :off sends thinkingConfig.thinkingBudget: 0; :low..:xhigh send thinkingConfig.thinkingLevel (:xhigh maps to "HIGH") and optional includeThoughts. Explicit :generation_config or :extra_body thinkingConfig takes precedence (see Thinking).

  • :tool_config - Raw Gemini toolConfig
  • :safety_settings - Raw Gemini safetySettings
  • :extra_headers - Additional headers as [{name, value}]
  • :extra_body - Additional top-level request body fields, merged last with stringified keys ("generationConfig" is merged one level deep so sibling caller keys coexist with :max_tokens / :output_schema while explicit caller keys win)
  • :req_options - Additional options passed to Req

Thinking

Gemini 3 models think by default. Set the depth with thinkingLevel ("MINIMAL", "LOW", "MEDIUM" or "HIGH"; each model accepts a subset) under thinkingConfig. thinkingBudget, a token count, is the older control: Gemini 2.5 models need it, and Gemini 3 still accepts it. Never send both; the API rejects the request. Portable :reasoning maps :off to thinkingBudget: 0 and :low..:xhigh to thinkingLevel (so when targeting Gemini 2.5, pass explicit generation_config: %{"thinkingConfig" => %{"thinkingBudget" => ...}} or set reasoning: nil on the Gemini provider entry). Thinking tokens are billed as output and count against :max_tokens, so lower thinkingLevel rather than :max_tokens to save cost. Set includeThoughts: true to get thought summaries back as "thinking" blocks.

Stop reasons

MAX_TOKENS maps to :max_tokens. Safety, recitation and other content filters, and blocked prompts, map to :refusal, with the reason in response_metadata.stop_details. A malformed or unexpected function call returns {:error, %Alloy.Provider.Error{}}, which the loop retries unless output was already streamed.

Example

Alloy.run("Summarize this code.",
  provider: {Alloy.Provider.Gemini,
    api_key: System.get_env("GEMINI_API_KEY"),
    model: "gemini-3.8-flash",
    generation_config: %{thinkingConfig: %{thinkingLevel: "LOW"}}
  }
)

Summary

Types

Configuration for the Gemini provider. See the module doc for field semantics.

Types

config()

@type config() :: %{
  :api_key => Alloy.Provider.api_key(),
  :model => String.t(),
  optional(:auth_scheme) => :api_key | :bearer,
  optional(:max_tokens) => pos_integer(),
  optional(:system_prompt) => String.t(),
  optional(:api_url) => String.t(),
  optional(:api_version) => String.t(),
  optional(:origin_key) => String.t(),
  optional(:cached_content) => String.t(),
  optional(:google_search) => boolean() | map(),
  optional(:url_context) => boolean() | map(),
  optional(:code_execution) => boolean() | map(),
  optional(:generation_config) => map() | keyword() | nil,
  optional(:reasoning) => Alloy.Provider.reasoning(),
  optional(:on_event) => (term() -> :ok),
  optional(:tool_config) => map(),
  optional(:safety_settings) => [map()],
  optional(:extra_headers) => [{String.t(), String.t()}],
  optional(:extra_body) => map(),
  optional(:req_options) => keyword()
}

Configuration for the Gemini provider. See the module doc for field semantics.