Skip to main content
Displaying source documents builds user trust by showing which data the AI used to formulate its answer. When using @meilisearch/ai-sdk, source documents are already part of your search tool’s output: whenever the model calls a Meilisearch search tool, the result contains the query it ran and the documents it found.

Render tool calls in your UI

The AI SDK represents each assistant message as an ordered list of parts (message.parts) instead of a single string. A tool call becomes its own message part, typed tool-{toolName}, with a state that tracks its progress. The examples below are based on a Next.js application, but the AI SDK offers compatible libraries for most frontend frameworks.

Register the search tool

Give the model a search tool on the server, as described in getting started with agentic search:
src/app/api/chat/route.tsx

Render the tool’s result

On the client, useChat streams each message’s parts as they arrive. Check part.type and part.state to render the right thing for each tool call:
src/components/chat.tsx
part.input holds the arguments the model passed to the tool, including the search query (q). part.output holds the tool’s return value, Meilisearch’s search response, with matching documents under output.hits.

Extract documents from the search response

The tool’s output shape depends on which Meilisearch tool the model called (meilisearchSearch, meilisearchMultiSearch, or meilisearchSearchSimilar). Guard your extraction so a partial or unexpected output never breaks the UI:
parse-document-hits.ts

Display sources in your UI

Here is a simple Sources component that lists the documents behind a tool call. This example uses React, but the same pattern, listing documents from a completed tool part, works with any frontend framework:
src/components/sources.tsx

Common UI patterns

There are several ways to present source documents to users:
  • Inline citations: Number each source and reference them in the response text (for example, [1], [2])
  • Collapsible panel: Show a “Sources” section below the response that users can expand
  • Side panel: Display sources in a sidebar next to the conversation
  • Footnotes: List sources at the bottom of each response
Choose the pattern that fits your application’s layout and your users’ needs.

Handle multiple searches

A single question can trigger multiple tool calls, for example when the model searches more than one index or issues several queries to compare results. Each tool call keeps its own query and documents, and the AI SDK gives every call a unique toolCallId, so you can render them independently or group them together:
src/components/multiple-sources.tsx

Using the experimental Chats API

If you call the experimental Chats API directly instead of using the AI SDK, Meilisearch exposes source documents through two special tools rather than through a single tool call. See display source documents with the Chats API for the tool schemas and how to parse them from the response stream.

Next steps

Agentic search getting started

Build an agent that returns source documents.

Configure guardrails

Keep responses grounded in the sources you display.

Handle errors and fallbacks

Handle searches that return no results.