Gemini Grounding with Google Search: Preserve Queries and Inline Citation Spans

Gemini grounded responses can expose search calls and URL citation spans. Preserve both before rendering, scoring, or rewriting the answer.

Sonar pins search-query cards and citation brackets to exact spans in a grounded Gemini answer.

Direct answer: Gemini API responses grounded with Google Search can expose the search queries used by the system and URL citation annotations tied to character spans in the generated text. Preserve those fields with the original response before transforming or rendering it.

A citation UI is only the final layer. Reproducibility depends on the request, model, grounding configuration, search calls, raw text, URL annotations, timestamps, and any application-side changes. Losing one layer can turn a traceable answer into a decorative source list.

Separate the grounding record

Store each evidence layer independently
LayerRecordReason
RequestPrompt, model, tools, parametersDefines the task and instrument
Search callQuery and call eventShows retrieval activity
AnswerUnmodified returned textPreserves citation offsets
AnnotationURL, title, start and end indexMaps evidence to a span
RenderDisplayed text and linksTests the user-facing result

Do not collapse multiple queries into a single “searched the web” flag. The query sequence can explain why a source appeared, why a subtopic was missed, or why two runs produced different evidence.

Preserve spans before normalizing text

The citation annotation includes character offsets. Trimming whitespace, replacing Unicode characters, inserting Markdown, normalizing line endings, or merging response parts can shift those offsets. Apply citations to the exact returned string or create a tested offset map from the raw string to the rendered string.

  1. Save the raw response unchanged.
  2. Validate that each start and end index falls inside the returned text.
  3. Extract the span and store it beside the URL and title.
  4. Detect overlapping, adjacent, empty, or out-of-order annotations.
  5. Render from the structured record and compare the visible link with the expected span.

Test emoji, accented characters, right-to-left text, line breaks, and code samples. Index semantics can be mishandled when one layer counts bytes and another counts characters.

Audit the source-claim relationship

An annotation shows the source the system associated with a text range. It does not by itself prove that the source supports every claim inside that range. Open the URL, locate the relevant passage, identify the source owner, and classify the support as direct, qualified, conflicting, or absent.

Record redirects, inaccessible pages, changed content, duplicate sources, and citations that point to a search result or intermediary rather than the fact owner. Keep the retrieved title separate from the current page title because either can change.

Use the citation-ready passage test and the evidence-led publishing guide for claim-level skeptical review.

Make runs reproducible without ranking claims

Save the date, account or project, model version, API version, locale, prompt, tool configuration, safety settings, response, search calls, citations, errors, latency, and cost fields available to the application. Repeating the same request later is a new observation because the web, index, model, and product can change.

Do not describe a cited URL as “ranking in Gemini” or “ranking in Google AI” from one response. The citation is evidence that the URL was returned in a particular grounded answer under a particular environment. Stable prevalence requires a repeated, declared sample.

Pricing and supported models change. Read the current Gemini pricing and changelog before calculating cost or pinning a production model.

Primary documentation

Community discussion

Discuss: Gemini Grounding with Google Search: Preserve Queries and Inline Citation Spans

Have a question, a useful example, or a different perspective? Join the discussion, share evidence, and help other readers reach a better answer.

0 replies Moderated
No replies yet.

Be the first to ask a focused question, share a practical example, or add useful evidence.

Ask a question or join the discussion

Share evidence, a useful example, or a clear question. Be specific, stay on topic, and challenge ideas without attacking people. First-time replies may be held for moderation.