> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 3. Chains Extended

> Extends chains with custom processing lambdas for modifying model outputs

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

```python theme={null}
{"topic": "lawyers", "joke_count": 3}
```

#### 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:

```python theme={null}
from dotenv import load_dotenv
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnableLambda
from langchain_openai import ChatOpenAI

# Load environment variables from .env
load_dotenv()

# Create a ChatOpenAI model
model = ChatOpenAI(model="gpt-4o")

# Define prompt templates
prompt_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a comedian who tells jokes about {topic}."),
        ("human", "Tell me {joke_count} jokes."),
    ]
)

# Define additional processing steps using RunnableLambda
uppercase_output = RunnableLambda(lambda x: x.upper())
count_words = RunnableLambda(lambda x: f"Word count: {len(x.split())}\n{x}")

# Create the combined chain using LangChain Expression Language (LCEL)
chain = prompt_template | model | StrOutputParser() | uppercase_output | count_words

# Run the chain
result = chain.invoke({"topic": "lawyers", "joke_count": 3})

# Output
print(result)
```

***

## Practice & Exercises

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

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice extending sequences with downstream functions, modifying text formats, and adding analytical steps.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langchain/3_chains_extended.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langchain/3_chains_extended.ipynb) | <a href="/public/notebooks/langchain/3_chains_extended.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
