Gemini Internal Tools

Use Google Gemini's built-in internal tools (Google Search, URL Context, Code Execution) with PraisonAI agents.

python
from praisonaiagents import Agent

agent = Agent(
    name="Gemini Researcher",
    llm="gemini/gemini-2.0-flash",
    instructions="Use Gemini built-in search when facts are needed.",
    tools=[{"googleSearch": {}}],
)
agent.start("What changed in Python 3.13 release highlights?")

The user asks a factual question; the agent invokes Gemini Google Search and summarises grounded results.

Diagram

Input

Agent

Tool

Output

Overview

Google Gemini provides three powerful internal tools that are natively supported by the model without requiring external implementations. These tools can be used directly through PraisonAI’s tool system.

Available Internal Tools

Quick Start

  1. Simple Usage

    Install PraisonAI with LLM support:

    bash
    pip install "praisonaiagents[llm]"
  2. With Configuration

    Set your Gemini API key:

    bash
    export GEMINI_API_KEY="your-api-key-here"
  3. Create Agent with Internal Tools

    Use internal tools in your agent:

    python
    from praisonaiagents import Agent
    
    # Agent with Google Search
    search_agent = Agent(
        instructions="Research assistant with web search",
        llm="gemini/gemini-2.0-flash",
        tools=[{"googleSearch": {}}]
    )
    
    response = search_agent.start("What's the latest news about AI?")

Individual Tool Examples

Google Search Tool

Use Google Search for real-time information retrieval:

python
from praisonaiagents import Agent

search_agent = Agent(
    instructions="You are a research assistant with web search capabilities",
    llm="gemini/gemini-2.0-flash",
    tools=[{"googleSearch": {}}]
)

response = search_agent.start("What are the latest developments in quantum computing?")
print(response)

URL Context Tool

Analyze content from specific web pages:

python
from praisonaiagents import Agent

url_agent = Agent(
    instructions="You are a content analyzer that can read and summarize web pages",
    llm="gemini/gemini-2.0-flash",
    tools=[{"urlContext": {}}]
)

response = url_agent.start("Summarize this article: https://example.com/article")
print(response)

Code Execution Tool

Execute Python code for calculations and data analysis:

python
from praisonaiagents import Agent

code_agent = Agent(
    instructions="You are a data analyst that can execute Python code",
    llm="gemini/gemini-2.0-flash",
    tools=[{"codeExecution": {}}]
)

response = code_agent.start("Calculate the compound interest for $10,000 at 5% annual rate for 10 years")
print(response)

Combined Tools Example

Use multiple internal tools together for complex tasks:

python
from praisonaiagents import Agent

research_agent = Agent(
    instructions="""You are an advanced research assistant that can:
    1. Search the web for information
    2. Analyze content from specific URLs
    3. Execute Python code for data analysis""",
    llm="gemini/gemini-2.0-flash",
    tools=[
        {"googleSearch": {}},
        {"urlContext": {}},
        {"codeExecution": {}}
    ]
)

# Complex research task
response = research_agent.start("""
Research the current stock price of Apple (AAPL), 
find recent news about the company, 
and calculate its P/E ratio if the EPS is $6.15
""")
print(response)

Multi-Agent System with Internal Tools

Create a multi-agent system where different agents use different internal tools:

python
from praisonaiagents import Agent, Task, AgentTeam

# Research agent with Google Search
researcher = Agent(
    name="Researcher",
    role="Web Research Specialist",
    goal="Find accurate and up-to-date information",
    instructions="Search for comprehensive information on topics",
    llm="gemini/gemini-2.0-flash",
    tools=[{"googleSearch": {}}]
)

# Analyst agent with Code Execution
analyst = Agent(
    name="Analyst",
    role="Data Analyst",
    goal="Analyze data and perform calculations",
    instructions="Perform data analysis and calculations",
    llm="gemini/gemini-2.0-flash",
    tools=[{"codeExecution": {}}]
)

# Content agent with URL Context
content_analyzer = Agent(
    name="ContentAnalyzer",
    role="Content Analysis Specialist",
    goal="Extract and summarize content from URLs",
    instructions="Analyze and summarize web content",
    llm="gemini/gemini-2.0-flash",
    tools=[{"urlContext": {}}]
)

# Define tasks
research_task = Task(
    description="Search for information about renewable energy trends in 2024",
    expected_output="List of key trends with sources",
    agent=researcher
)

analysis_task = Task(
    description="Calculate the growth rate of solar energy adoption based on the research",
    expected_output="Growth rate calculation with visualization",
    agent=analyst
)

content_task = Task(
    description="Analyze this article: https://example.com/renewable-energy-report",
    expected_output="Summary of key points from the article",
    agent=content_analyzer
)

# Create and run the multi-agent system
agents = AgentTeam(
    agents=[researcher, analyst, content_analyzer],
    tasks=[research_task, analysis_task, content_task],
    process="sequential"
)

agents.start()

Mixing Internal and External Tools

Combine Gemini’s internal tools with custom external tools:

python
from praisonaiagents import Agent

def custom_calculator(expression: str) -> str:
    """Custom calculator function"""
    import ast
    import operator

    ops = {
        ast.Add: operator.add,
        ast.Sub: operator.sub,
        ast.Mult: operator.mul,
        ast.Div: operator.truediv,
        ast.Pow: operator.pow,
        ast.USub: operator.neg,
    }

    def eval_expr(node):
        if isinstance(node, ast.Constant):
            return node.value
        elif isinstance(node, ast.BinOp):
            return ops[type(node.op)](eval_expr(node.left), eval_expr(node.right))
        elif isinstance(node, ast.UnaryOp):
            return ops[type(node.op)](eval_expr(node.operand))
        else:
            raise TypeError(f"Unsupported operation: {type(node)}")

    try:
        return str(eval_expr(ast.parse(expression, mode='eval').body))
    except Exception as e:
        return f"Error: {e}"

# Agent with both internal and external tools
hybrid_agent = Agent(
    instructions="You are a versatile assistant with multiple capabilities",
    llm="gemini/gemini-2.0-flash",
    tools=[
        {"googleSearch": {}},      # Internal tool
        {"codeExecution": {}},     # Internal tool
        custom_calculator          # External tool
    ]
)

response = hybrid_agent.start("Search for the current Bitcoin price and calculate 15% of $10,000")
print(response)

How It Works

Diagram
Gemini Internal ToolsAgentUserGemini Internal ToolsAgentUserRequestProcess requestResultResponse

Tool Definition Syntax

Gemini internal tools use a special dictionary syntax:

python
# Internal tool format
tools=[{"toolName": {}}]

# Multiple internal tools
tools=[
    {"googleSearch": {}},
    {"urlContext": {}},
    {"codeExecution": {}}
]

Integration Flow

  1. Tool Definition: Define tools using the special internal tool syntax
  2. Pass-Through: PraisonAI passes these tools directly to LiteLLM
  3. Execution: LiteLLM sends them to Gemini as internal tool configurations
  4. Results: Gemini executes the tools natively and returns integrated responses

Benefits of Internal Tools

Native Integration
  • No external API calls required
  • Seamless integration with Gemini’s capabilities
  • Optimized for performance
Automatic Grounding
  • Search results are automatically integrated into responses
  • Context-aware information retrieval
  • Source attribution built-in
Security
  • Code execution is sandboxed within Gemini’s environment
  • No local code execution risks
  • Controlled access to resources
No Rate Limits
  • No separate API rate limits
  • Included in Gemini API quota
  • Simplified billing

Best Practices

Model Selection

Use Gemini 2.0+ models for internal tools: – gemini/gemini-2.0-flash (recommended) – gemini/gemini-2.0-flash-thinking-exp – Other Gemini 2.0+ models

Error Handling

Always handle potential errors:

python
try:
    response = agent.start("Your query")
except Exception as e:
    print(f"Error: {e}")
Debugging

Enable reflection for better debugging:

python
agent = Agent(
    # ... other config
    reflection=True
)

Troubleshooting

API Key Issues

Problem: API key not recognized

Solution: Ensure the environment variable is set correctly:

bash
echo $GEMINI_API_KEY  # Should show your key

Model Support

Problem: Tool not available error

Solution: Verify you’re using a Gemini 2.0+ model:

python
llm="gemini/gemini-2.0-flash"  # Correct
# NOT: llm="gemini/gemini-1.5-pro"  # May not support all tools

Regional Restrictions

Problem: Some tools may have regional restrictions

Solution: Check Gemini API documentation for availability in your region

References

Related Documentation