fix: use regional endpoint for non-global Discovery Engine data stores#4706
fix: use regional endpoint for non-global Discovery Engine data stores#4706OiPunk wants to merge 1 commit intogoogle:mainfrom
Conversation
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
Summary of ChangesHello, 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
🧠 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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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 "" |
There was a problem hiding this comment.
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.
| resource_id = data_store_id or search_engine_id or "" | |
| resource_id = data_store_id or search_engine_id |
| @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, | ||
| ) |
There was a problem hiding this comment.
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,
)|
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. |
|
Hi @DeanChensj , can you please review this. |
Summary
DiscoveryEngineSearchToolalways created theSearchServiceClientwith 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_idorsearch_engine_idresource path and constructs the appropriate regional API endpoint (e.g.eu-discoveryengine.googleapis.com) viaClientOptions.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 correctapi_endpointinClientOptions. 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 newapi_endpointparameter.Endpoint mapping
globaldiscoveryengine.googleapis.comeueu-discoveryengine.googleapis.comusus-discoveryengine.googleapis.comeurope-west1europe-west1-discoveryengine.googleapis.comTest plan
pytest tests/unittests/tools/test_discovery_engine_search_tool.py)pyink(no changes needed)Fixes #4697