Skip to content

Conversation

@danicat
Copy link

@danicat danicat commented Dec 13, 2025

Standardize VertexAiRagRetrieval to always use function declarations, even for Gemini 2.x+ models. This prevents unnecessary direct retrieval configurations and resolves premature API calls and 429 errors when the tool is not explicitly invoked. This ensures the RAG tool is only called when the model explicitly invokes its declared function.

Link to Issue

Testing Plan

Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Please include a summary of passed pytest results.

All tests pass but pytest is very noisy with thousands of warnings:

3590 passed, 2099 warnings in 27.13s 

I didn't try to tackle any of the warnings as they aren't related to the change.

Manual End-to-End (E2E) Tests:

Please provide instructions on how to manually test your changes, including any
necessary setup or configuration. Please provide logs or screenshots to help
reviewers better understand the fix.

  • create an agent with adk create rag_test
  • go to the cloud console -> vertex ai -> rag engine and create a rag corpus
  • add any data to the rag corpus
  • open the rag corpus and get the "resource name" in the details tag
  • configure the agent with one tool:
from google.adk.agents.llm_agent import Agent
from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval
from vertexai.preview import rag

rag_query = VertexAiRagRetrieval(
    name='rag_query',
    description=(
        'Use this tool to query the rag.'
    ),
    rag_resources=[
        rag.RagResource(
            rag_corpus="projects/123/locations/us-central1/ragCorpora/456" # insert rag resource name here
        )
    ],
    similarity_top_k=10,
    vector_distance_threshold=0.6,
)
root_agent = Agent(
    model='gemini-2.5-flash',
    name='root_agent',
    description='A helpful assistant for user questions.',
    instruction='Answer user questions to the best of your knowledge',
    tools=[
        rag_query,
    ]
)
  • run adk web
  • say "hello" and inspect the request (no rag call should have been made)
  • say "look for x in the rag" and inspect the request and response (a rag call should have been made)

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Add any other context or screenshots about the feature request here.

…ni 2.x+

Standardize VertexAiRagRetrieval to always use function declarations, even for Gemini 2.x+ models. This prevents unnecessary direct retrieval configurations and resolves premature API calls and 429 errors when the tool is not explicitly invoked.

This ensures the RAG tool is only called when the model explicitly invokes its declared function.

Fixes google#3315
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @danicat, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the integration of the VertexAiRagRetrieval tool with Gemini models, particularly addressing behavior in Gemini 2.x+ versions. Previously, these models might have engaged the RAG tool prematurely through a built-in configuration. The updated approach standardizes the tool's interaction by always using function declarations, ensuring that the RAG tool is activated only when explicitly requested by the model. This change aims to prevent unnecessary API calls and enhance the overall reliability and efficiency of RAG-enabled agents.

Highlights

  • Standardized RAG Tool Invocation: The VertexAiRagRetrieval tool now consistently uses function declarations for all Gemini models, including 2.x+ versions.
  • Prevented Premature API Calls: This change resolves issues where the RAG tool was making unnecessary direct retrieval configurations and premature API calls, leading to 429 errors.
  • Explicit Tool Invocation: The RAG tool is now guaranteed to be called only when the model explicitly invokes its declared function, improving control and efficiency.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Dec 13, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request standardizes VertexAiRagRetrieval to always use function declarations, removing the special handling for Gemini 2.x+ models. This change simplifies the implementation in vertex_ai_rag_retrieval.py by removing conditional logic, which improves maintainability and resolves the issues with premature API calls described in the pull request. The corresponding unit test in test_vertex_ai_rag_retrieval.py has been correctly updated to assert the new, unified behavior. The changes are clear, well-justified, and appear to be a solid fix.

@ryanaiagent ryanaiagent self-assigned this Dec 16, 2025
@ryanaiagent
Copy link
Collaborator

Hi @danicat , Thank you for your contribution! We appreciate you taking the time to submit this pull request.

@ryanaiagent ryanaiagent added the needs-review [Status] The PR is awaiting review from the maintainer label Dec 17, 2025
@ryanaiagent
Copy link
Collaborator

Hi @seanzhou1023 , can you please review this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review [Status] The PR is awaiting review from the maintainer tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error 429 RESOURCE_EXHAUSTED when using VertexAiRagRetrieval tool

3 participants