Gemini Provider
The Gemini provider (create_gemini()) supports Google’s generative models, known for their large context windows and native multimodal capabilities.
Configuration
Set your API key in your .Renviron:
GEMINI_API_KEY="AIza..."Google Search Grounding
One of Gemini’s unique features is native Google Search grounding. This allows the model to search the web to answer queries.
library(aisdk)
provider <- create_gemini()
model <- provider$language_model("gemini-2.0-flash")
# Enable Google Search
response <- generate_text(
model = model,
prompt = "What are the latest results for the 2024 US Election?",
tools = list(
list(google_search = list()) # Built-in tool
)
)Advanced Tool Options
Gemini supports specific tool types like google_search_retrieval for more fine-grained control over when the model uses search:
tools = list(
list(google_search_retrieval = list(
dynamicRetrievalConfig = list(
mode = "MODE_DYNAMIC",
dynamicThreshold = 0.3
)
))
)Multimodal Input
Gemini is natively multimodal. The SDK supports passing image data to Gemini models (see the Multimodal vignette for details).
library(aisdk)
model <- create_gemini()$language_model("gemini-2.5-flash")
result <- analyze_image(
model = model,
image = "inst/extdata/example.png",
prompt = "Describe the main objects in this image."
)
cat(result$text)You can also build multimodal messages manually with input_text() and input_image() if you need full control over the prompt structure.
Image Generation
Gemini is currently the first provider in aisdk with a dedicated image_model() path.
library(aisdk)
provider <- create_gemini()
image_model <- provider$image_model("gemini-3.1-flash-image-preview")
result <- generate_image(
model = image_model,
prompt = "A clean editorial photo of a blue ceramic mug on a white table.",
output_dir = tempdir()
)
result$images[[1]]$pathSee the Image Generation vignette for the full workflow.
Image Editing
Gemini image models can also perform prompt-based image edits:
library(aisdk)
result <- edit_image(
model = create_gemini()$image_model("gemini-3.1-flash-image-preview"),
image = "inst/extdata/product.png",
prompt = "Change the mug color to cobalt blue.",
output_dir = tempdir()
)
result$images[[1]]$pathAt the moment, aisdk supports prompt-based image editing for Gemini, but does not yet expose mask-based editing.