Skip to main content
In this section, we will learn how to integrate custom operations (like text transformations or calculation tasks) into our LCEL pipelines using RunnableLambda.

Objectives

  1. Dynamically append processing steps to an existing LCEL chain.
  2. Build custom transformers (such as uppercasing and word counting) using RunnableLambda.
  3. Understand how custom logic execution behaves within a sequence.

LCEL Chains Extension Plan

Goal

Extend a standard prompt-model-parser chain with custom formatting functions to convert the output to uppercase and count the total words in the response.

Sample Input

Sample Output

An uppercase text report stating the word count followed by the jokes.

Plan

  1. Initialize prompt, model, and string output parser.
  2. Define a RunnableLambda to convert response strings to uppercase.
  3. Define another RunnableLambda that splits the output, counts the words, and prepends the count metadata.
  4. Construct the extended LCEL chain: chain = prompt_template | model | StrOutputParser() | uppercase_output | count_words.
  5. Invoke the chain.

Code Implementation

The following example shows a pipeline where the prompt formats, the model generates jokes, the string parser extracts text, and two custom RunnableLambda steps convert the text to uppercase and count the total words:

Practice & Exercises

To reinforce what you’ve learned in this section, practice with the interactive notebook:

Practice & Exercises

Practice extending sequences with downstream functions, modifying text formats, and adding analytical steps.💻 VS Code | 🚀 Colab | 📥 Download