LangChain and LangGraph · Chapter 19 of 25

State and parallel steps

By the end of this chapter you can say what happens when a node writes a key the state already holds, give a key a reducer so that many nodes can add to it, run nodes side by side and join them again, start one step per item of a list with Send, and explain the error LangGraph raises when two nodes write the same key at once. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

So far every graph has been a line or a branch: one node runs, then the next. Faisal settles a move-out by collecting deductions from the deposit: unpaid rent, lost keys, damage (policy 2.3). Each is a separate check, and each adds a line to the same list. Two questions follow, and this chapter answers both:

  • When several nodes write the same key, what ends up in the state?
  • The checks do not need each other. Why should the damage check wait for the rent check? And when Mrunal's inbox holds a dozen messages, why label them one at a time?

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    reducer.py: one key, many writers

    Build a graph whose state (a TypedDict) holds tenant_id, keys_lost, damage, deductions, total and refund. deductions is a list of dictionaries, each with a reason and an amount, that every check adds to. Four nodes, one after another, START, unpaid_rent, lost_keys, damage, settle, END, compiled as graph: unpaid_rent adds {"reason": "unpaid rent", "amount": due} with the due from roomraja.rent_due(tenant_id); lost_keys adds lost keys at 750 rupees a key; damage adds damage with the state's damage; a check adds nothing when there is nothing to deduct. settle sets total, the sum of the amounts, and refund, the tenant's deposit (roomraja.get_tenant) minus the total, never below 0.

    Under if __name__ == "__main__":, run it for T1001 with 1 key lost and 2000 rupees of damage and print one line per deduction, in the order they were added, as the reason, a colon, a space and the amount, then Total:, the total, a comma, refund: and the refund, for example Total: 13750, refund: 5250. The check runs your graph for other tenants.

  2. Task 2

    parallel.py: three checks at once

    Start from task 1's graph and change only its edges: each of unpaid_rent, lost_keys and damage gets an edge from START and an edge to settle, and no check waits for another. Compile it as graph.

    Under if __name__ == "__main__":, run it for T1004 with 2 keys lost and 3000 rupees of damage and print each deduction, largest amount first, as in task 1, then the Total: line. The check runs your graph for other tenants.

  3. Task 3

    send.py: one step per message

    Build a graph whose state holds messages (a list of dictionaries with an id and a text) and labels (a list every step adds to). From START, send one classify step per message with Send, giving it only that message's id and text. classify asks lab-chat with the system message Classify the tenant's message as one of: rent, deposit, notice, maintenance, guest, food, room_change, complaint, praise. Reply with the label only. and the message's text as the human message, and adds {"id": the id, "label": the reply's text, stripped} to labels; then the graph ends. Compile it as graph.

    Under if __name__ == "__main__":, read /workspace/data/messages.jsonl, run the graph on its 13th to 24th messages (M013 to M024), and print one line per message, by id, as the id, a space and its label; then one line with each label, a space and how many, joined by a comma and a space, most first, ties by the label's name. The check runs your graph on other messages.

  4. Task 4 · interview question

    Interview: fix_me.py, two checks, one key

    Before a tenant moves out, Mrunal's checklist runs three checks at once (unpaid rent, notice in the app, the lock-in), each adding its warnings to the checklist. Run fix_me.py: it stops with an error before printing anything.

    Fix it so that the three checks still run in parallel and every warning reaches the checklist; it then prints one line per tenant, the tenant id, a colon, a space and the warnings in alphabetical order joined by a semicolon and a space, or ready to move out. Keep the three checks, the graph's shape and graph. The check runs your graph for other tenants.

    The interviewer will ask what happens when two nodes write the same key, and why LangGraph refuses instead of keeping one of the values.

What you will learn

  • Reducers: how a node's update is combined with the state.
  • Annotated with a function: your own rule for a key.
  • Fan-out and fan-in: nodes side by side, then one that joins them.
  • Send: one step per item, each with its own input.
  • InvalidUpdateError: two writers, one key, no rule.

More in Production

Build "State and parallel steps" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.