liveBook · Manning

Runnable interface

As explained in AI Agents and Applications

The Runnable interface is a common LangChain programming interface for components that can be invoked and composed in workflows.

An LLM workflow often has several jobs: prepare input, retrieve context, call a model, and format its answer. LangChain’s Runnable interface gives chain components a common way to accept input, process it, and return output. That shared contract lets components fit together without custom glue code for every step. section 3.1.2 section 1.2.1

A Runnable chain supports several ways to execute and compose work:

  • Use `invoke()` for one execution, `stream()` for streaming output, or `batch()` for multiple inputs; asynchronous versions are also available. section 4.8
  • Use LCEL’s `|` operator to pass each component’s output to the next. Use `RunnableParallel` for independent operations on the same input. section 4.8
  • Wrap a Python callable in `RunnableLambda` to add custom processing without writing a class that implements Runnable. section 3.1.2

Add a text-splitting step

A summarization workflow can wrap a function that splits input text into chunks. `RunnableLambda` turns that Python callable into a chain component: `text_chunks_chain = RunnableLambda(lambda x: [{'chunk': chunk} for chunk in TokenTextSplitter(chunk_size=3000, chunk_overlap=100).split_text(x)])` The step receives text and returns a list of chunk dictionaries for later processing. section 3.1.2

Pass a question into a RAG chain

A retrieval-augmented generation chain needs both retrieved context and the original question. A retriever supplies `context`, while `RunnablePassthrough()` preserves the input as `question`: `rag_chain = {"context": retriever, "question": RunnablePassthrough()} | rag_prompt | chatbot` `answer = rag_chain.invoke(question)` The dictionary prepares the prompt inputs; the pipe then connects the prompt and chat model. section 7.3.3

For a larger workflow, build separate chains for tasks such as search, fetching, extraction, and summarization, then combine them. LCEL also supports fallbacks, parallel execution, and tracing through LangSmith. The interface makes these arrangements consistent; it does not remove the need to decide which steps depend on one another. section 4.7 section 4.8

How AI Agents and Applications covers it

How deep it goes
Defines the input-processing-output contract and demonstrates execution and composition methods, without detailing the interface’s internals.
Examples
Shows token-based text splitting through RunnableLambda and a RAG question routed through RunnablePassthrough.
What it assumes
Assumes basic Python callables for custom steps and shows API-key setup before running a model-backed chain.
What you can do after
Apply it via LangChain LCEL to assemble, invoke, and parallelize modular chains.
How it connects
Connects LCEL chains with prompt templates, retrievers, chat models, RunnableLambda, and RunnablePassthrough.

Also explained in

Compare how 2 books explain Runnable interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.