Query Your Own Documents with LlamaIndex and LangChain

Two ways to ask questions about your own documents, one built on LlamaIndex and one on LangChain, and what each does differently.

An LLM can only answer questions about your documents if your app finds the relevant passages and sends them along with the question. This is retrieval-augmented generation (RAG), and both LlamaIndex and LangChain can build it in a few dozen lines. I maintain a document Q&A app built on each, and this post compares how they work.

What LlamaIndex and LangChain Do

LlamaIndex is built around getting your data into a form an LLM can use. It provides readers and parsers to load documents, indexes to store them, and query engines that take a question and return an answer, so a basic RAG app needs very little code.

LangChain provides chat models, prompts, retrievers and vector stores as separate components that you connect yourself. Building the retrieval step takes more code, and in return you control the prompt and can see each step.

Query a PDF with LlamaIndex

My Chat with PDF app parses a PDF with LlamaParse, then lets LlamaIndex handle the rest. Embedding, retrieval and answer synthesis are three calls:

st.session_state.index = VectorStoreIndex.from_documents(
    st.session_state.documents,
    embed_model=OpenAIEmbedding(model=EMBED_MODEL, api_key=openai_api_key),
)

query_engine = st.session_state.index.as_query_engine(
    llm=OpenAI(model=model, api_key=openai_api_key),
    similarity_top_k=TOP_K,
    response_mode="tree_summarize",
)
response = query_engine.query(query)

The query engine retrieves the five closest chunks, writes the prompt and combines the chunks into an answer. The response_mode setting controls how it combines them, and LlamaIndex supplies the prompt.

LlamaIndex Chat with PDF app answering a question with page sources
LlamaIndex Chat with PDF app answering a question with page sources

Query a Document Library with LangChain

The Chat with Docs tool in my LangChain examples app keeps documents in Postgres with pgvector. Here the retrieval step is explicit: search the store, number the chunks with their file and page, and pass them to the model with a prompt the app controls:

hits = store.search(question)
context = "\n\n".join(f"[{i}] ({d.metadata.get('source')}, p.{d.metadata.get('page')})\n{d.page_content}"
                      for i, d in enumerate(hits, 1))
answer = chat_model().invoke(PROMPT.format(context=context, question=question)).text

Because the app writes the prompt, it can ask for inline citations like [1] and [2], and tell the model to ignore any instructions hidden in the documents.

LangChain Chat with Docs tool answering a question with numbered citations
LangChain Chat with Docs tool answering a question with numbered citations

How the Two Apps Compare

LlamaIndex appLangChain app
PDF parsingLlamaParse, hosted, keeps tables and handles scanspypdf, reads the text layer only
IndexIn memory, rebuilt each sessionPostgres with pgvector (or Pinecone), persists
DocumentsOne PDF at a timeA library of PDF, text and Markdown files
RetrievalTop 5 chunks, combined by tree_summarizeTop 6 chunks, numbered in a custom prompt
CitationsPage numbers and retrieved passagesInline [1], [2] with file name and page
API keysEntered in the sidebarSidebar or server variables, with an optional app password

For PDFs with tables, the parser affects answer quality more than the framework does. A financial report parsed by LlamaParse gives the model cleaner text than the same report read with pypdf. In day-to-day use, the main difference is persistence. The LlamaIndex app forgets the PDF when you close the tab, and the LangChain app keeps a library you can add to over time.

Deploy Either App on Railway

Both apps are one-click templates on Railway. The LlamaIndex Apps template deploys Chat with PDF and a URL summarizer, with keys entered in the sidebar. The LangChain Apps template deploys the LangChain app with Postgres and pgvector; set APP_PASSWORD if you store API keys on the server.

LlamaIndex Apps: Deploy on Railway

LangChain Apps: Deploy on Railway

Frequently Asked Questions

Should I use LlamaIndex or LangChain for RAG? LlamaIndex gets you to a working document Q&A app with less code, and its parsing and indexing tools are its strength. LangChain suits apps where you want control over each step, or where retrieval is one part of a larger agent or workflow.

Can I use LlamaIndex and LangChain together? Yes. A common pattern is to build the index with LlamaIndex and call it as a retriever or tool from a LangChain app.

Which one handles PDFs with tables better? That depends on the parser you use with it. A layout-aware parser like LlamaParse keeps tables intact, and plain text extraction often scrambles them.

Subscribe to alphasec

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe