Building Search Applications with Vertex AI Search
Source notebook
Repo path:
search/vertexai-search-options/vertexai_search_options.ipynb· Open on GitHub · intermediate
Builds Vertex AI Search workflows using Search API, Gemini grounding, and LangChain retrieval.
Summary
This notebook shows how to use a Vertex AI Agent Builder Search App backed by an unstructured Vertex AI Search data store. It demonstrates three access patterns: direct Discovery Engine search with snippets and summaries, Gemini grounding against the data store, and LangChain RetrievalQA using VertexAISearchRetriever. The workflow covers setup, authentication, project and location configuration, search app identifiers, model setup, safety settings, retrieval, and cleanup.
Diagrams
search_options.png — source
Key code patterns
Discovery Engine search request
client = discoveryengine.SearchServiceClient(client_options=client_options)
request = discoveryengine.SearchRequest(
serving_config=serving_config,
query=search_query,
page_size=10,
content_search_spec=search_spec(),
)
response = client.search(request)Uses the Vertex AI Search app serving config to return snippets and generated summaries.
Summary and snippet configuration
summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
summary_result_count=10,
include_citations=True,
ignore_adversarial_query=True,
model_prompt_spec=ModelPromptSpec(preamble=CUSTOM_PROMPT),
)Constrains generated summaries with citations, adversarial-query handling, and a domain prompt.
Gemini grounding tool
tools = [Tool.from_retrieval(
retrieval=generative_models.grounding.Retrieval(
source=generative_models.grounding.VertexAISearch(
datastore=VERTEX_AI_SEARCH_DATASTORE_ID,
project=PROJECT_ID,
location=VERTEX_AI_SEARCH_LOCATION,
)
)
)]Connects Gemini directly to a Vertex AI Search data store as a grounding source.
LangChain retriever
retriever = VertexAISearchRetriever(
project_id=PROJECT_ID,
location_id=VERTEX_AI_SEARCH_LOCATION,
data_store_id=VERTEX_AI_SEARCH_DATASTORE_ID,
get_extractive_answers=True,
max_documents=10,
)Lets existing LangChain RAG chains retrieve from Vertex AI Search.
Models & APIs used
- Models: gemini-2.0-flash
- APIs / services: Vertex AI, Vertex AI Search, Vertex AI Agent Builder, Discovery Engine
- SDKs / libraries:
google-cloud-aiplatform,google-cloud-discoveryengine,langchain_google_community,langchain,langchain-google-vertexai,langchain-google-community,vertexai
When to use this
Use this pattern when building a RAG or search application over custom unstructured data with Vertex AI Search and optional Gemini or LangChain integration.
Gotchas & caveats
- Requires an existing Google Cloud project with the Vertex AI API enabled.
- Requires manually creating a Vertex AI Agent Builder Search App and unstructured data store before running the notebook.
- The notebook asks users to restart the Jupyter runtime after installing packages.
- Colab authentication is only run when google.colab is detected.
- Discovery Engine client options depend on whether the Vertex AI Search location is global or regional.
- Search app ID, data store ID, project ID, and location must be copied from the Agent Builder console.
- Cleanup requires deleting both the Vertex AI Search App and the data store.
Best practices
- Use a custom prompt that tells the assistant to answer only from grounding snippets and not make up information.
- Enable snippets, summaries, citations, query expansion, and spell correction in direct search requests.
- Use safety settings when generating Gemini responses.
- Ground Gemini answers in the Vertex AI Search data store instead of relying only on model knowledge.
- Use extractive answers and document limits when configuring the LangChain VertexAISearchRetriever.
- Delete the Search App and data store during cleanup.
Related
- Concepts: Vertex AI Search · RAG & Grounding · Function Calling & Tools
- Entities: Vertex AI · Vertex AI SDK · LangChain · Grounding · Gemini
- Area: Vertex AI Search Notebooks
- Best practices: Vertex AI Search - Best Practices · RAG & Grounding - Best Practices · Function Calling & Tools - Best Practices