Runnable

Open in ColabOpen in GitHub

Overview

LangChain's Runnable objects provide a modular and flexible approach to designing workflows by enabling the chaining, parallel execution, and transformation of data. These utilities allow for efficient handling of structured inputs and outputs, with minimal code overhead.

Key Components is:

  • RunnableLambda: A lightweight utility that enables the application of custom logic through lambda functions, ideal for dynamic and quick data transformations.

  • RunnablePassthrough: Designed to pass input data unchanged or augment it with additional attributes when paired with the .assign() method.

  • itemgetter: A Python operator module utility for efficiently extracting specific keys or indices from structured data such as dictionaries or tuples.

These tools can be combined to build powerful workflows, such as:

  • Extracting and processing specific data elements using itemgetter.

  • Performing custom transformations with RunnableLambda.

  • Creating end-to-end data pipelines with Runnable chains.

By leveraging these components, users can design scalable and reusable pipelines for machine learning and data processing workflows.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can also load the OPEN_API_KEY from the .env file.

Efficient Data Handling with RunnablePassthrough

RunnablePassthrough is a utility designed to streamline data processing workflows by either passing input data unchanged or enhancing it with additional attributes. Its flexibility makes it a valuable tool for handling data in pipelines where minimal transformation or selective augmentation is required.

  1. Simple Data Forwarding

  • Suitable for scenarios where no transformation is required, such as logging raw data or passing it to downstream systems.

  1. Dynamic Data Augmentation

  • Enables the addition of metadata or context to input data for use in machine learning pipelines or analytics systems.


  • RunnablePassthrough can either pass the input unchanged or append additional keys to it.

  • When RunnablePassthrough() is called on its own, it simply takes the input and passes it as is.

  • When called using RunnablePassthrough.assign(...), it takes the input and adds additional arguments provided to the assign function.

RunnablePassthrough

When invoking the chain with invoke(), the input data must be of type dictionary.

However, with the update to the LangChain library, if the template includes only one variable, it is also possible to pass just the value directly.

Here is an example using RunnablePassthrough.RunnablePassthrough is a runnable object with the following characteristics:

  1. Basic Operation

    • Performs a simple pass-through function that forwards input values directly to output

    • Can be executed independently using the invoke() method

  2. Use Cases

    • Useful when you need to pass data through chain steps without modification

    • Can be combined with other components to build complex data pipelines

    • Particularly helpful when you need to preserve original input while adding new fields

  3. Input Handling

    • Accepts dictionary-type inputs

    • Can handle single values as well

    • Maintains data structure throughout the chain

Here is an example of creating a chain using RunnablePassthrough.

Here is a comparison of the results when using RunnablePassthrough.assign().

RunnablePassthrough.assign()

  • Combines the key/value pairs from the input with the newly assigned key/value pairs.

Efficient Parallel Execution with RunnableParallel

RunnableParallel is a utility designed to execute multiple Runnable objects concurrently, streamlining workflows that require parallel processing. It distributes input data across different components, collects their results, and combines them into a unified output. This functionality makes it a powerful tool for optimizing workflows where tasks can be performed independently and simultaneously.

  1. Concurrent Execution

    • Executes multiple Runnable objects simultaneously, reducing the time required for tasks that can be parallelized.

  2. Unified Output Management

    • Combines the results from all parallel executions into a single, cohesive output, simplifying downstream processing.

  3. Flexibility

    • Can handle diverse input types and support complex workflows by distributing the workload efficiently.

Chains can also be applied to RunnableParallel.

Dynamic Processing with RunnableLambda

RunnableLambda is a flexible utility that allows developers to define custom data transformation logic using lambda functions. By enabling quick and easy implementation of custom processing workflows, RunnableLambda simplifies the creation of tailored data pipelines while ensuring minimal setup overhead.

  1. Customizable Data Transformation

    • Allows users to define custom logic for transforming input data using lambda functions, offering unparalleled flexibility.

  2. Lightweight and Simple

    • Provides a straightforward way to implement ad-hoc processing without the need for extensive class or function definitions.

Extracting Specific Keys Using itemgetter

itemgetter is a utility function from Python's operator module with the following features and benefits:

  1. Core Functionality

    • Efficiently extracts values from specific keys or indices in dictionaries, tuples, and lists

    • Capable of extracting multiple keys or indices simultaneously

    • Supports functional programming style

  2. Performance Optimization

    • More efficient than regular indexing for repetitive key access operations

    • Optimized memory usage

    • Performance advantages when processing large datasets

  3. Usage in LangChain

    • Data filtering in chain compositions

    • Selective extraction from complex input structures

    • Combines with other Runnable objects for data preprocessing

Last updated