Response Synthesizer
The ResponseSynthesizer is responsible for sending the query, nodes, and prompt templates to the LLM to generate a response. There are a few key modes for generating a response:
Refine: “create and refine” an answer by sequentially going through each retrieved text chunk. This makes a separate LLM call per Node. Good for more detailed answers.CompactAndRefine(default): “compact” the prompt during each LLM call by stuffing as many text chunks that can fit within the maximum prompt size. If there are too many chunks to stuff in one prompt, “create and refine” an answer by going through multiple compact prompts. The same asrefine, but should result in less LLM calls.TreeSummarize: Given a set of text chunks and the query, recursively construct a tree and return the root node as the response. Good for summarization purposes.MultiModal: Combines textual inputs with additional modality-specific metadata to generate an integrated response. It leverages a text QA template to build a prompt that incorporates various input types and produces either streaming or complete responses. This approach is ideal for use cases where enriching the answer with multi-modal context (such as images, audio, or other data) can enhance the output quality.
import { NodeWithScore, TextNode, getResponseSynthesizer, responseModeSchema } from "llamaindex";
// you can also use responseModeSchema.Enum.refine, responseModeSchema.Enum.tree_summarize, responseModeSchema.Enum.multi_modal// or you can use the CompactAndRefine, Refine, TreeSummarize, or MultiModal classes directlyconst responseSynthesizer = getResponseSynthesizer(responseModeSchema.Enum.compact);
const nodesWithScore: NodeWithScore[] = [ { node: new TextNode({ text: "I am 10 years old." }), score: 1, }, { node: new TextNode({ text: "John is 20 years old." }), score: 0.5, },];
const response = await responseSynthesizer.synthesize({ query: "What age am I?", nodesWithScore,});console.log(response.response);The synthesize function also supports streaming, just add stream: true as an option:
const stream = await responseSynthesizer.synthesize({ query: "What age am I?", nodesWithScore, stream: true,});for await (const chunk of stream) { process.stdout.write(chunk.response);}API Reference
Section titled “API Reference”Note for AI agents: this documentation is built for programmatic access.
- Overview of all docs: https://developers.llamaindex.ai/llms.txt
- Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md
- Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters.
- A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/for-agents/mcp/
- Other LlamaIndex tooling for agents — the LlamaParse Platform MCP server, agent skills and plugins, and the n8n node — is mapped at https://developers.llamaindex.ai/for-agents/