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.

A question-answering workflow may need to retrieve context, preserve the user’s question, build a prompt, and call a model. LangChain’s Runnable interface gives components a common way to accept input, process it, and return output, so they can be composed into a chain. section 3.1.2 section 7.3.3

LangChain Expression Language (LCEL) uses the pipe operator | to compose sequential steps: one step’s output becomes the next step’s input. A chain itself follows the Runnable protocol, so it can be executed through the same methods as its components. section 4.8

Common operations and building blocks include:

  • invoke() runs once, stream() returns streaming output, and batch() processes a batch. Each has an asynchronous version. section 4.8
  • RunnableLambda wraps a Python callable so custom processing can participate in a chain without a separate Runnable class. section 3.1.2
  • RunnableParallel runs operations on the same input concurrently; .map() runs a chain for each item in a list. section 4.8
  • RunnablePassthrough preserves an input for use elsewhere in a chain. section 4.8

A retrieval-augmented question-answering chain

Given a vector database, create a retriever with vector_db.as_retriever(), a question feeder with RunnablePassthrough(), and a chat model. The chain sends the question to both the retriever and the feeder, then supplies their outputs as context and question to the prompt. Call rag_chain.invoke(question) to run it. section 7.3.3

rag_chain = {"context": retriever, 
             "question": question_feeder}|rag_prompt|chatbot

The dictionary supplies context and question to the prompt; the pipe operators then connect the prompt to the chat model. section 7.3.3

Wrap a text splitter

RunnableLambda can turn text splitting into a chain step. This example passes an input string to TokenTextSplitter and returns a list of dictionaries containing the resulting chunks. section 3.1.2

text_chunks_chain = (
    RunnableLambda(lambda x: 
        [
            {
                'chunk': text_chunk, 
            }
            for text_chunk in
               TokenTextSplitter(chunk_size=3000,
                                 chunk_overlap=100).split_text(x)
        ]
    )
)

The callable configures chunk size and overlap, splits the input, and packages each chunk under the chunk key. section 3.1.2

LCEL also supports fallbacks and tracing through LangSmith. These can help with error handling and debugging, but composing a chain still requires attention to how each step’s output fits the next step’s input. section 4.7 section 4.8

How AI Agents and Applications covers it

How deep it goes
Defines the interface and execution methods, then demonstrates composition, parallelism, and wrapping custom Python functions.
Examples
Demonstrates text chunking with RunnableLambda and a question-answering chain with a retriever and RunnablePassthrough.
What you can do after
Apply it via LangChain to compose and invoke chains, including custom processing steps.
How it connects
Connects LCEL chains with retrieval-augmented generation, prompt templates, chat models, and parallel execution.

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.