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 returningString.t() | {:ok, String.t()} | {:error, term()}invoked per HTTP attempt (inside a bounded worker when:timeout_msis finite or:cancel_tokenis set). When passing a callback viaAlloy.run/2, also set:origin_keyto 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, sendsx-goog-api-key) or:bearer(sendsAuthorization: Bearerfor Google OAuth / ADC access tokens; pair with a stable:origin_keywhen passing viaAlloy.run/2):origin_key- Account identifier used for cross-origin provenance hashing (required for function:api_keyandauth_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 requiressystemInstruction,tools, andtoolConfigto be baked into the cached content resource, setting:cached_contentomitssystemInstructionandfunctionDeclarationsfrom the wire body (while keepingAlloy.run/2local tool handlers active) and rejects explicit provider-level:tool_config,:google_search,:url_context, or:code_executionconflicts.:google_search-trueor config map to enable Google Search grounding:url_context-trueor config map to enable URL context retrieval:code_execution-trueor config map to enable Gemini code execution:max_tokens- Max output tokens, sent asmaxOutputTokens. Thinking tokens count against it. When unset, the model's own output limit applies (Alloy 0.12.4 and earlier sent4096):system_prompt- System prompt string:api_url- Base URL (default:"https://generativelanguage.googleapis.com"):api_version- API version path (default:"v1beta"):generation_config- Raw GeminigenerationConfigfields (see Thinking):reasoning- Portable reasoning config (nil | :off | :low | :medium | :high | :xhigh | %{effort: ..., display: :summarized | :omitted}).:offsendsthinkingConfig.thinkingBudget: 0;:low..:xhighsendthinkingConfig.thinkingLevel(:xhighmaps to"HIGH") and optionalincludeThoughts. Explicit:generation_configor:extra_bodythinkingConfigtakes precedence (see Thinking).:tool_config- Raw GeminitoolConfig:safety_settings- Raw GeminisafetySettings: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_schemawhile 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
@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.