Skip to main content

Overview

Runnable Chains provide a declarative way to compose AI components into sophisticated chains. Without Runnable Chains, building complex AI workflows requires verbose code with manual data passing and error handling. With Runnable Chains, you can:
  • Chain components elegantly using the pipe operator (|) for readable, maintainable code
  • Execute operations in parallel to maximize performance and reduce latency
  • Build dynamic chains that adapt based on input conditions
  • Manage conversation history seamlessly across interactions
  • Integrate tools and structured outputs with minimal boilerplate
  • Visualize your chains to understand complex workflows at a glance
Runnable Chains make your AI applications more modular, testable, and production-ready.

Core Concepts

The Pipe Operator

The foundation of Runnable Chains is the pipe operator (|), which chains components together. Each component receives the output of the previous one:

Runnables

Every component in Runnable Chains is a Runnable - an object with invoke() and ainvoke() methods. This unified interface allows any component to be used in chains:
  • Prompts: ChatPromptTemplate
  • Models: Any model from infer_model()
  • Functions: Wrapped with RunnableLambda or @chain decorator
  • Data structures: Automatically converted (dicts become RunnableParallel)

Building Blocks

ChatPromptTemplate

Create dynamic prompts with variable substitution and message formatting.

Simple Templates

Message-Based Templates

Chat History with Placeholders

Few-Shot Examples


Model Integration

Models implement the Runnable interface and can be enhanced with memory, tools, and structured outputs.

Basic Model Usage

Adding Memory

Binding Tools

Structured Output

Combining Features


RunnableParallel

Execute multiple chains simultaneously and collect results in a dictionary.

Use Case: RAG with Multiple Retrievers


RunnablePassthrough

Pass input through unchanged or with additional assignments.

Basic Passthrough

Assigning Values


RunnableLambda

Wrap Python functions to use them in chains.

Processing Chain Results


RunnableBranch

Route execution to different chains based on conditions.

With Classification


@chain Decorator

Create custom Runnable functions with full control.

Dynamic Chain Construction


Advanced Patterns

RAG with Context Management

Multi-Stage Processing

Parallel + Sequential Combinations


Visualization

ASCII Visualization

Output:

Mermaid Diagrams

Output:

Async Execution

All Runnable Chain components support async/await:

Best Practices

1. Keep Chains Readable

2. Use Descriptive Names

3. Extract Reusable Components

4. Handle Errors Gracefully

5. Test Chains Incrementally


Common Use Cases

Chatbot with Memory

Document Q&A

Agent with Tools

Content Generation Pipeline


Model Methods

  • add_memory(history=True, memory=None): Add conversation history
  • bind_tools(tools, tool_call_limit=5): Attach tools to model
  • with_structured_output(schema): Configure Pydantic output

Ready to build? Start with a simple chain and gradually add complexity. Runnable Chains grow with your needs!