Repository navigation
DiscoveryEngineSearchTool auto-detect sends redundant CHUNKS searches under concurrent first use #6101
Description
Summary
DiscoveryEngineSearchTool(search_result_mode=None) appears to use CHUNKS-first auto-detect and cache _search_result_mode only after the structured-datastore fallback. Under concurrent first use of a fresh shared tool instance, multiple callers can enter the auto-detect branch before the cache is set, emitting redundant failing CHUNKS SearchService.Search calls before the DOCUMENTS retries.
Environment
- google-adk: 2.2.0
- google-cloud-discoveryengine: 0.20.0
- google-api-core: 2.31.0
- google-auth: 2.53.0
- grpcio: 1.81.1
- Python: 3.14.4
- OS: macOS arm64
- Relevant source:
src/google/adk/tools/discovery_engine_search_tool.py
Evidence
- A fresh
ghsearch of open and closed issues/PRs found no clear duplicate. - Current source still appears to try
SearchResultMode.CHUNKSbeforeSearchResultMode.DOCUMENTSwhensearch_result_mode=None, then mutatesself._search_result_modeto DOCUMENTS after the structured-datastore fallback. I did not find an obvious single-flight lock around that transition. - The evidence run used a tiny temporary structured JSON datastore. Project, account, datastore, and document identifiers are intentionally omitted here.
- Total actual
SearchService.Searchcalls in the evidence run: 4092 / cap 5000. - 429 count: 0.
- Max rolling 60s Search starts: 120 / effective cap 120.
- Direct explicit DOCUMENTS control passed.
- Direct no-content-search-spec control passed.
- ADK explicit DOCUMENTS control: 1000 wrapper calls, 1000 DOCUMENTS searches, 0 CHUNKS.
- ADK auto concurrent cold start: 1000 wrapper calls, 2000 low-level searches.
- ADK auto CHUNKS calls: 1000.
- ADK auto DOCUMENTS calls: 1000.
- Redundant CHUNKS total above a single-flight expectation: 950.
- Auto calls per wrapper: 2.0x.
- Explicit DOCUMENTS calls per wrapper: 1.0x.
- Cleanup verified temporary datastore deletion by
404 NOT_FOUND.
User Impact
This can multiply SearchService.Search calls and therefore increase quota usage, latency, and potentially billable request volume.
Expected Behavior
For a structured datastore, concurrent first use of a fresh shared DiscoveryEngineSearchTool(search_result_mode=None) should single-flight auto-detection. At most one caller should need to probe CHUNKS and learn the DOCUMENTS fallback for that tool instance; concurrent callers should reuse the detected result mode.
Possible fixes:
- Guard auto-detection with a per-instance single-flight lock.
- Default to DOCUMENTS for structured datastores when that can be known without probing.
- Document that structured datastore users should set
search_result_mode=SearchResultMode.DOCUMENTSto avoid extra calls.
Minimal Repro Shape
This can be reproduced without live Discovery Engine calls:
-
Instantiate one fresh shared
DiscoveryEngineSearchTool(search_result_mode=None). -
Replace
_discovery_engine_clientwith a fake client. -
Have the fake client sleep briefly on CHUNKS requests, then raise the same structured-datastore
InvalidArgumentmessage:content_search_spec.search_result_mode must be set to SearchRequest.ContentSearchSpec.SearchResultMode.DOCUMENTS when the engine contains structured data store. -
Have DOCUMENTS requests return one fake document result.
-
Release N worker threads through a
threading.Barrier.
Current behavior emits more than one CHUNKS probe for a single fresh shared tool instance. Fixed behavior should emit at most one.
Control: explicit SearchResultMode.DOCUMENTS emits exactly one DOCUMENTS call per wrapper call and zero CHUNKS calls.
Scope of Run
The evidence run used only discoveryengine.googleapis.com SearchService.Search against a tiny temporary structured JSON datastore. It did not use Gemini, an ADK agent loop, VertexAiSearchTool model calls, process_llm_request, AnswerQuery, SummarySpec, Search Summary, Grounded Generation, Ranking API, Cloud Storage, BigQuery, Document AI, Compute, Cloud Run, GKE, custom Vertex endpoints, OCR, or layout parsing.
No project IDs, billing IDs, local filesystem paths, datastore names, document names, or user-specific identifiers are included in this report.
Activity
sanketpatil06 commentedon Jun 17, 2026
Hi @glaziermag, thanks for reporting this.
As a quick workaround, you can bypass the auto-detect step entirely by manually setting
search_result_modetoDOCUMENTSorCHUNKS, depending on your datastore type. Additionally, a PR has been opened to address this, could you please test the changes in PR #6106 and confirm if they resolve the issue on your end?glaziermag commentedon Jun 17, 2026
Thanks, I tested PR #6106 against a live temporary structured Discovery Engine datastore.
Result: it resolves the concurrent cold-start issue on my side. With 16 concurrent first-use calls on the PR branch, I saw 1 CHUNKS probe total and 16 DOCUMENTS searches. On current main against the same datastore, I reproduced the issue with 16 CHUNKS probes and 16 DOCUMENTS retries. Explicit DOCUMENTS mode also worked with 0 CHUNKS calls. Temporary datastore was cleaned up and verified deleted.
fix: single-flight Discovery Engine mode detection
fix: single-flight Discovery Engine mode detection