RAG & Grounding

RAG & Grounding on Google Cloud is the pattern of retrieving relevant, attributable context from managed search, vector, graph, database, web, URL, or memory systems and using it to constrain Gemini or agent responses. The goal is to make responses more accurate, current, domain-specific, and inspectable through retrieved context, citations, grounding metadata, and evaluation.

Synthesized across 67 notebooks. See the source pages under Notebooks below for line-level grounding.

This area starts with content preparation: extract documents or records, split them into useful chunks, preserve metadata such as source URI, page number, chunk number, file ID, entity, or graph relationship, and generate embeddings with task-appropriate models. The retrieved context can come from Vertex AI RAG Engine, Vertex AI Search, Agent Platform Vector Search, Cloud SQL or AlloyDB with pgvector, BigQuery, Firestore, Feature Store, Pinecone, Weaviate, Spanner Graph, Neo4j, URL context, Google Search, Enterprise Web Search, Maps, or Memory Bank.

A typical grounded Gemini application retrieves context before generation, injects only the relevant facts into the prompt or passes a RAG retrieval tool to generate_content, and instructs the model to answer only from the provided sources. Grounding metadata, source documents, citations, logprobs, and retrieved context inspection are used to debug whether the answer is actually supported.

Production-quality RAG is less about simply adding a vector index and more about retrieval quality, operational control, and evaluation. The notebooks repeatedly emphasize chunking and overlap tuning, embedding task types, top_k and distance thresholds, quota-aware batching and retries, IAM setup, cleanup of billable resources, prompt-injection defenses, and groundedness or retrieval metrics such as recall, precision, nDCG, MRR, and custom EvalTask rubrics.

Canonical workflow

  1. Enable the required Google Cloud APIs, configure authentication, project, region, billing, IAM roles, and any required storage or retrieval backend.
  2. Ingest source material from Cloud Storage, PDFs, GitHub, BigQuery, Firestore, Cloud SQL, Vertex AI Search, graph databases, URLs, web search, or Memory Bank, while preserving source metadata.
  3. Parse and chunk documents deliberately, including chunk_size, chunk_overlap, page numbers, file IDs, source URIs, and domain-specific extraction or parsing instructions where needed.
  4. Generate embeddings with a consistent model and task type, then store vectors and metadata in a managed retrieval backend such as Vertex AI RAG Engine, Vector Search, Vertex AI Search, Cloud SQL pgvector, Feature Store, Pinecone, Weaviate, Firestore, Spanner, or Neo4j.
  5. Retrieve candidate context using semantic, hybrid, graph, URL, web, memory, or search tools, tuning top_k, similarity_top_k, vector_distance_threshold, filters, and task-type embeddings for the use case.
  6. Ground Gemini or an agent by injecting retrieved context into the prompt or passing an explicit retrieval or search tool, and instruct the model to answer only from retrieved sources with citations where appropriate.
  7. Inspect retrieved contexts, grounding metadata, grounding chunks, grounding supports, source documents, logprobs, and citation fields to debug answer support and traceability.
  8. Evaluate retrieval and answer quality with row-level and aggregate metrics, custom groundedness rubrics, recall, precision, nDCG, MRR, reference-free or referenced evaluation, then clean up billable resources.

Best practices

See RAG & Grounding - Best Practices for the full distilled guide.

  • Use managed retrieval services such as Vertex AI RAG Engine, Vertex AI Search, Agent Platform Vector Search, or managed database vector stores when scalability, latency, IAM, and infrastructure management matter.
  • Use the same embedding model and compatible dimensionality for document chunks, indexed vectors, and query embeddings; verify vector dimensions and distance measures match the backend.
  • Choose embedding task types deliberately, such as RETRIEVAL_DOCUMENT for indexed documents and QUESTION_ANSWERING or RETRIEVAL_QUERY for user questions, instead of treating all embeddings as generic similarity embeddings.
  • Configure chunk_size, chunk_overlap, top_k, similarity_top_k, vector_distance_threshold, and filters based on retrieval metrics rather than defaults.
  • Preserve source metadata such as page number, chunk number, file path, URI, document ID, graph entity, or table key so generated answers can cite and debug their support.
  • Inspect retrieval results directly before generation with tools such as rag.retrieval_query, search requests, source documents, grounding_chunks, grounding_supports, and url_context_metadata.

Key models & APIs

  • Gemini 3.5 Flash, Gemini 2.5 Flash, Gemini 2.0 Flash, Gemini 3.1 Pro, Gemini 2.0 Flash Live Preview, gemini-embedding-001, gemini-embedding-2, text-embedding-005, Vertex AI RAG Engine, Vertex AI Gen AI SDK and Google Gen AI SDK, Vertex AI Agent Engine and ADK, Agent Engine Memory Bank, Agent Platform Vector Search and Vector Search 2.0, Vertex AI Search and Discovery Engine API, Enterprise Web Search grounding, Google Search grounding, Google Maps grounding, URL Context, Vertex AI Gen AI Evaluation Service, EvalTask, Rapid Evaluation API, and Dataflow ML, BigQuery AI.GENERATE, AI.GENERATE_TABLE, ObjectRefs, and remote models, Cloud SQL for PostgreSQL and pgvector, Vertex AI Feature Store, Firestore, Spanner Graph, Neo4j GraphRAG, Pinecone and Weaviate integrations, LangChain, LlamaIndex, LangGraph, Document AI, Cloud Storage, Secret Manager

Notebooks (67)

Agents & ADK (1)

Embeddings & Vector Search (5)

Gemini (47)

Open Models (2)

Vertex AI Search (12)


Part of the Generative AI Google Brain · RAG & Grounding - Best Practices