Skip to main content
Response synthesizers take retrieved nodes and generate a final response to the user’s query. They control how context is presented to the LLM and how the final answer is constructed.

Overview

All synthesizers extend BaseSynthesizer and implement the synthesize() method. They differ in how they combine multiple text chunks into a coherent response.

Synthesis Modes

LlamaIndex provides four built-in synthesis strategies:

Compact (Default)

Best for: Most use cases, balances quality and efficiency. Compacts text chunks to fit within the context window, then refines the response:
How it works:
  1. Combines chunks to maximize context window usage
  2. Generates initial response from first compact chunk
  3. Refines response with subsequent chunks

Refine

Best for: Comprehensive answers requiring all context. Builds response iteratively, refining with each chunk:
How it works:
  1. Generate initial answer from first chunk
  2. For each subsequent chunk:
    • Present existing answer + new chunk
    • Ask LLM to refine the answer
  3. Return final refined answer
Pros:
  • Most comprehensive, considers all context
  • Good for complex queries
Cons:
  • Requires multiple LLM calls (one per chunk)
  • Slower and more expensive

Tree Summarize

Best for: Summarization tasks, parallel processing. Recursively summarizes chunks in a tree structure:
How it works:
  1. Pack chunks to fit context window
  2. If single chunk: generate answer directly
  3. If multiple chunks:
    • Summarize each chunk in parallel
    • Recursively summarize summaries
    • Return final summary
Pros:
  • Parallelizable (faster for many chunks)
  • Good for summarization
Cons:
  • May lose details in recursive summarization
  • Not ideal for precise Q&A

Multi-Modal

Best for: Images and multi-modal content. Handles images and other non-text content:
How it works:
  1. Preserves multi-modal content (text + images)
  2. Formats prompt with all content types
  3. Sends to multi-modal LLM

Factory Function

Use getResponseSynthesizer() for simple cases:
Available modes:
  • "compact" - CompactAndRefine
  • "refine" - Refine
  • "tree_summarize" - TreeSummarize
  • "multi_modal" - MultiModal

Streaming Responses

All synthesizers support streaming:

Custom Prompts

Customize the prompts used by synthesizers:

Using with Query Engines

Integrate synthesizers into query engines:

Custom Synthesizers

Implement custom synthesis logic:

Choosing a Synthesizer

Best Practices

Prompt Engineering:
  • Customize prompts for your domain
  • Include examples in prompts for better results
  • Test prompts with different synthesizers
Performance:
  • Use compact for most cases (good balance)
  • Use tree_summarize when you have many chunks
  • Avoid refine unless you need maximum quality
Context Management:
  • Retrieve more nodes than needed, let synthesizer select best ones
  • Use postprocessors before synthesis to filter nodes
  • Monitor token usage to avoid context window issues

Next Steps

Postprocessors

Filter and rerank nodes before synthesis

Evaluation

Measure and improve response quality