Map-Reduce Summaries
Split, summarise, combine.
Map reduce works in two steps. Map, summarise every part separately, all at the same time. Then reduce, combine those small summaries into one. Because the parts are independent, it's much faster.

- Map: summarise each part
- Reduce: combine
- Parts run in parallel
Map, then reduce
The map step uses batch, so every part is summarised at once, in one sentence each. We print the part summaries. The reduce step joins them, and asks for one short overview. Run it. We see one line per part, then a final overview of the whole shop.
from pathlib import Path
from langchain.chat_models import init_chat_model
from langchain_text_splitters import RecursiveCharacterTextSplitter
handbook = "\n".join(p.read_text() for p in sorted(Path("kb").glob("*.md")))
parts = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=0).split_text(handbook)
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
mapped = model.batch([f"Summarise in one short sentence:\n{p}" for p in parts])
for m in mapped:
print("-", m.text.strip())
notes = "\n".join(m.text for m in mapped)
overview = model.invoke(f"Combine these notes into a two-sentence overview:\n{notes}")
print("\noverview:", overview.text.strip())- Riverside Books offers flexible gift cards and a free loyalty club that rewards members with points, vouchers, and exclusive perks. - You can return undamaged books within 30 days for a full refund, or faulty books at any time with free return shipping, by replying to your order confirmation email. - UK standard delivery costs £2.99 (free over £25) and takes 3-5 days, while express, international, and tracked options are also available. - Riverside Books is an independent bookshop located in Bath that sells new and second- hand books, hosts a weekly children's story time, and is open seven days a week. overview: Based in Bath, the independent Riverside Books offers a wide selection of new and second-hand titles, weekly children's story times, and a rewarding loyalty club. Customers can enjoy flexible shipping options, a hassle-free return policy, and convenient gift cards when shopping at this seven-day-a-week bookshop.
Also uses kb/shipping.md
# Shipping
Standard delivery in the UK takes 3 to 5 working days and costs £2.99.
Delivery is free on orders over £25.
Express delivery arrives the next working day if you order before 2pm, and costs £5.99.
We ship to Ireland and France in 5 to 8 working days for £7.50.
Every order gets a tracking link by email once it leaves our warehouse.Also uses kb/returns.md
# Returns and refunds
You can return any book within 30 days of delivery for a full refund.
Books must be unread and in the condition you received them.
Damaged or faulty books can be returned at any time, and we pay the return postage.
Refunds go back to the original payment method within 5 working days of us receiving the book.
To start a return, reply to your order confirmation email.Also uses kb/gift_cards.md
# Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.Also uses kb/loyalty.md
# Riverside Readers loyalty club
Joining the Riverside Readers club is free.
Members earn one point for every pound they spend.
100 points can be swapped for a £5 voucher.
Members get early access to signed editions and a birthday discount of 10 percent.Also uses kb/shop.md
# About the shop
Riverside Books has one physical shop, at 12 Mill Lane, Bath.
The shop is open from 9am to 6pm Monday to Saturday, and 11am to 4pm on Sunday.
We sell new and second-hand books, and run a children's story time every Saturday at 10am.
You can contact us at hello@riverside-books.example or on 01225 000 000.Running vs map-reduce
So which should you use? A running summary is simple, and keeps a clear story, but it's slow. Map reduce is fast, because the parts run in parallel, and it scales to huge documents. For very long documents, you can even reduce in several rounds.
Running summary
- Simple loopKeeps the storySlow
Map-reduce
- Parallel partsFastScales to huge docs
Summary
Map reduce summarises parts in parallel, then combines the results. It's fast, and scales well. Next, translating and rewriting text.
Key takeaways- Map with batch
- Reduce by combining
- Fast and scalable