Runnables, LCEL and RunnableConfig
Every LangChain component (prompts, chat models, parsers, retrievers) implements the Runnable interface, and LCEL composes them with `|`. Know how values flow and get coerced, how the wrappers (`with_retry`, `with_fallbacks`, `bind`, `with_config`) change behaviour, and how `RunnableConfig` fields reach nested steps.
Key points
- 1
The Runnable interface is
invoke,batchandstream, plusainvoke,abatchandastream. Each takes one input and an optionalconfig. The defaultbatchrunsinvokein parallel on a thread pool, and the default async methods run the sync version in a thread executor. - 2
a | bbuilds aRunnableSequence, where each output feeds the next step.prompt | model | StrOutputParser()returns a plainstr, and without the parser you get anAIMessage. - 3
Coercion happens only when a Runnable is one of the operands. A dict becomes
RunnableParallel(every value gets the same input and the output is a dict with the same keys), and a function oritemgetterbecomesRunnableLambda. Joiningf1 | f2raisesTypeError, anddict | dictis just Python's dict merge. - 4
RunnablePassthrough()returns its input unchanged.RunnablePassthrough.assign(k=fn)keeps the input dict and adds computed keys, and raisesValueErrorif the input is not a dict..pick("k")returns one value, while.pick(["a", "b"])returns a smaller dict. - 5
A
RunnableLambdafunction receives exactly one input (optionally plusconfig), so pack multiple values into a dict. If the function returns a Runnable, that Runnable is invoked with the same input, which is the basis of function-based routing. Nesting is bounded byrecursion_limit(default 25), and exceeding it raisesRecursionError. - 6
RunnableBranch((cond, runnable), ..., default)checks conditions lazily in order, runs only the first match on the original input, and requires a trailing default. - 7
@chainturns a function into a named Runnable (aRunnableLambda). Runnables invoked inside it inherit its config through a context variable and are traced as its children. - 8
bind(**kwargs)returns aRunnableBindingthat passes fixed keyword arguments on every call.with_config(...)attaches config such as tags, and child configs are merged with the parent's rather than replacing them. - 9
with_retry(retry_if_exception_type=(Exception,), stop_after_attempt=3, wait_exponential_jitter=True)counts total attempts. Errors outside the listed types fail immediately, and inbatchonly the failing inputs are retried. Keep the retry scope as small as possible, usually the model. - 10
with_fallbacks([b], exceptions_to_handle=(Exception,), exception_key=None)tries the fallbacks in order. Unlisted error types propagate. Withexception_keyset, the fallback receives the input dict plus the exception, so inputs must be dicts. If everything fails, the first error is re-raised. - 11
Wrapper order matters.
p.with_retry().with_fallbacks([b])runs p, p, b, whereasp.with_fallbacks([b]).with_retry()retries the whole group (p, b, p, b). A model-level fallback cannot catch a later parser error, so wrap the whole chain if you need that. - 12
Inheritance across
RunnableConfigfields:tags,metadata,callbacksandconfigurableflow to sub-calls, butrun_nameandrun_iddo not.max_concurrencycapsbatchparallelism.configurable_fields(a parameter) andconfigurable_alternatives(a whole component) readconfig["configurable"], and an unknown alternative name raisesValueError.
Common traps
batchis client-side concurrency, not a provider batch API. Results come back in input order, unlikebatch_as_completed.Callbacks passed to a component's constructor only see that component's runs. Use
config={"callbacks": [...]}to observe the whole chain.Comparing a classifier's output to a string without
StrOutputParser()compares against anAIMessage, so it is always False and the router silently takes the default path.Inside a dict step,
RunnablePassthrough()gets the original input, not a sibling branch's output.Without a narrowed
exceptions_to_handle, a fallback model can hide real bugs in your own code.LLMChain,ConversationChainand the other legacy chains live inlangchain-classicin 1.x. Replace them withprompt | model(plus a parser), and usecreate_agentor LangGraph when the path is not fixed.
Read the source
Further reading
Suggest a resourceNo extra reading for Runnables, LCEL and RunnableConfig yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Runnables, LCEL and RunnableConfig
Ten questions, with the answer and explanation after each one.