Skip to main content
Meilisearch’s chat completions endpoint works as a built-in RAG (Retrieval Augmented Generation) system: for each user message, Meilisearch searches your indexes, then passes the retrieved documents to the LLM to generate a grounded response. This guide shows you how to build a multi-turn chat interface on top of this. Make sure you have completed the setup guide before continuing.
In code examples, replace WORKSPACE_NAME with the name of your workspace. On Meilisearch Cloud, the default workspace name is cloud.

Streaming is required

All requests to the chat completions endpoint must include "stream": true. Non-streaming (stream: false) is not yet supported and returns a 501 Not Implemented error.

Message roles

Every entry in the messages array carries a role that tells the LLM who authored it. The chat completions endpoint uses three roles, each with a distinct origin: Understanding the origin matters when debugging: unexpected system content usually means the workspace system prompt needs tuning, wrong answers are an assistant problem, and malformed input is a user problem.

Send a streaming request

Send a POST request to /chats/{workspace}/chat/completions with stream: true:
This basic request works and the LLM will search your indexes and generate an answer. However, without Meilisearch tools, you get no visibility into what is being searched and no way to maintain conversation context across follow-up questions. The next section explains how to address this.

Meilisearch tools

Meilisearch provides three special tools that improve the chat experience. Declare them in the tools array of your request, and Meilisearch intercepts them, so they are never forwarded to the LLM provider.
These tool definitions must include the exact parameter schemas below. Missing or incorrect parameters will prevent the tools from working.
The call_id field links _meiliSearchProgress and _meiliSearchSources events together: both carry the same call_id, allowing you to associate a search query with the documents it returned. _meiliAppendConversationMessage is the key to multi-turn conversations. Since the endpoint is stateless, Meilisearch uses this tool to expose the internal search tool calls and their results back to the client. You must push these messages into your messages array before the next request, or the LLM will lose the context from previous searches and produce lower-quality answers.

Tool schemas

Complete example: progress, sources, and history

The following example combines all three tools and demonstrates the full recommended usage: streaming progress, displaying sources, and maintaining conversation history for multi-turn questions.
JavaScript (Fetch)

Troubleshooting

Empty reply from server (curl error 52)

Causes:
  • Chat completions feature not enabled
  • Missing authentication in requests
Solution:
  1. Enable the feature (see setup guide)
  2. Include the Authorization header in all requests

”Invalid API key” error

Cause: Using the wrong type of API key Solution:
  • Use the “Default Chat API Key”
  • Do not use search or admin API keys for chat endpoints
  • Find your chat key with the list keys endpoint

”Socket connection closed unexpectedly”

Cause: Usually means the LLM provider API key is missing or invalid in workspace settings Solution:
  1. Check workspace configuration:
  2. Update with a valid API key:

No search progress visible

Cause: The _meiliSearchProgress tool is not declared in the request Solution: The search still runs and the LLM still answers, but without _meiliSearchProgress you receive no visibility into what searches are being performed. Add all three Meilisearch tools to your request as shown in the complete example.

Next steps

One-shot summarization

Generate single AI answers without conversation history.

Stream chat responses

Handle streaming responses for a real-time experience.

Display source documents

Show users which documents were used to generate responses.

Configure guardrails

Restrict AI responses to topics covered by your data.

Chat completions API reference

Full reference for the chat completions endpoint.

Reduce hallucination

Techniques to keep AI responses grounded in your data.