In a chain, each step only sees the output of the step before it. But sometimes a later step needs the original input too. Runnable passthrough carries data along, unchanged.
Here, a runnable parallel runs two branches. Question uses runnable passthrough, so it's just the original input. Length is a small function that counts its words. Run it. The output keeps the original question, next to the new value. This is exactly how retrieval chains keep the user's question, while adding documents beside it.
passthrough.pypython
from langchain_core.runnables import RunnableParallel, RunnablePassthroughstep = RunnableParallel( question=RunnablePassthrough(), length=lambda q: len(q.split()),)print(step.invoke("Do you have any books about the sea?"))
Output
{'question': 'Do you have any books about the sea?', 'length': 8}
Assign is a related trick. It keeps every key of a dictionary input, and adds new ones, computed from it. Run it. The title is still there, and a new key, title length, has been added. Later steps can use both.
assign.pypython
from langchain_core.runnables import RunnablePassthroughadd_length = RunnablePassthrough.assign(title_length=lambda d: len(d["title"]))print(add_length.invoke({"title": "Pride and Prejudice"}))
Output
{'title': 'Pride and Prejudice', 'title_length': 19}
Runnable passthrough passes the input through unchanged, and assign adds new keys while keeping the old ones. Next, choosing different paths for different inputs.