Runnables: Passthrough, Lambda, Parallel
Everything you can put in a chain is called a Runnable. Prompts, models and parsers are all Runnables.
Sometimes you need a step that is not one of those. You want to run your own function, or run two things side by side.
LangChain gives you three small helpers for that. This lesson is about them.
RunnableLambda: your own function as a step
Any plain Python function can join a chain. Wrap it in RunnableLambda.
Now shout has invoke, batch and stream like everything else. It can sit anywhere in a chain.
When you pipe a plain function directly, LangChain wraps it for you. Writing RunnableLambda is clearer, though.
Using it in a real chain
The model's text flows into count_words. The dict it returns is the chain's final answer.
RunnableParallel: two things at once
Think of a relay race with two lanes. Both runners get the same baton and run at the same time.
RunnableParallel sends the same input to several Runnables and collects the results in a dict.
Both model calls run at the same time. The keys you pick become the keys of the output dict.
A dict is a parallel in disguise
Inside a chain, a plain dict is turned into a RunnableParallel for you. That is the shorthand almost everyone uses.
The dict runs joke and fact side by side. Its output has the exact keys summary_prompt needs.
This pattern shows up all over LangChain, especially in RAG later in the course.
RunnablePassthrough: hand it on unchanged
Sometimes one lane of the parallel should just pass the input along untouched.
The original lane returns exactly what it received. The other lane changes it.
assign: keep everything and add one key
RunnablePassthrough.assign keeps the whole input dict and adds new keys to it.
text is still there. length and upper were added next to it.
Use this when a later step needs both the old data and something new computed from it.
Naming a step
Give steps a name and they show up nicely in traces and logs.
RunnableLambda: your own function as a step
RunnableParallel, or a plain dict: run several steps on the same input, get a dict back
RunnablePassthrough: pass the input along unchanged
RunnablePassthrough.assign: keep the input dict and add keys
Remember: a dict in a chain runs its values in parallel. A function in a chain becomes a step. Passthrough keeps what you already have.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.