Skip to contents

Execute Gemini requests with built-in tools (Google Search, Code Execution) and custom function calling. Provides tool builder functions for easy configuration.

Usage

gemini_with_tools(
  prompt,
  sys_prompt = NULL,
  tools = list(),
  ml = NULL,
  temp = 1,
  timeout = 60,
  max_think = FALSE
)

tool_google_search()

tool_code_execution()

tool_function(name, description, parameters)

Arguments

prompt

Character. User message or question. Must be a single string.

sys_prompt

Character. System instruction to set model behavior context. If NULL (default), no system instruction is sent. Use for persona setup or task-specific guidelines.

tools

List. Tool configurations created with tool_google_search(), tool_code_execution(), or tool_function(). Pass as a list even for single tools: tools = list(tool_google_search()). Empty list for no tools.

ml

Character. Model ID to use. If NULL (default), uses ART_GEMINI_MODEL env var. Google Search requires Gemini 2.0+ models.

temp

Numeric. Temperature setting (0-2). Default 1. Use 0 for deterministic tool responses, higher for creative responses.

timeout

Numeric. Request timeout in seconds. Default 60. Tool-augmented requests may need longer timeouts.

max_think

Logical. Enable extended reasoning for Gemini 3 models. Silently ignored for other models. Default FALSE.

name

Character. Function identifier (e.g., "get_weather", "search_database"). Use snake_case, no spaces. This name is returned by the model when requesting a function call.

description

Character. Clear description of what the function does and when to use it. The model uses this to decide when to call the function.

parameters

List. OpenAPI-compatible parameter schema with type ("object"), properties (dict of parameter definitions), and optionally required (array of required parameter names). See examples for format.

Value

Character model reply with attributes modelVersion, usageMetadata, and contents_history

Details

Gemini supports three categories of tools:

Built-in Tools (fully managed by Gemini):

  • Google Search grounding (use tool_google_search())

  • Code execution (use tool_code_execution())

Custom Function Calling:

  • Define functions with tool_function() for external tool integration

  • Model generates structured JSON for function calls

  • You handle execution and send results back

Tool Execution Flow:

  1. Built-in tools execute automatically within the API call

  2. Custom functions return structured JSON for you to execute

  3. Use gemini_continue() to send function results back

Google Search grounding reduces hallucinations by connecting the model to real-time web information. Works automatically - no manual result handling needed.

Pricing: US$14 per 1,000 search queries (as of 2025).

Enables the model to write and execute Python code for math problems, data processing, and complex calculations. Execution happens automatically within the API call.

Creates a custom function declaration for function calling. When the model decides to use this function, it returns structured JSON with the function name and arguments. You must:

  1. Execute the function in your code

  2. Send the result back using gemini_continue()

Parameter schema uses a subset of OpenAPI format:

  • type: Usually "object"

  • properties: Dict of parameter definitions (each with type, description, optional enum)

  • required: Array of required parameter names

Functions

  • gemini_with_tools(): Execute Gemini Request with Tools

  • tool_google_search(): Create Google Search grounding tool (Gemini 2.0+)

  • tool_code_execution(): Create code execution tool

  • tool_function(): Create custom function declaration

References

  • Function calling: https://ai.google.dev/gemini-api/docs/function-calling

  • Built-in tools: https://ai.google.dev/gemini-api/docs/tools

  • Grounding with Google Search: https://ai.google.dev/gemini-api/docs/google-search

Examples

if (FALSE) { # \dontrun{
# Google Search grounding
gemini_with_tools(
  "Who won the 2024 UEFA Euro?",
  tools = list(tool_google_search())
)

# Code execution
gemini_with_tools(
  "Calculate the factorial of 20",
  tools = list(tool_code_execution())
)

# Custom function calling
get_weather <- tool_function(
  name = "get_weather",
  description = "Get current weather for a location",
  parameters = list(
    type = "object",
    properties = list(
      location = list(type = "string", description = "City name"),
      units = list(type = "string", enum = list("celsius", "fahrenheit"))
    ),
    required = list("location")
  )
)

gemini_with_tools(
  "What's the weather in Paris?",
  tools = list(get_weather),
  temp = 0
)

# Complex with tools: longer
gemini_with_tools("...", tools = list(tool_google_search()), timeout = 120)

# Deterministic Outputs: Consistent JSON output
# Use `temp = 0` for reproducible structured data
gemini_with_tools(
  "Extract metadata as JSON",
  sys_prompt = "Return valid JSON only",
  temp = 0
)
} # }

if (FALSE) { # \dontrun{
gemini_with_tools(
  "What are the latest developments in AI?",
  tools = list(tool_google_search())
)
} # }
if (FALSE) { # \dontrun{
gemini_with_tools(
  "Calculate the sum of prime numbers less than 100",
  tools = list(tool_code_execution())
)
} # }
if (FALSE) { # \dontrun{
# Define a weather function
weather_func <- tool_function(
  name = "get_weather",
  description = "Get current weather for a location",
  parameters = list(
    type = "object",
    properties = list(
      location = list(
        type = "string",
        description = "City name or coordinates"
      ),
      units = list(
        type = "string",
        description = "Temperature units",
        enum = list("celsius", "fahrenheit")
      )
    ),
    required = list("location")
  )
)

# Use in request
resp <- gemini_with_tools(
  "What's the weather in Tokyo?",
  tools = list(weather_func)
)

# Check if model requested function call (implementation depends on response format)
# Then execute your function and continue conversation with results
} # }