Return source documents for citation

A tutor that answers without showing its work is a tutor you cannot audit. Setting return_source_documents=True on the chain returns the exact chunks that grounded each answer, which turns every response into a trail the learner can follow.

booktutor.py
python
result = qa_chain.invoke({
    'question': 'What is attention?',
    'chat_history': chat_history,
    'book_name': 'Deep Learning Book',
})

print('Answer:', result['answer'])
print()
for i, doc in enumerate(result['source_documents'], 1):
    print(f'Source {i}:')
    print(doc.page_content[:200])
    print('-' * 40)

The result dict contains both the answer and source_documents, which is a list of LCDocument objects with page_content and metadata. Render them in your UI next to the answer so the learner can verify claims instantly.

Docling can emit page references during parsing. Store them in the document metadata when you build chunks, and they flow through the splitter and into the FAISS index. At query time, source_documents carry metadata, so you can render a page tag next to each chunk. The upgrade is a few lines in the loader and pays off every single query.

Quiz: Quiz

Loading practice…

Checkpoint: Chain wiring checkpoint

Loading practice…