Skip to content

fix: use regional endpoint for non-global Discovery Engine data stores#4706

Open
OiPunk wants to merge 1 commit intogoogle:mainfrom
OiPunk:codex/adk-4697-discovery-regional-endpoint
Open

fix: use regional endpoint for non-global Discovery Engine data stores#4706
OiPunk wants to merge 1 commit intogoogle:mainfrom
OiPunk:codex/adk-4697-discovery-regional-endpoint

Conversation

@OiPunk
Copy link

@OiPunk OiPunk commented Mar 4, 2026

Summary

DiscoveryEngineSearchTool always created the SearchServiceClient with the default global endpoint (discoveryengine.googleapis.com), causing 400 errors for data stores located in non-global regions (e.g. eu, us, europe-west1).

This PR parses the location from the data_store_id or search_engine_id resource path and constructs the appropriate regional API endpoint (e.g. eu-discoveryengine.googleapis.com) via ClientOptions.api_endpoint.

Changes

  • src/google/adk/tools/discovery_engine_search_tool.py: Extract the location segment from the resource ID using a regex and set the correct api_endpoint in ClientOptions. The global location (global) continues to use the default endpoint.
  • tests/unittests/tools/test_discovery_engine_search_tool.py: Added 5 new unit tests covering EU, US, single-region (europe-west1), explicit global, and short-ID (no location) cases. Updated the existing success test to assert the new api_endpoint parameter.

Endpoint mapping

Location Endpoint
global discoveryengine.googleapis.com
eu eu-discoveryengine.googleapis.com
us us-discoveryengine.googleapis.com
europe-west1 europe-west1-discoveryengine.googleapis.com

Test plan

  • All 13 unit tests pass (pytest tests/unittests/tools/test_discovery_engine_search_tool.py)
  • Formatted with pyink (no changes needed)

Fixes #4697

DiscoveryEngineSearchTool always used the default global endpoint
(discoveryengine.googleapis.com), causing 400 errors when accessing
data stores in regional locations like "eu" or "us".

Parse the location from the data_store_id or search_engine_id resource
path and construct the appropriate regional endpoint (e.g.,
eu-discoveryengine.googleapis.com) via ClientOptions.

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

Summary of Changes

Hello, 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 addresses an issue where the Discovery Engine search tool failed for non-global data stores by always attempting to connect to the default global endpoint. The changes introduce logic to parse the resource ID, identify the correct regional location, and construct the appropriate API endpoint, ensuring successful communication with Discovery Engine services across different regions.

Highlights

  • Regional Endpoint Resolution: The DiscoveryEngineSearchTool now dynamically determines and uses the correct regional API endpoint for Google Discovery Engine based on the location extracted from the data_store_id or search_engine_id. This fixes 400 errors for non-global data stores.
  • Improved Test Coverage: Five new unit tests were added to validate the regional endpoint logic for various scenarios, including EU, US, single-region, explicit global, and short-ID cases. An existing test was also updated to assert the api_endpoint parameter.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/google/adk/tools/discovery_engine_search_tool.py
    • Imported the re module for regular expression operations.
    • Defined _LOCATION_PATTERN to extract location from resource IDs.
    • Defined _DEFAULT_ENDPOINT for the global Discovery Engine API.
    • Modified the __init__ method to parse the location from data_store_id or search_engine_id.
    • Constructed the appropriate regional api_endpoint (e.g., eu-discoveryengine.googleapis.com) using the extracted location.
    • Passed the dynamically determined api_endpoint to client_options.ClientOptions.
  • tests/unittests/tools/test_discovery_engine_search_tool.py
    • Updated test_discovery_engine_search_success to include api_endpoint assertion.
    • Added test_regional_endpoint_eu_data_store to verify EU regional endpoint.
    • Added test_regional_endpoint_us_search_engine to verify US regional endpoint.
    • Added test_regional_endpoint_single_region to verify single-region endpoint (e.g., europe-west1).
    • Added test_global_endpoint_explicit to verify explicit global location.
    • Added test_global_endpoint_no_location_in_id to verify fallback to global for short IDs.
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 Mar 4, 2026
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 correctly addresses an issue where DiscoveryEngineSearchTool would fail for non-global data stores by always using the global API endpoint. The fix introduces logic to parse the location from the resource ID and construct the appropriate regional endpoint, which is a solid approach. The accompanying tests are thorough and cover various scenarios. I have a couple of suggestions to further improve the code's clarity and the maintainability of the tests.

if quota_project_id
else None

resource_id = data_store_id or search_engine_id or ""
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The or "" is redundant here. The ValueError check on lines 60-65 ensures that either data_store_id or search_engine_id is a non-None string, so the expression data_store_id or search_engine_id will always evaluate to a string. Removing the fallback to an empty string makes the code slightly cleaner and relies on the existing validation.

Suggested change
resource_id = data_store_id or search_engine_id or ""
resource_id = data_store_id or search_engine_id

Comment on lines +161 to +227
@mock.patch.object(discovery_engine_search_tool, "client_options")
@mock.patch.object(discoveryengine, "SearchServiceClient")
def test_regional_endpoint_eu_data_store(
self, mock_search_client, mock_client_options
):
"""Test that an EU data store uses the EU regional endpoint."""
DiscoveryEngineSearchTool(
data_store_id="projects/my-project/locations/eu/collections/default_collection/dataStores/my-ds"
)
mock_client_options.ClientOptions.assert_called_once_with(
api_endpoint="eu-discoveryengine.googleapis.com",
quota_project_id=None,
)

@mock.patch.object(discovery_engine_search_tool, "client_options")
@mock.patch.object(discoveryengine, "SearchServiceClient")
def test_regional_endpoint_us_search_engine(
self, mock_search_client, mock_client_options
):
"""Test that a US search engine uses the US regional endpoint."""
DiscoveryEngineSearchTool(
search_engine_id="projects/my-project/locations/us/collections/default_collection/engines/my-engine"
)
mock_client_options.ClientOptions.assert_called_once_with(
api_endpoint="us-discoveryengine.googleapis.com",
quota_project_id=None,
)

@mock.patch.object(discovery_engine_search_tool, "client_options")
@mock.patch.object(discoveryengine, "SearchServiceClient")
def test_regional_endpoint_single_region(
self, mock_search_client, mock_client_options
):
"""Test that a single-region location uses the correct endpoint."""
DiscoveryEngineSearchTool(
data_store_id="projects/my-project/locations/europe-west1/collections/default_collection/dataStores/my-ds"
)
mock_client_options.ClientOptions.assert_called_once_with(
api_endpoint="europe-west1-discoveryengine.googleapis.com",
quota_project_id=None,
)

@mock.patch.object(discovery_engine_search_tool, "client_options")
@mock.patch.object(discoveryengine, "SearchServiceClient")
def test_global_endpoint_explicit(
self, mock_search_client, mock_client_options
):
"""Test that a global data store uses the default global endpoint."""
DiscoveryEngineSearchTool(
data_store_id="projects/my-project/locations/global/collections/default_collection/dataStores/my-ds"
)
mock_client_options.ClientOptions.assert_called_once_with(
api_endpoint="discoveryengine.googleapis.com",
quota_project_id=None,
)

@mock.patch.object(discovery_engine_search_tool, "client_options")
@mock.patch.object(discoveryengine, "SearchServiceClient")
def test_global_endpoint_no_location_in_id(
self, mock_search_client, mock_client_options
):
"""Test that a short ID without location falls back to global endpoint."""
DiscoveryEngineSearchTool(data_store_id="test_data_store")
mock_client_options.ClientOptions.assert_called_once_with(
api_endpoint="discoveryengine.googleapis.com",
quota_project_id=None,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

These five tests for endpoint selection are very similar. They can be consolidated into a single, more maintainable test by using pytest.mark.parametrize. This approach reduces code duplication and makes it clearer what is being tested across different inputs.

  @pytest.mark.parametrize(
      ("resource_id_key", "resource_id_value", "expected_endpoint"),
      [
          (
              "data_store_id",
              "projects/my-project/locations/eu/collections/default_collection/dataStores/my-ds",
              "eu-discoveryengine.googleapis.com",
          ),
          (
              "search_engine_id",
              "projects/my-project/locations/us/collections/default_collection/engines/my-engine",
              "us-discoveryengine.googleapis.com",
          ),
          (
              "data_store_id",
              "projects/my-project/locations/europe-west1/collections/default_collection/dataStores/my-ds",
              "europe-west1-discoveryengine.googleapis.com",
          ),
          (
              "data_store_id",
              "projects/my-project/locations/global/collections/default_collection/dataStores/my-ds",
              "discoveryengine.googleapis.com",
          ),
          ("data_store_id", "test_data_store", "discoveryengine.googleapis.com"),
      ],
  )
  @mock.patch.object(discovery_engine_search_tool, "client_options")
  @mock.patch.object(discoveryengine, "SearchServiceClient")
  def test_endpoint_selection(
      self,
      mock_search_client,
      mock_client_options,
      resource_id_key,
      resource_id_value,
      expected_endpoint,
  ):
    """Test that the correct API endpoint is selected based on resource ID."""
    kwargs = {resource_id_key: resource_id_value}
    DiscoveryEngineSearchTool(**kwargs)
    mock_client_options.ClientOptions.assert_called_once_with(
        api_endpoint=expected_endpoint,
        quota_project_id=None,
    )

@rohityan rohityan self-assigned this Mar 4, 2026
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label Mar 4, 2026
@rohityan
Copy link
Collaborator

rohityan commented Mar 4, 2026

Hi @OiPunk , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@rohityan
Copy link
Collaborator

rohityan commented Mar 4, 2026

Hi @DeanChensj , 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/issue 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.

DiscoveryEngineSearchTool: 400 error with regional (non-global) data stores

3 participants