# Runnables: Lambda, Parallel, Passthrough and Batch — LangChain / LlamaIndex

Source: https://www.geekswithgeeks.com/en/langchain-llamaindex/c-runnables

> Wrap plain functions and run steps side by side.

## Plain code as a pipeline step

Not every step needs an LLM. **`RunnableLambda`** wraps any Python function so it can sit in a chain. **`RunnableParallel`** (or a dict inside a chain) runs several runnables on the same input and returns a dict of their results, useful for fetching context and the question together. **`RunnablePassthrough`** forwards the input unchanged, so you can keep the original question alongside computed values. **`batch`** runs a chain over many inputs, concurrently where possible. Using runnables for pre- and post-processing keeps your logic visible in one chain and easy to trace.

## Lambda, parallel and batch, run

I ran this offline in a Python virtual environment with langchain-core 1.6.6, langchain-text-splitters 1.1.2 and llama-index-core 0.14.25. No API key or network call is needed because a fake model or a toy embedding stands in for the real one. The pipeline cleans the text then counts words (3). The parallel runnable returns the cleaned text, the word count and the untouched original in one dict. `batch` handles three inputs and returns [2, 4, 0].

```python
from langchain_core.runnables import RunnableLambda, RunnablePassthrough, RunnableParallel

clean = RunnableLambda(lambda s: s.strip().lower())
count = RunnableLambda(lambda s: len(s.split()))

pipeline = clean | count
print(pipeline.invoke("  Hello Big WORLD  "))

both = RunnableParallel(text=clean, words=clean | count, original=RunnablePassthrough())
print(both.invoke("  Hello Big WORLD  "))
print(pipeline.batch(["a b", "a b c d", ""]))

```

Output:

```
3
{'text': 'hello big world', 'words': 3, 'original': '  Hello Big WORLD  '}
[2, 4, 0]
```

**Quiz:** What does RunnableParallel return?

- [x] A dict of the results of each branch run on the same input
- [ ] Only the first result
- [ ] Nothing
- [ ] A trained model

*Answer:* A dict of the results of each branch run on the same input. Each key holds the output of one branch.
