How to beginner · 4 min read

How to use SummaryIndex in LlamaIndex

Quick answer
Use SummaryIndex in LlamaIndex to build a concise summary representation of documents for efficient querying. Initialize the index with your documents, then query it with natural language prompts to get summarized answers.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install llama-index openai

Setup

Install the llama-index and openai packages and set your OpenAI API key as an environment variable.

bash
pip install llama-index openai

Step by step

This example shows how to create a SummaryIndex from documents and query it for a summary answer.

python
import os
from llama_index import SimpleDirectoryReader, SummaryIndex, ServiceContext
from openai import OpenAI

# Set your OpenAI API key in environment variable before running
# export OPENAI_API_KEY="your_api_key"

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Load documents from a directory (replace 'data' with your folder)
docs = SimpleDirectoryReader('data').load_data()

# Create a service context with OpenAI client and model
service_context = ServiceContext.from_defaults(
    llm=client,
    chunk_size_limit=512
)

# Build the SummaryIndex
summary_index = SummaryIndex.from_documents(docs, service_context=service_context)

# Query the index
query = "Summarize the main points of the documents."
response = summary_index.query(query)

print("Summary answer:", response.response)
output
Summary answer: <concise summary text generated by the model>

Common variations

  • Use different LLM models by changing the llm parameter in ServiceContext.from_defaults().
  • Load documents from other sources like PDFs or URLs using appropriate loaders.
  • Use async calls if integrating in async frameworks by adapting the client calls accordingly.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the summary is too short or irrelevant, increase chunk_size_limit or adjust your prompt.
  • For large documents, consider chunking or using other index types for better performance.

Key Takeaways

  • Use SummaryIndex to create a compact summary representation of documents for efficient querying.
  • Initialize SummaryIndex with documents and a ServiceContext that wraps your LLM client.
  • Query the index with natural language prompts to get concise summaries.
  • Adjust chunk size and model parameters to optimize summary quality and performance.
  • Ensure your OpenAI API key is set in os.environ to avoid authentication issues.
Verified 2026-04 · gpt-4o
Verify ↗